Abstract Classes

A class is a model for creating objects, contains description of properties or variables and actions or methods of its objects. If an object has the properties and actions as mentioned in the class, then that object belongs to the class. The rule is that anything is written in the class is applicable to all of its objects. If a method is written in the class, it is available as it is to all of the class objects.

Abstract Method And Class

An abstract method does not contain any body. It contains only the method header. So we can say it is an incomplete method. An abstract class is a class that generally contains some abstract methods. Both the abstract class and the abstract methods should be declared by using the keyword abstract. Since, abstract class contains incomplete methods, it is not possible to estimate the total memory required to create the object. So, JVM can not create objects to an abstract class. We should create subclasses and all the abstract methods should be implemented in the subclasses. Then it is possible to create objects to the subclasses since they are complete classes.

Points To Know About Abstract Class

  • An abstract class is a class that contains 0 or more abstract methods.

  • An abstract class can contain instance variables and concrete methods in addition to abstract methods.

  • We cannot declare a class as both abstract and final

  • It is possible to derive an abstract class as a subclass from a concrete super class

  • Abstract class and method is declared by using the keyword abstract.

  • All the abstract methods of the abstract class should be implemented in its subclasses

  • We cannot create an object to abstract class.

  • The reference of abstract class can not refer to individual methods of its subclasses

  • The reference of abstract class can be used to refer to objects of its subclasses

  • If any abstract method is not implemented, then that subclass should be declared as abstract. In this case, we cannot create an object to the subclass. We cannot create an object to the subclass. We should create another subclass to this subclass and implement the remaining abstract method here.

Example Of Abstraction

// All the objects need different implementations of the same method
abstract class Myclass {
    // this is abstract method
    abstract void calculate(double x);

class Sub1 extends Myclass {
    // calculate square value
    void calculate(double x) {
        System.out.println("Square= " + (x * x));

class Sub2 extends Myclass {
    // calculate square root value
    void calculate(double x) {
        System.out.println("Square root= " + Math.sqrt(x));

class Sub3 extends Myclass {
    // calculate cube value
    void calculate(double x) {
        System.out.println("Cube= " + (x * x * x));

class Different {
    public static void main(String[] args) {
        // create sub class objects
        Sub1 obj1 = new Sub1();
        Sub2 obj2 = new Sub2();
        Sub3 obj3 = new Sub3();
        // let the objects call and use calculate() method
        obj1.calculate(3); // calculate square value
        obj2.calculate(4); // calculate square root

        obj3.calculate(5); // calculate cube value
The output is 
Square= 9.0
Square root= 2.0
Cube= 125.0

In the above program we create Myclass as the abstract super class with an abstract method calculate(). This method does not have any body within it. Sub1, Sub2 and Sub3 are the three subclasses where the abstract method is implemented as per the requirement of the objects. Since, the same abstract method is implemented differently for different objects, they can perform different tasks.

Subscribe For More Content