STUDYECRAT Java : OOP - ABSTRACTION
60s
Java : OOP - ABSTRACTION
Prove your skills in this interactive quiz
Live Code
Run snippets directly
Timed
60s per question
Scored
Earn 3D badges
★ Java Abstraction: Key Interview Points
1. Abstract Class Fundamentals
abstract class Vehicle {
abstract void start(); // MUST be implemented
void stop() { System.out.print("Stopped"); }
}
// Concrete class must implement abstract methods
abstract void start(); // MUST be implemented
void stop() { System.out.print("Stopped"); }
}
// Concrete class must implement abstract methods
- Tip: Use
abstract
keyword for classes/methods that need partial implementation - Real Use: Framework base classes (e.g., HttpServlet in Spring)
2. Abstract Class Constructors
abstract class Database {
Database(String url) { // Abstract class CAN have constructors
System.out.print("Connecting to "+url);
}
}
// Called when concrete subclass is instantiated
Database(String url) { // Abstract class CAN have constructors
System.out.print("Connecting to "+url);
}
}
// Called when concrete subclass is instantiated
- Tip: Use constructors to enforce initialization logic in subclasses
- Real Use: JDBC driver initialization patterns
3. Partial vs Full Abstraction
abstract class Logger {
void log(String msg) { // Concrete method
System.out.print("LOG: "+msg);
}
abstract void debug(String msg); // Abstract method
}
// Mix of implemented and unimplemented methods
void log(String msg) { // Concrete method
System.out.print("LOG: "+msg);
}
abstract void debug(String msg); // Abstract method
}
// Mix of implemented and unimplemented methods
- Tip: Abstract classes allow partial implementation (unlike interfaces pre-Java 8)
- Real Use: Template Method pattern in frameworks
4. Abstract Class vs Interface
// Abstract Class:
abstract class Shape {
int sides; // Can have state
abstract void draw();
}
// Interface:
interface Drawable {
void draw(); // Pure abstraction
}
// Choose based on need for state vs behavior contracts
abstract class Shape {
int sides; // Can have state
abstract void draw();
}
// Interface:
interface Drawable {
void draw(); // Pure abstraction
}
// Choose based on need for state vs behavior contracts
- Tip: Use abstract classes for "is-a" and interfaces for "can-do" relationships
- Real Use: Collections framework (AbstractList vs List interface)
5. Abstract Method Constraints
abstract class PaymentGateway {
abstract void process(); // No body
// abstract static void validate(); // INVALID!
// abstract final void refund(); // INVALID!
}
// Abstract methods cannot be static/final/private
abstract void process(); // No body
// abstract static void validate(); // INVALID!
// abstract final void refund(); // INVALID!
}
// Abstract methods cannot be static/final/private
- Tip: Abstract methods must be overridden, so modifiers preventing this are illegal
- Real Use: Payment processing SDK design
💡 Pro Interview Tip
When asked about abstraction, always mention the template method pattern - it's the most practical application of abstract classes in enterprise Java.