Bird
Raised Fist0
Javaprogramming~10 mins

Abstract vs concrete classes in Java - Visual Side-by-Side Comparison

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Abstract vs concrete classes
Define Abstract Class
Contains abstract methods?
YesCannot instantiate directly
Must subclass
Define Concrete Class
Can instantiate concrete class
Use methods (abstract implemented in subclass)
Abstract classes define a template with some methods unimplemented. Concrete classes fill in details and can be created as objects.
Execution Sample
Java
abstract class Animal {
  abstract void sound();
}

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();
  }
}
Defines an abstract Animal class with an abstract method sound, then a Dog class implements sound, creates Dog object, and calls sound.
Execution Table
StepActionEvaluationResult
1Define abstract class Animal with abstract method sound()Class created, cannot instantiateAnimal is abstract
2Define concrete class Dog extends Animal and implements sound()Dog provides method bodyDog is concrete
3Create Dog object d = new Dog()Dog is concrete, so object createdd is a Dog instance
4Call d.sound()Dog's sound() runsPrints 'Bark'
5Try to create Animal objectCompilation errorCannot instantiate abstract class
💡 Execution stops because abstract classes cannot be instantiated directly
Variable Tracker
VariableStartAfter Step 3After Step 4Final
dundefinedDog instance createdDog instance existsDog instance exists
Key Moments - 3 Insights
Why can't we create an object of an abstract class directly?
Because abstract classes have incomplete methods (see step 1 and 5 in execution_table), Java prevents creating objects from them to avoid missing method implementations.
How does the concrete class provide the missing method?
The concrete class (Dog) implements the abstract method sound() fully (step 2), so it can be instantiated and used (steps 3 and 4).
What happens if a concrete subclass does not implement all abstract methods?
It causes a compilation error because the subclass remains abstract implicitly and cannot be instantiated, similar to step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3?
AAn object of abstract class Animal is created
BAn object of concrete class Dog is created
CCompilation error occurs
DThe abstract method sound() is called
💡 Hint
Check the 'Action' and 'Result' columns at step 3 in the execution_table
At which step does the program print 'Bark'?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where d.sound() is called and output is produced in execution_table
If we try to instantiate Animal directly, what happens according to the execution table?
ACompilation error occurs
BObject is created successfully
CAbstract method runs
DProgram prints 'Bark'
💡 Hint
Refer to step 5 in execution_table about creating Animal object
Concept Snapshot
abstract class ClassName {
  abstract void method(); // no body
}

class ConcreteClass extends ClassName {
  void method() { /* implementation */ }
}

- Abstract classes cannot be instantiated.
- Concrete subclasses must implement all abstract methods.
- Concrete classes can be instantiated and used.
- Abstract classes define a template for subclasses.
Full Transcript
This visual execution shows how abstract and concrete classes work in Java. First, an abstract class Animal is defined with an abstract method sound. Abstract classes cannot be instantiated directly, so trying to create an Animal object causes a compilation error. Then, a concrete class Dog extends Animal and implements the sound method. Because Dog provides the missing method, it can be instantiated. When we create a Dog object and call its sound method, it prints 'Bark'. This demonstrates that abstract classes provide a blueprint, and concrete classes fill in the details to be usable objects.

Practice

(1/5)
1. Which statement best describes an abstract class in Java?
easy
A. It is the same as an interface and cannot have any methods with code.
B. It must have all methods fully implemented and can be instantiated.
C. It can have methods without implementation and cannot be instantiated directly.
D. It is a class that can only contain static methods.

Solution

  1. Step 1: Understand abstract class definition

    An abstract class can have methods without implementation (abstract methods) and cannot create objects directly.
  2. Step 2: Compare with other options

    Concrete classes have full method implementations and can be instantiated. Interfaces differ from abstract classes. Static-only classes are unrelated.
  3. Final Answer:

    It can have methods without implementation and cannot be instantiated directly. -> Option C
  4. Quick Check:

    Abstract class = no direct objects [OK]
Hint: Abstract classes can't create objects directly [OK]
Common Mistakes:
  • Thinking abstract classes can be instantiated
  • Confusing abstract classes with interfaces
  • Assuming all methods must be implemented
2. Which of the following is the correct way to declare an abstract class in Java?
easy
A. abstract class Vehicle {}
B. class abstract Vehicle {}
C. Vehicle abstract class {}
D. class Vehicle abstract {}

Solution

  1. Step 1: Recall Java syntax for abstract classes

    The keyword 'abstract' comes before 'class' followed by the class name.
  2. Step 2: Check each option

    Only 'abstract class Vehicle {}' matches correct syntax. The other options have incorrect keyword order.
  3. Final Answer:

    abstract class Vehicle {} -> Option A
  4. Quick Check:

    abstract keyword before class name [OK]
Hint: Put 'abstract' before 'class' keyword [OK]
Common Mistakes:
  • Placing 'abstract' after 'class'
  • Mixing keyword order
  • Omitting 'abstract' keyword
3. What will be the output of the following Java code?
abstract class Animal {
    abstract void sound();
}

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();
    }
}
medium
A. Runtime error
B. Animal sound
C. Compilation error
D. Bark

Solution

  1. Step 1: Understand class hierarchy and method overriding

    Animal is abstract with abstract method sound(). Dog extends Animal and implements sound() printing "Bark".
  2. Step 2: Analyze main method execution

    Animal reference points to Dog object. Calling a.sound() runs Dog's sound(), printing "Bark".
  3. Final Answer:

    Bark -> Option D
  4. Quick Check:

    Abstract method overridden = Dog's output [OK]
Hint: Abstract method calls run subclass implementation [OK]
Common Mistakes:
  • Expecting abstract class method to run
  • Thinking abstract class can be instantiated
  • Confusing compile and runtime errors
4. Identify the error in the following code snippet:
abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    // No draw() method implemented
}

public class Test {
    public static void main(String[] args) {
        Circle c = new Circle();
        c.draw();
    }
}
medium
A. Circle must implement the abstract method draw() or be declared abstract.
B. Cannot create object of class Circle.
C. Abstract class Shape cannot have abstract methods.
D. No error, code runs fine.

Solution

  1. Step 1: Check subclass implementation of abstract methods

    Circle extends Shape but does not implement abstract method draw().
  2. Step 2: Understand Java rules for abstract methods

    A concrete class must implement all abstract methods or be declared abstract itself. Circle is concrete but missing draw().
  3. Final Answer:

    Circle must implement the abstract method draw() or be declared abstract. -> Option A
  4. Quick Check:

    Concrete subclass must implement all abstract methods [OK]
Hint: Concrete class must implement all abstract methods [OK]
Common Mistakes:
  • Thinking abstract methods can be skipped
  • Assuming abstract class can't have abstract methods
  • Believing object creation is the error
5. You want to design a system where different types of employees calculate their salary differently. Which approach best uses abstract and concrete classes?
hard
A. Create only concrete classes for each employee type without any abstract class.
B. Create an abstract class Employee with an abstract method calculateSalary(), then create concrete subclasses like Manager and Developer implementing it.
C. Use an interface with no methods and concrete classes implementing it.
D. Create a concrete Employee class with a fixed calculateSalary() method used by all employees.

Solution

  1. Step 1: Identify need for shared rules with different implementations

    Employee types share concept of salary calculation but differ in details.
  2. Step 2: Use abstract class with abstract method

    Abstract class Employee defines calculateSalary() abstractly. Subclasses implement specific logic.
  3. Step 3: Evaluate other options

    Create only concrete classes for each employee type without any abstract class. lacks shared abstraction. Use an interface with no methods and concrete classes implementing it. uses interface with no methods, so no contract. Create a concrete Employee class with a fixed calculateSalary() method used by all employees. fixes salary calculation, no variation.
  4. Final Answer:

    Create an abstract class Employee with an abstract method calculateSalary(), then create concrete subclasses like Manager and Developer implementing it. -> Option B
  5. Quick Check:

    Abstract class sets rules, subclasses do work [OK]
Hint: Abstract class for rules, concrete classes for details [OK]
Common Mistakes:
  • Not using abstraction for shared behavior
  • Using concrete class with fixed method only
  • Interfaces without methods don't enforce contracts