0
0
Javaprogramming~10 mins

Abstract classes in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Abstract classes
Define abstract class
Declare abstract method(s)?
YesNo method body
Provide concrete method(s)
Create subclass
Implement abstract method(s)
Instantiate subclass object
Use methods (abstract implemented + concrete)
An abstract class defines methods without full code, subclasses must complete them before creating objects.
Execution Sample
Java
abstract class Animal {
    abstract void sound();
    void breathe() {
        System.out.println("Breathing...");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();
        d.breathe();
    }
}
Defines an abstract Animal class with an abstract sound method and a concrete breathe method; Dog class implements sound; then Dog object calls both methods.
Execution Table
StepActionEvaluationResult
1Define abstract class Animal with abstract method sound()Animal cannot be instantiatedAnimal is a blueprint with abstract method sound()
2Define concrete method breathe() in AnimalMethod breathe() has implementationbreathe() prints 'Breathing...' when called
3Create subclass Dog extends AnimalDog inherits Animal's methodsDog must implement abstract method sound()
4Implement sound() in Dogsound() prints 'Bark'Dog now concrete, can be instantiated
5Instantiate Dog d = new Dog()Object d createdd is a Dog object
6Call d.sound()Calls Dog's sound()Output: Bark
7Call d.breathe()Calls Animal's breathe()Output: Breathing...
8Try to instantiate Animal (not shown)Compilation errorCannot instantiate abstract class Animal
💡 Execution stops after Dog object calls methods; Animal cannot be instantiated directly.
Variable Tracker
VariableStartAfter Step 5After Step 6After Step 7Final
dnullDog object createdMethod sound() calledMethod breathe() calledDog object with methods used
Key Moments - 3 Insights
Why can't we create an object of the abstract class Animal?
Because Animal has an abstract method sound() without implementation, Java prevents creating objects from incomplete classes (see execution_table step 1 and 8).
How does Dog class use the breathe() method without defining it?
Dog inherits breathe() from Animal, which has a concrete implementation, so Dog objects can use it directly (see execution_table step 7).
What happens if Dog does not implement the abstract method sound()?
Dog would also become abstract and cannot be instantiated, so Java requires Dog to implement sound() to create objects (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed when d.sound() is called at step 6?
A"Breathing..."
B"Bark"
CNothing, it causes an error
D"sound() method called"
💡 Hint
Check execution_table row for step 6 where d.sound() is called and output is shown.
At which step does the Dog class become concrete and instantiable?
AStep 4
BStep 5
CStep 3
DStep 1
💡 Hint
Look at execution_table step 4 where Dog implements sound(), making it concrete.
If we try to create new Animal(), what happens according to the execution table?
ARuntime error occurs
BObject is created successfully
CCompilation error: Cannot instantiate abstract class
DAnimal's sound() method is called
💡 Hint
See execution_table step 8 about instantiating Animal.
Concept Snapshot
abstract class ClassName {
  abstract void methodName(); // no body
  void concreteMethod() { ... } // optional
}

- Abstract classes cannot be instantiated.
- Subclasses must implement all abstract methods.
- Can have concrete methods usable by subclasses.
- Used to define a common template with some methods left to subclasses.
Full Transcript
An abstract class in Java is a class that cannot be instantiated directly because it may contain abstract methods without implementation. It acts as a blueprint for other classes. Abstract methods are declared without a body and must be implemented by subclasses. Concrete methods in the abstract class can be used by subclasses as is. In the example, Animal is abstract with an abstract method sound() and a concrete method breathe(). Dog extends Animal and implements sound(), making Dog concrete and instantiable. The Dog object can call both sound() and breathe(). Trying to instantiate Animal directly causes a compilation error. This structure helps organize code by defining common behavior and forcing subclasses to provide specific implementations.