Abstract Classes and Methods


In this lesson we will learn about Abstract Classes and Abstract Methods in Java programminglangauge.

Abstract Methods
An abstract method is a method that is declared in a (super)class, but not defined. In order to be instantiated, a subclass must provide the definition.

Abstract Classes
An abstract class is any class that includes an abstract method. It is similar to Pure virtual in C++. If a class includes an abstract method, the class must be declared abstract, too.

Code Example:
abstract class AbstractClass {
   abstract public void Print();
   // no body, just the function header
}

class MyConcreteClass extends AbstractClass {
   public void Print() {
   // actual code goes here
   }
}
Important points to be remembered:
  • An abstract class cannot be instantiated.
  • A subclass of an abstract class that does not provide bodies for all abstract methods must also be declared abstract.
  • A subclass of a non-abstract class can override a (non-abstract) method of its superclass, and declare it abstract. In that case, the subclass must be declared abstract.