Abstract classes let you create a base blueprint for other classes. They help share common code but cannot be used to make objects directly.
Abstract classes in Java
Start learning this pattern below
Jump into concepts and practice - no test required
abstract class Animal { // Abstract method (no body) abstract void makeSound(); // Regular method void sleep() { System.out.println("Sleeping..."); } }
An abstract class is declared with the keyword abstract.
It can have both abstract methods (without body) and regular methods (with body).
Vehicle is abstract and Car provides the method body.abstract class Vehicle { abstract void startEngine(); } class Car extends Vehicle { void startEngine() { System.out.println("Car engine started"); } }
area() must be implemented by Circle.abstract class Shape { abstract double area(); } class Circle extends Shape { double radius; Circle(double radius) { this.radius = radius; } double area() { return Math.PI * radius * radius; } }
plugIn() and abstract methods like turnOn().abstract class Appliance { void plugIn() { System.out.println("Plugged in"); } abstract void turnOn(); } class Fan extends Appliance { void turnOn() { System.out.println("Fan is on"); } }
This program shows an abstract class Animal with an abstract method makeSound() and a regular method sleep(). The subclasses Dog and Cat implement makeSound(). We cannot create an Animal object directly.
abstract class Animal { abstract void makeSound(); void sleep() { System.out.println("Sleeping..."); } } class Dog extends Animal { void makeSound() { System.out.println("Bark"); } } class Cat extends Animal { void makeSound() { System.out.println("Meow"); } } public class Main { public static void main(String[] args) { // Animal animal = new Animal(); // Error: Cannot instantiate abstract class Dog dog = new Dog(); Cat cat = new Cat(); System.out.println("Dog says:"); dog.makeSound(); dog.sleep(); System.out.println("Cat says:"); cat.makeSound(); cat.sleep(); } }
Time complexity depends on the methods implemented in subclasses, not the abstract class itself.
Abstract classes use memory only when instantiated through subclasses.
Common mistake: Trying to create an object of an abstract class directly causes a compile error.
Use abstract classes when you want to share code and force subclasses to implement certain methods. Use interfaces if you only want to define method signatures without any code.
Abstract classes cannot be instantiated but can have both abstract and regular methods.
Subclasses must implement all abstract methods.
They help organize code by sharing common behavior and enforcing method implementation.
Practice
abstract classes in Java is true?Solution
Step 1: Understand abstract class instantiation
Abstract classes cannot be instantiated directly, meaning you cannot create objects from them usingnew.Step 2: Check method rules in abstract classes
Abstract classes can have both abstract methods (without body) and regular methods (with code). So, not all methods must be abstract.Final Answer:
You cannot create an object directly from an abstract class. -> Option DQuick Check:
Abstract class instantiation = not allowed [OK]
- Thinking abstract classes can be instantiated
- Believing all methods must be abstract
- Confusing abstract classes with interfaces
Solution
Step 1: Recall Java syntax for abstract classes
The keywordabstractmust come before the keywordclassin the declaration.Step 2: Check each option's order
Only abstract class MyClass {} has the correct order:abstract class MyClass {}. Others have incorrect keyword order.Final Answer:
abstract class MyClass {} -> Option AQuick Check:
abstract class syntax = 'abstract class' [OK]
- Placing abstract after class
- Mixing keyword order
- Omitting abstract keyword
abstract class Animal {
abstract void sound();
void sleep() {
System.out.println("Sleeping");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
a.sleep();
}
}Solution
Step 1: Understand method calls on abstract class reference
The variableais of typeAnimalbut refers to aDogobject. Callingsound()calls Dog's implementation, printing "Bark".Step 2: Call the concrete method from abstract class
Callingsleep()uses the method defined inAnimal, printing "Sleeping".Final Answer:
Bark Sleeping -> Option CQuick Check:
Dog sound then Animal sleep = Bark then Sleeping [OK]
- Expecting compilation error for abstract class reference
- Confusing method call order
- Thinking abstract class methods can't be called
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
class Square extends Shape {
}
public class Test {
public static void main(String[] args) {
Shape s = new Square();
s.draw();
}
}Solution
Step 1: Check abstract method implementation in subclasses
The abstract methoddraw()inShapemust be implemented by all non-abstract subclasses.Step 2: Verify Square class implementation
TheSquareclass does not implementdraw()and is not declared abstract, causing a compilation error.Final Answer:
Square class must implement the abstract method draw() -> Option AQuick Check:
All abstract methods must be implemented in concrete subclasses [OK]
- Forgetting to implement abstract methods
- Thinking abstract class objects can be created
- Marking implemented methods as abstract
startEngine() but each vehicle starts differently. Which approach using abstract classes is best?Solution
Step 1: Understand the need for shared method with different implementations
SincestartEngine()must be shared but implemented differently, an abstract method enforces subclasses to provide their own version.Step 2: Choose abstract class with abstract method
DeclaringVehicleas abstract with abstractstartEngine()ensures all subclasses implement it, sharing the concept but customizing behavior.Final Answer:
Make an abstract class Vehicle with an abstract method startEngine(), then subclasses implement it. -> Option BQuick Check:
Abstract class with abstract method enforces implementation [OK]
- Using concrete method without forcing override
- Confusing interfaces with abstract classes
- Making class final prevents subclassing
