Last Updated: 1 April, 2023
A class that is declared with the abstract keyword is known as an abstract class in Java. An abstract class cannot be instantiated, which means we cannot create the object of an abstract class.
An abstract class may or may not contain abstract methods (methods that do not have a body, for example, public void display();) but if a class has at least one abstract method, then the class must be declared an abstract class.
To use an abstract class, we have to inherit it from another class and provide implementations for the abstract methods in it. It's mandatory to provide implementations for all the abstract methods in it. If any abstract method is not implemented, then that sub-class should be declared as ‘abstract’.
Syntax of Abstract Class
A method that is declared with an abstract keyword is known as an abstract method in Java. An abstract method doesn't have its body in the abstract class; we provide implementations of the abstract method in the subclass only.
An abstract method is just a prototype for the method with the following attributes:
Example: An abstract method declared inside an abstract class
abstract class Order {
abstract String getOrderSummary(int orderId) throws OrderNotFoundException;
}
An abstract method is always declared inside the abstract class; if a normal class contains an abstract method, then that class must be declared as an abstract class, otherwise the compiler will throw an exception at compile time.
Consider the following example:
class Order {
abstract String getOrderSummary(int orderId) throws OrderNotFoundException;
}
Here, getOrderSummary() is an abstract method that is declared inside a non-abstract class that is an invalid implementation in Java.
Example: An abstract class with abstract and non-abstract methods
Output
Mobile Common Features
RealMe7 Mobile - Base Features
RealMe7 Mobile - New Features
Mobile Common Features
RealMe8 Mobile - Base Features
RealMe8 Mobile - New Features
Abstract Class | Concrete Class |
---|---|
An abstract class created with abstract keyword.For example:
abstract class Bank {
|
A Concrete class created without abstract keyword. It is a normal Java class. For example:
class Bank {
|
We cannot create objects of the Abstract class. | Yes, we can create objects of the concrete class. |
An abstract class may have abstract methods and non-abstract methods. | A concrete class can have only one non-abstract method (normal method). |
An abstract class cannot be declared final. | A concrete class can be declared final. |
We need to inherit an abstract class to use it. If an abstract class is inherited by another class, and this class does not have any static properties, then there is no use for that abstract class. | We can create the object of the concrete class and use the members of the class. |
Here, we are creating an object of child class 'B' and assigning the reference in the abstract class reference object.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Why can an abstract class have a constructor in Java?
When a class extends an abstract class, the constructor of the subclass will either implicitly or explicitly invoke the constructor of the superclass.This chaining of constructors is one of the reasons abstract classes can have constructors in Java. An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.
Why can't an abstract class be instantiated in Java?
An abstract class contains incomplete methods; it is not possible to estimate the total memory required to create the object. So, JVM cannot create objects for an abstract class.
Can we declare an abstract method as static?
No
Can we declare an abstract method as private?
No, because an abstract method has no method body, it must be implemented in the child class. If we declare the abstract method as private, then it cannot be implemented outside the class.
Can we declare an abstract class as final?
No, It makes no sense to declare an abstract class as final; you will get a compile-time error because the final class cannot be extended by the child class, and the abstract class is only used by the child class to use and implement its abstract method.