Abstract Class in Java – With Examples
Updated on Feb 17, 2025 | 5 min read | 5.3k views
Share:
For working professionals
For fresh graduates
More
Updated on Feb 17, 2025 | 5 min read | 5.3k views
Share:
Table of Contents
Data abstraction is the technique of concealing certain information and displaying only required details to the users. This data abstraction can be achieved in two ways, abstract class and interfaces.
An abstract class in Java is a restricted class encased or declared with the abstract keyword. An abstract class can have both abstract and non abstract methods. It cannot be used for creating an object. To access an abstract class, it must be inherited from another class.
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
Example of an Abstract Class
abstract class B{}
This method can be used only in an abstract class and does not contain a body. The body will be provided by the inherited subclass.
An example of an abstract method is:
abstract void printStatus(); //no method body and abstract
In the following example, the car is an abstract class with one abstract method, run. The implementation is provided by the class, Audi.
abstract class Car{
abstract void run();
}
class Audi4 extends Bike{
void run(){System.out.println(“running fine”);}
public static void main(String args[]){
Car obj = new Audi4();
obj.run();
}
}
The output of the program will be “running fine”.
In most cases, the implementation class remains unknown (data abstraction), and an object of the implementation class is obtained through the factory method.
A method that returns the class instance is called the factory method.
In the following example, creating the instance of a square class, the draw() method of the Square class gets initiated.
abstract class Shape{
abstract void draw();
}
//In real scenario, implementation is provided by others i.e., unknown by end user
class Square extends Shape{
void draw(){System.out.println(“drawing Square”);}
}
class Circle1 extends Shape{
void draw(){System.out.println(“drawing hexagon”);}
}
//In real scenario, method is called by programmer or user
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();//In a real scenario, object is provided through method, e.g., getShape() method
s.draw();
}
}
The output of the above program will be “drawing hexagon”.
Below are a few scenarios where an abstract class can be used:
The following scenarios require a Java interface to achieve abstraction:
The interface should be declared only by interface keyword. This results in total abstraction so that all the inheritance methods are declared in an empty body, and all the fields become public, static, and final as default. The class implementing the interface should implement all the methods that are declared in the interface.
In the below example, the printable interface has only one method whose implementation is provided in class A6.
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println(“Welcome”);}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
The output of the program will be
Welcome.
Example 2:
interface Bank{
float rateOfInterest();
}
class BOI implements Bank{
public float rateOfInterest(){return 8.5f;}
}
class CUB implements Bank{
public float rateOfInterest(){return 9.2f;}
}
class TestInterface2{
public static void main(String[] args){
Bank b=new BOI();
System.out.println(“ROI: “+b.rateOfInterest());
}}
The output of the above program will be
To learn more about data abstraction in Java and software development, enroll in the Executive Post Graduate Program in Software Development offered by IIIT Bangalore in association with upGrad.
Executive Postgraduate program in Software Development is a 13-month online program. The curriculum is specially designed for working professionals to focus on learning without giving up on their jobs. The curriculum includes industry-based projects and case studies. In addition, the program has over 10 live sessions from the industry experts to help the candidates stay updated with the current trends in the industry.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources