View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Abstract Class in Java – With Examples

By Rohan Vats

Updated on Feb 17, 2025 | 5 min read | 5.3k views

Share:

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. 

What is an Abstract Class in Java?

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.

Rules of Abstract Class

  • An abstract class can only be declared within an abstract keyword.
  • It can contain both abstract and non-abstract methods.
  • It can also have final methods.
  • It can never be instantiated.
  • Constructors and static methods can also be used.

Example of an Abstract Class

abstract class B{}

An Abstract Method in Java

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 

Example of Java Abstract class with an Abstract Method

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. 

Example of Java Abstract Class with 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”. 

When should an Abstract Class be used in Java?

Below are a few scenarios where an abstract class can be used:

  • When attempting to introduce the concept of inheritance in code (to share code among various relevant classes) by using common methods of a base class.
  • When the requirements are specific and the implementation details are incomplete. 
  • When classes used for extending an abstract class have multiple common fields or methods requiring non-public modifiers. 
  • When non-final or non-static methods are used to modify the object state. 

When to use an Interface in Java for Abstraction?

The following scenarios require a Java interface to achieve abstraction:

  • To support functionalities of multiple inheritances
  • To achieve loose coupling

Declaring an Interface

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. 

Example of Java 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 

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months
View Program

Job-Linked Program

Bootcamp36 Weeks
View Program

Advantages of Abstraction in Java

  • Loose coupling: Abstraction in Java helps in loose coupling by mitigating the dependent entities at an exponential level. 
  • Reusable codes: Abstract class in Java is a great time saver. The abstract method can be called only when necessary. Abstract class avoids rewriting the same codes, thereby saving time.
  • Data protection: Data abstraction in Java helps conceal the sensitive code details from the users. It helps in hiding important project features and displaying only essential components. 
  • Faster problem solving: With the help of dynamic method resolution, various issues and complications can be resolved by engaging just one abstract method. 

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. 

Frequently Asked Questions (FAQs)

1. Can we have an abstract class without any abstract method?

2. Why is data abstraction essential?

3. How is data abstraction different from control abstraction?

Rohan Vats

408 articles published

Get Free Consultation

+91

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

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

View Program
upGrad

Microsoft | upGrad KnowledgeHut

Microsoft Azure Data Engineering Certification

Access Digital Learning Library

Certification

45 Hrs Live Expert-Led Training

View Program
upGrad

upGrad KnowledgeHut

Professional Certificate Program in UI/UX Design & Design Thinking

#1 Course for UI/UX Designers

Bootcamp

3 Months

View Program