Understanding Java Abstract Classes
Concept explanation of abstract classes that cannot be instantiated and may contain abstract methods
java (1.0+)
2025-11-03
abstract
oop
inheritance
concepts
Description
Abstract classes in Java are classes that cannot be instantiated directly. They serve as base classes for other classes and can contain both abstract methods (without implementation) and concrete methods (with implementation).
Key Concepts
What is an Abstract Class?
- Declared with the
abstractkeyword - Cannot be instantiated directly
- Can contain abstract methods (no body)
- Can contain concrete methods (with implementation)
- Can have constructors, fields, and both static and instance methods
Abstract Methods
- Declared with
abstractkeyword - Have no implementation (no body)
- Must be implemented by concrete subclasses
- Cannot be private, final, or static
When to Use Abstract Classes
- When you want to share code among closely related classes
- When you have some common implementation but some methods need to be implemented by subclasses
- When you want to provide a template for subclasses
- For “is-a” relationships where some behavior is shared
Abstract Class vs Interface
- Abstract class: Single inheritance, can have instance variables, various access modifiers, can have constructors
- Interface: Multiple inheritance, no instance variables (except constants), methods are public, no constructors
- Use abstract classes when you need shared implementation
- Use interfaces when you need to define contracts
Benefits
- Code reuse: Share common implementation
- Template method pattern: Define algorithm skeleton
- Forced implementation: Require subclasses to implement specific methods
- Flexibility: Mix abstract and concrete methods
Best Practices
- Use abstract classes for “is-a” relationships
- Provide meaningful default implementations when possible
- Keep abstract methods focused and specific
- Document expected behavior
- Consider using interfaces for more flexibility
Code
// Abstract classabstract class Animal { protected String name; // Constructor public Animal(String name) { this.name = name; } // Concrete method (has implementation) public void sleep() { System.out.println(name + " is sleeping"); } // Abstract method (no implementation) public abstract void makeSound(); // Abstract method public abstract void move();}// Concrete subclassclass Dog extends Animal { public Dog(String name) { super(name); } // Must implement abstract methods @Override public void makeSound() { System.out.println(name + " barks"); } @Override public void move() { System.out.println(name + " runs on four legs"); }}// Another concrete subclassclass Bird extends Animal { public Bird(String name) { super(name); } @Override public void makeSound() { System.out.println(name + " chirps"); } @Override public void move() { System.out.println(name + " flies"); }}// UsageAnimal dog = new Dog("Buddy");dog.sleep(); // Inherited concrete methoddog.makeSound(); // Overridden abstract methoddog.move(); // Overridden abstract methodAnimal bird = new Bird("Tweety");bird.sleep(); // Inherited concrete methodbird.makeSound(); // Overridden abstract methodbird.move(); // Overridden abstract method// Cannot instantiate abstract class// Animal animal = new Animal("Generic"); // Compile error
Comments
No comments yet. Be the first to comment!
Please login to leave a comment.