Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Abstract Classes in Java

  • Difficulty Level : Easy
  • Last Updated : 29 Oct, 2021

In C++, if a class has at least one pure virtual function, then the class becomes abstract. Unlike C++, in Java, a separate keyword abstract is used to make a class abstract. 

Illustration: Abstract class

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

abstract class Shape {
    int color;

    // An abstract function
    abstract void draw();
}

Following are some important observations about abstract classes in Java.

  1. An instance of an abstract class can not be created.
  2. Constructors are allowed.
  3. We can have abstract class without any abstract method.
  4. Abstract classes can also have final methods
  5. We are not allowed to create object for any abstract class.
  6. We can define static methods in an abstract class

Let us elaborate over these observations and do justify them with help of clean java programs as follows.



Observation 1: In Java,just likely in C++ an instance of an abstract class cannot be created, we can have references to abstract class type though. It is as shown below via clean java program.

Example 

Java




// Java Program to Illustrate That an Iinstance of Abstract
// Class Can not be created
 
// Class 1
// Abstract class
abstract class Base {
    abstract void fun();
}
 
// Class 2
class Derived extends Base {
    void fun()
    {
        System.out.println("Derived fun() called");
    }
}
 
// Class 3
// Main class
class Main {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Uncommenting the following line will cause
        // compiler error as the line tries to create an
        // instance of abstract class. Base b = new Base();
 
        // We can have references of Base type.
        Base b = new Derived();
        b.fun();
    }
}


Output

Derived fun() called

Observation 2: Like C++, an abstract class can contain constructors in Java. And a constructor of abstract class is called when an instance of an inherited class is created. It is as shown in the program below as follows: 

Example 

Java




// Java Program to Illustrate Abstract Class
// Can contain Constructors
 
// Class 1
// Abstract class
abstract class Base {
 
    // Constructor of class 1
    Base()
    {
        // Print statement
        System.out.println("Base Constructor Called");
    }
 
    // Abstract method inside class1
    abstract void fun();
}
 
// Class 2
class Derived extends Base {
 
    // Constructor of class2
    Derived()
    {
        System.out.println("Derived Constructor Called");
    }
 
    // Method of class2
    void fun()
    {
        System.out.println("Derived fun() called");
    }
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Creating object of class 2
        // inside main() method
        Derived d = new Derived();
    }
}


Output

Base Constructor Called
Derived Constructor Called

Observation 3: In Java, we can have an abstract class without any abstract method. This allows us to create classes that cannot be instantiated but can only be inherited. It is as shown below as follows with help of clean java program.

Example 



Java




// Java Program to illustrate Abstract class
// Without any abstract method
 
// Class 1
// An abstract class without any abstract method
abstract class Base {
 
    // Demo method
    void fun()
    {
        // Print message if class 1 function is called
        System.out.println(
            "Function of Base class is called");
    }
}
 
// Class 2
class Derived extends Base {
}
 
// Class 3
class Main {
 
    // Main driver method
    public static void main(String args[])
    {
        // Creating object of class 2
        Derived d = new Derived();
 
        // Calling function defined in class 1 inside main()
        // with object of class 2 inside main() method
        d.fun();
    }
}


Output

Function of Base class is called

Observation 4: Abstract classes can also have final methods (methods that cannot be overridden)

Example 

Java




// Java Program to Illustrate Abstract classes
// Can also have Final Methods
 
// Class 1
// Abstract class
abstract class Base {
   
    final void fun()
    {
        System.out.println("Derived fun() called");
    }
}
 
// Class 2
class Derived extends Base {
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating object of abstract class
        Base b = new Derived();
 
        // Calling method on object created above
        // inside main()
        b.fun();
    }
}


Output

Derived fun() called

Observation 5: For any abstract java class we are not allowed to create an object i.e., for abstract class instantiation is not possible. 

Example 

Java




// Java Program to Illustrate Abstract Class
 
// Main class
// An abstract class
abstract class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Trying to create an object
        GFG gfg = new GFG();
    }
}


Output:

Observation 6: Similar to the interface we can define static methods in an abstract class that can be called independently without an object. 

Example 

Java




// Java Program to Illustrate Static Methods in Abstract
// Class Can be called Independently
 
// Class 1
// Abstract class
abstract class Helper {
 
    // Abstract method
    static void demofun()
    {
 
        // Print statement
        System.out.println("Geeks for Geeks");
    }
}
 
// Class 2
// Main class extending Helper class
public class GFG extends Helper {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Calling method inside main()
        // as defined in above class
        Helper.demofun();
    }
}


Output

Geeks for Geeks

Must Read:

 Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!