Abstract Class in Java

Abstract Class in Java

4 mins read1.3K Views Comment
Updated on Aug 27, 2024 10:45 IST

The tutorial article below covers data abstraction using abstract class in Java with examples and explanations. 

2022_06_abstract.jpg

Contents

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

– / –
350 hours
Free
6 months
– / –
4 months
– / –
– / –
– / –
1 month
50 K
7 weeks
7.8 K
3 months
8.47 K
2 months
7 K
6 months
4 K
2 months

Introduction

Data Abstraction is one of the fundamental pillars of OOPs concepts in Java. It is achieved by the use of Abstract Classes and Interfaces in Java Programming. FYI, data abstraction is a property that allows showing up only essential and relevant information by hiding unnecessary details. Like, we can use the search engines (Google, Bing, Firefox, Yahoo, etc.) without knowing gore details about working protocols and stuff.

Let’s learn how can we attain data abstraction using abstract class in Java.

What is Abstract Class in Java?

An abstract class is a class which may or may not contain some abstract methods. Abstract classes can also have concrete methods (non-abstract methods) along with some abstract ones in them. It can’t be instantiated. That means you can not create an object for an abstract class for direct access. Moreover, an abstract class can have constructors, static and final methods as well.

Read More: Final Keyword in Java

What does an abstract class do? A Java Abstract class provides a common method implementation to all its extended subclasses. It basically works as a stage to support its subclasses without coming directly into the spotlight.

To declare an abstract class or an abstract method, all we need to do is precede them with the keyword abstract.  

Explore: Difference between Abstract Class and Interface in Java

What is an Abstract Method?

An abstract method is a method which does not have any definition. It is the responsibility of the subclass methods to give a concrete implementation of such methods. In doing so, each subclass is free to give its own implementation of the abstract method.

Also read: Method Overriding in Java

Note: If a class contains abstract methods, then the class should also be declared as abstract. Else it will not get compiled by JVM.

Now, let’s hop to the implementation part.

Compare best Courses on Java Programming from Coursera, Udemy, and Udacity.

Implementation of Abstract Class and Abstract Method in Java

The below example shows the implementation of Abstract Class and Method in Java. It also shows the access to abstract superclass constructors from their subclasses.

Example:


 
abstract class Figure //Abstract Superclass
{
double dimension1, dimension2;
Figure(double d1, double d2) //Constructor of Class Figure
{
dimension1 = d1;
dimension2 = d2;
}
abstract void calculateArea(); //Abstract Method
}
class Rectangle extends Figure //Subclass 1
{
Rectangle(double l, double b) //Constructor of Subclass Rectangle
{
super(l,b); //super() to call superclass Figure constructor
}
void calculateArea() //Defining abstract class into Subclass Rectangle
{
System.out.println("The area of the Rectangle is: " +(dimension1*dimension2));
}
}
class Triangle extends Figure //Subclass 2
{
Triangle(double base, double height) //Constructor of class Triangle
{
super(base, height); //super() to call superclass Figure constructor
}
void calculateArea() //Defining abstract class into Subclass Triangle
{
double area = 0.5*dimension1*dimension2;
System.out.println("The area of the Triangle is: " +area);
}
}
class AbstractMethodDemo //Main Class
{
public static void main(String[] args)
{
Figure f; //Reference Variable of Figure class
f = new Rectangle(3,4); //Constructor call to subclass Rectangle
f.calculateArea(); //calls calculateArea() in Rectangle
f = new Triangle(4,5); //Constructor call to subclass Triangle
f.calculateArea(); //calls calculateArea() in Triangle
}
}
Copy code

Also Read: Variables in Java

Explanation:

example

As you can see from the above diagram, the abstract class: Figure inherits subclass Rectangle and Triangle. Also, the abstract method in the abstract class Figure got redefined by its subclasses as per their uses.

Read about: Super Keyword in Java

Moreover, observing the above example, the class Figure was not instantiated. Because abstract classes may contain abstract methods and call directly, such methods are of no use as they don’t contain any method definition. Therefore, the Java Compiler restricts us from instantiating an abstract class. 

Also, Read: Data Types in Java – Primitive and Non-Primitive Data Types Explained

Output:

output

Read: Implementing Array in Java

Point to Remember about Abstract Class and Abstract Method

Following are points to keep in mind before using Abstract Class and Abstract Methods in Java Programs:

  • Abstract classes can have concrete methods in them as well.
  • A class containing an abstract method is indeed abstract. However, an abstract class may or may not contain an abstract method.
  • Only reference variable for an abstract class can be created, not object.
  • A concrete (non-abstract) subclass must override all abstract methods in the abstract superclass.
  • If a subclass does not override an abstract method defined in its superclass, then the compiler reports an error. To resolve such a case, one should either implement the abstract the class or should define the subclass as abstract as well.
  • There are no Abstract Constructors in Java.
  • We cannot have abstract static methods or classes as the purpose of abstract classes is to get inherited and redefined. But as we learned, static keyword in java doesn’t allow overriding. Although, we can have concrete static methods in an abstract class.
  • Abstract class in Java can implement interfaces without even providing the implementation of interface methods.

Read: Exception Handling in Java.

Conclusion

Hope the above article helped you learn something new about using abstract classes in Java. If you have any queries feel free to reach out to us on the link below. For more such content on Java, stay tuned to Shiksha Online. 

Read: Conditional Statements in Java

FAQs

What is Abstraction in Java?

Abstraction in Java is a property that allows showing up only essential and relevant information by hiding unnecessary details. Like, we can use search engines (Google, Bing, Firefox, Yahoo, etc.) without knowing the details about working protocols.

What is an Abstract Class in Java?

An abstract class is a class that may or may not contain some abstract methods. It cannot be instantiated. A Java Abstract class provides a common method implementation to all its extended subclasses.

What is an Abstract Method in Java?

It is a method that does not have any definition. It is the responsibility of the subclass methods to give a concrete implementation of such methods. In doing so, each subclass is free to give its own implementation of the abstract method.

Why there are no abstract constructors?

In such as case, the abstract constructor will never get invoked as the subclasses cannot inherit constructors. That is why we don't have abstract constructors in Java.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio