Abstract Classes in Java - GeeksforGeeks

21 Apr, 2022
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
abstract class Shape 
{
    int color;

    // An abstract function
    abstract void draw();
}
Following are some important observations about abstract classes in Java.
An instance of an abstract class can not be created.Constructors are allowed.We can have an abstract class without any abstract method.There can be final method in abstract class but any abstract method in class(abstract class) can not be declared as final  or in simper terms final method can not be abstract itself as it will yield error: “Illegal combination of modifiers: abstract and final”We are not allowed to create object for any abstract class.We can define static methods in an abstract class
An instance of an abstract class can not be created.
Constructors are allowed.
We can have an abstract class without any abstract method.
There can be final method in abstract class but any abstract method in class(abstract class) can not be declared as final  or in simper terms final method can not be abstract itself as it will yield error: “Illegal combination of modifiers: abstract and final”
We are not allowed to create object for any abstract class.
We can define static methods in an abstract class
Let us elaborate on 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 instance of Abstract// Class Can not be created // Class 1// Abstract classabstract class Base {    abstract void fun();} // Class 2class Derived extends Base {    void fun()    {        System.out.println("Derived fun() called");    }} // Class 3// Main classclass 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();    }}
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 classabstract class Base {     // Constructor of class 1    Base()    {        // Print statement        System.out.println("Base Constructor Called");    }     // Abstract method inside class1    abstract void fun();} // Class 2class 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 classclass GFG {     // Main driver method    public static void main(String args[])    {        // Creating object of class 2        // inside main() method        Derived d = new Derived();    }}
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 methodabstract class Base {     // Demo method    void fun()    {        // Print message if class 1 function is called        System.out.println(            "Function of Base class is called");    }} // Class 2class Derived extends Base {} // Class 3class 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();    }}
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 classabstract class Base {     final void fun()    {        System.out.println("Base fun() called");    }} // Class 2class Derived extends Base {} // Class 3// Main classclass 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();    }}
Base 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 classabstract 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 classabstract class Helper {     // Abstract method    static void demofun()    {         // Print statement        System.out.println("Geeks for Geeks");    }} // Class 2// Main class extending Helper classpublic class GFG extends Helper {     // Main driver method    public static void main(String[] args)    {         // Calling method inside main()        // as defined in above class        Helper.demofun();    }}
Geeks for Geeks
YouTubeGeeksforGeeks501K subscribersAbstract Classes (Java Programming Language) | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 13:23•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=DWmpxZ59JW0" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Must Read:
Difference between Abstract class and Interface in Java 
Difference between Abstract class and Abstract Methods
Constructors in Java Abstract Class
 Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
gauravmoney26
rocknaga81
solankimayank
sagartomar9927
kashishsoda
imkunal0306
paritoshchaudhari
Java-Abstract Class and Interface
java-basics
Java-Object Oriented
Java
School Programming
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Split() String method in Java with examples
Arrays.sort() in Java with examples
Reverse a string in Java
Initialize an ArrayList in Java
How to iterate any Map in Java
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects