Explore Topics
Abstraction means hiding unnecessary details and exposing only what matters. Think of driving a car — you use the steering wheel and pedals without knowing how the engine works internally. In Java, abstract classes are like blueprints for a house: they define the structure but leave some rooms unfinished. A subclass must complete the work by implementing all abstract methods. An abstract class can contain both abstract methods (no body) and concrete methods (with body). You use the abstract keyword to mark a class and its incomplete methods.
- Abstraction = hiding complexity, exposing only what matters
- Abstract class = incomplete class (like a house blueprint)
- Abstract method = declaration only, no implementation body
- Subclass MUST implement all inherited abstract methods
- Abstract classes CAN have concrete (complete) methods too
- Marked with the "abstract" keyword on both class and method
- Used to define a generalized form shared between subclasses
- ✦Defines a common template that all subclasses follow
- ✦Can share concrete code (DRY — Don't Repeat Yourself)
- ✦Supports runtime polymorphism through abstract references
- ✦Fields and constructors allow shared state initialization
- △Single inheritance only — a class can extend just one abstract class
- △Tight coupling — subclass is bound to the abstract class hierarchy
- △Cannot instantiate directly — requires a concrete subclass
- △Changes to the abstract class ripple through all subclasses
Quiz
abstract class Animal {
Animal() { speak(); }
abstract void speak();
}
class Dog extends Animal {
String sound = "Woof";
void speak() { System.out.println(sound); }
}
new Dog();