Programs

Abstract Class in Java – With Examples

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”. 

Popular Courses & Articles on Software Engineering

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 

ROI: 8.5 

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. 

Can we have an abstract class without any abstract method?

Yes. It is possible to have an abstract class without any abstract method since they are both independent techniques. Declaring an abstract class implies that it cannot be instantiated on its own and can only be done with the help of a subclass. Defining an abstract method means the method is defined or contained in the subclass.

Why is data abstraction essential?

Yes. It is possible to have an abstract class without any abstract method since they are both independent techniques. Declaring an abstract class implies that it cannot be instantiated on its own and can only be done with the help of a subclass. Defining an abstract method means the method is defined or contained in the subclass.

How is data abstraction different from control abstraction?

Data abstraction is the process of creating complex and complicated data types but highlighting or revealing only essential details. Control abstraction is the process that deals with the software part of abstraction. It is the process where the program is broken down or simplified, and unwanted details of the program executions are concealed. Control abstraction is primarily used to generate new functionalities and blend various control statements into a single entity.

Want to share this article?

Upskill Yourself & Get Ready for The Future

Leave a comment

Your email address will not be published. Required fields are marked *

Our Best Software Development Course

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×