Abstract Classes and Methods

1. What are abstract classes and methods?

  • When multiple subclasses share common features but the method body cannot be determined, the method in the parent class is defined as abstract.
  • It forces subclasses to rewrite the method in a specific format. Since inheritance of an abstract class requires either the subclass to be abstract or to override all abstract methods in the parent class.
  • A class containing an abstract method must itself be abstract.

2. Syntax for abstract classes and methods?

  • public abstract return-type method-name (parameter-list);
  • public abstract class class-name

Note: An abstract method has no body, i.e., no curly braces or content inside them.

3. What should be noted when inheriting an abstract class?

  • Abstract classes cannot be instantiated.
  • When a class inherits an abstract class, it must either override all abstract methods from the parent class.
  • Alternatively, the subclass can also be abstract, or use an adapter to override all abstract methods of the parent class, allowing the subclass to only override its own unique methods.

4. Why can't abstract classes be instantiated?

Because an abstract class is inherently "incomplete" or "unfinished", and Java does not allow creating an incomplete object.

1. From a syntax perspective

An abstract class is marked with abstract, and its design purpose is:

  • To act as a parent class, used for inheritance, and not exist independently

Java syntax explicitly states:

  • new abstract-class() → Compilation error
  • Only concrete classes can be instantiated

2. From a logical perspective: abstract classes are "unfinished"

An abstract class may contain abstract methods:

abstract class Animal {

// Declaration without body
public abstract void eat();

}

  • eat() is just a contract, without implementation
  • If you could new Animal(), calling eat() would leave the JVM unsure of what code to execute
  • Hence, Java prohibits the creation of such logically incomplete objects

3. From a design perspective

The purpose of an abstract class is:

  • To extract the common structure of subclasses
  • To enforce subclasses to implement certain methods
  • It does not represent any specific entity

For example:

  • Animal is abstract
  • Actual instances are Dog, Cat
  • You cannot create an "abstract animal," only concrete dogs and cats

4. Why can an abstract class have a constructor if it cannot be instantiated?

Many people wonder: An abstract class can have a constructor, so why can't it be instantiated?

Because:

  • The constructor of an abstract class is not for its own use
  • It is called by subclasses during construction (via super())
  • When a subclass is instantiated, it first initializes the abstract parent class's members (the constructor is used to initialize the parent class's member variables, not to create the object itself)
  • But the abstract class itself still cannot be created directly

Additional notes:

  • An abstract class does not necessarily need to have abstract methods
  • However, any method declared as abstract must belong to an abstract class
  • Typically, abstract classes have abstract methods because otherwise they have no purpose

Use cases for abstract classes

If you do not know which specific subclass you are dealing with, for example:

Father f = getOneAnimal(); // Could return Son, Daughter, GrandSon...

You don't know who it is, so you can't cast it.

At this point, abstract classes become useful.

The real purpose of an abstract class is to provide a unified interface, without worrying about the specific type.

No matter which subclass it is, you can call the same method:

abstract class Father {

public abstract void work();

}

class Son extends Father {

@Override
public void work() {
    System.out.println("Son writes code");
}

}

class Daughter extends Father {

@Override
public void work() {
    System.out.println("Daughter draws");
}

}

When writing code, you can directly call the method using the parent class:

Father f = new Son(); f.work();

f = new Daughter(); f.work();

  • No casting required
  • No type checking needed
  • No need to know the actual type

This is the power of polymorphism + abstract classes: Unified interface, automatically adapted to different subclasses

What is the difference between casting and abstract classes?

  • Casting = You want special functionality

  • You must cast if you want to call a method unique to the subclass

  • Abstract class = You want unified functionality

  • You can call methods that all subclasses must implement directly through the parent class reference, without casting

A final summary:

  • To call a method unique to the subclass → Must cast
  • To call common behavior across all subclasses → Use abstract class / polymorphism

They are not alternatives, but serve different purposes:

  • Abstract class handles "common standards"
  • Casting handles "specific needs"

With an abstract class, you can call methods that the parent class has already declared and that the subclass has overridden, not the methods unique to the subclass:

The parent class has already declared the method, so the compiler allows it At runtime, the JVM looks for the implementation in the subclass That’s polymorphism

Distinguishing between casting and abstract classes

  • The purpose of an abstract class is that subclasses override the method, and the parent class method has no practical use, so the parent class only defines the method, making it abstract to achieve a unified interface.
  • Casting is used to access unique features of the subclass, where the method is not defined in the parent class. The compiler cannot find it at compile time, so we cast the parent class to the subclass type to access the method.

Abstract classes have constructors

In Java, the constructor of an abstract class cannot be used to instantiate the class directly, but its main purpose is to be called by subclasses to initialize the member variables defined in the abstract class. It is used to initialize the parent class's members when a subclass is constructed, since the subclass inherits the parent class's members and calls the parent class constructor via super()

1. Definition method: The constructor of an abstract class is defined similarly to a regular class, with or without parameters. 2. Subclass calling requirements: The subclass constructor must explicitly or implicitly call the abstract class constructor via super(), otherwise it will cause a compilation error. 3. Difference with interfaces: Interfaces do not have constructors because they do not define member variables and have no need for inheritance initialization.

Tags: abstract class java Polymorphism Inheritance method overriding

Posted on Wed, 08 Jul 2026 16:26:34 +0000 by Pazuzu156