What is Abstract Class in java?
In a java which class is declared with Abstract Keyword called a “Java Abstract Class“. An abstract class can have Abstract methods and no abstract method or both. This class can’t be instantiated, it should be extended and its method implemented.

A point of Java abstract class
Abstraction is one of the major features of OOP concept. Abstraction is a process of hiding the implementation details and handles complexity from the user, only main and usable functionality provided to the user.
For Example, A car only has the main option give to the user to control it. Like Break, Gear, Steering, accelerator etc. Not all details how is internally working.
Let’s see the Points to Remember:-
- Abstract classes must be declared with Abstract Keyword.
- Java Abstract class can’t be instantiated.
- To use an abstract class in java, need to inherit it from another class and implement the abstract methods in it.
- A inherit an abstract class, need to provide implementations to all the abstract methods in it.
- Abstract classes can contain abstract methods or not (normal methods).
- If calss have any Abstract method (Without body) should be declared with Abstract Keyword.
- Can have constructors, static methods and final methods.
Syntax
A simple syntax how to declare abstract class.
1 |
abstract class ClassName{ } |
And abstract method syntax is:-
1 |
abstract void MethodName(); |
Java abstract class example Code
Let’s see an example, where an abstract class name “ObjectSahpe” and have one abstract method (without body method) with the name “draw()”
A shape class is to have the main method to access this program. It’s a very simple and basic example of abstraction in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package a.b; abstract class ObjectShapes { abstract void draw(); } class Rectangle extends ObjectShapes{ @Override void draw() { System.out.println("Draw Rectangle"); } } class Shape{ public static void main(String args[]){ ObjectShapes rect1=new Rectangle(); rect1.draw(); } } |
Output: Draw Rectangle
Abstract Methods
An abstract method has no body, only a name of the method and “
1 |
public abstract void sleep(); |
This method must be overridden to use.
Note: You can’t have an abstract method in a non-abstract class. And you have to implement all abstract methods.
Java Abstract class constructor | Data member | More methods
Abstract class can have:
- Abstract method
- Non-abstract method (method body)
- Constructor
- Data member
- Java main() method.
Let’s see the example of class cantering data members and methods.
Animal.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
abstract class Animal { // declare data member String objectName = " "; //constructor Animal(String name) { objectName = name; System.out.print(name + "Animal created "); } //constructor Animal() { System.out.println("Animal created "); } // normal method public void sleep() { System.out.println("Animal sleeping time"); } // normal method public void eat() { System.out.println("Animal eat something"); } // Abstract method public abstract void makeNoise(); } class Dog extends Animal { Dog (String name){ super(name); } public void makeNoise() { System.out.println("Bark! Bark!"); } } class Cow extends Animal { Cow (String name){ super(name); } public void makeNoise() { System.out.println("Moo! Moo!"); } } |
AbstractionEx.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class AbstractionEx { public static void main(String args[]){ //cow object Cow cow = new Cow("Cow "); cow.makeNoise(); cow.sleep(); cow.eat(); //Dog object Dog dog = new Dog("Dog "); dog.makeNoise(); dog.sleep(); dog.eat(); } } |
Output:

How to Java abstract class implements interface?
Answer: A class can only inherit from one class but can implement multiple interfaces in Java.
An abstract class is very similar to an interface. The main difference is that an abstract class can define some function already, an interface can’t (note that this changed in Java9+).
Now coming to answer you have to use an “implement” keyword to implements interface.
interface: – Walk
1 2 3 4 5 |
package abs.intr; interface Walk { String walk(); } |
Abstract class:- Animal implemented Walk interface
1 2 3 4 5 |
package abs.intr; abstract class Animal implements Walk { public abstract String MakeNoise(); } |
Main class for execution of code:- with Concrete Cat and dog methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package abs.intr; public class MainClass { public static void main(String args[]){ Animal catObj = new Cat(); System.out.println(catObj.MakeNoise()); Animal dogObj = new Dog(); System.out.println(dogObj.MakeNoise()); } } class Cat extends Animal { public String MakeNoise() { return "Meow"; } @Override public String walk() { return "cat is walking"; } } class Dog extends Animal { @Override public String walk() { return "Dog is walking"; } @Override public String MakeNoise() { return "bark"; } } |
Output: Meow
bark
Note: Interface methods are all public by default, even if public
public
Q: What is the purpose of the Abstract class in Java?
Answer: Mainly Abstract class is used for achieving an Abstraction. This classes are used to provide common functionality across a set of related classes while also allowing default method implementations.
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Abstract class in Java Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.