Abstract class in Java is a class that is declared using abstract
keyword. Abstract class can have
regular methods (methods with method body) as well as abstract methods
(methods without method body
and having abstract specifier).
General form of abstract class in Java
abstract class className{ .. .. }
When is abstract class needed
When designing your classes for an application you may have a situation where a super class can provide a general structure and common implementation for few methods while leaving implementation of methods to the sub classes where implementation differs.
The methods that have to be implemented by the sub classes are specified as abstract methods in the super class. A sub class has to provide implementation of the methods that are declared as abstract in the parent class.
General form of Java abstract method is as follows-
abstract type methodName(method_arguments);
There is no method body in the abstract methods, it is the responsibility of the sub classes to override abstract methods and provide implementation.
Abstract class in Java example
Suppose you have to design payment functionality where you have to provide functionality for cash and credit card payment and you need to capture customer details like name, address.
Here capturing customer details is common but the mode of payment is different. In this scenario you can create
an abstract class with abstract doPayment()
method which has to be implemented by the sub classes and
keep the common code in abstract class to be used by all the sub classes.
public abstract class Payment { private String customerName; private String customerAddress; public Payment() { } public Payment(String customerName, String customerAddress){ this.customerName = customerName; this.customerAddress = customerAddress; } public void printCustDetails() { System.out.println("Name- " + customerName + " Address- " + customerAddress); } // abstract method abstract void doPayment(double amount); } // childclass-1 class CashPayment extends Payment { CashPayment(String customerName, String customerAddress){ super(customerName, customerAddress); } @Override public void doPayment(double amount) { System.out.println("In child class CashPayment, payment of - " + amount); printCustDetails(); } } //childclass-2 class CCPayment extends Payment { CCPayment(String customerName, String customerAddress){ super(customerName, customerAddress); } @Override public void doPayment(double amount) { System.out.println("In child class CCPayment, payment of - " + amount); printCustDetails(); } }Class used to run the code-
public class MainClass { public static void main(String[] args) { // super class reference Payment payment; // holding CashPayment instance payment = new CashPayment("Amy", "5th ave, NYC"); payment.doPayment(56.25); // holding CCPayment instance payment = new CCPayment("Parker", "Burnsville, NC"); payment.doPayment(67.89); } }Output
In child class CashPayment, payment of - 56.25 Name- Amy Address- 5th ave, NYC In child class CCPayment, payment of - 67.89 Name- Parker Address- Burnsville, NC
In the example there is an abstract class Payment which has one concrete method printCustDetails()
for the common implementation and an abstract method doPayment()
. Abstract method is implemented by
both the child classes CashPayment
and CCPayment
as per their requirement. Both of
these classes also call the common method to print the customer details.
Constraints when using Abstract class in Java
- Abstract class can’t be instantiated. In the above example where Payment is an abstract class, trying to instantiate it-
Payment payment = new Payment();
results in a compile time error “Cannot instantiate the type Payment”. - Though Abstract class can’t be instantiated but an object reference of abstract class can be created.
This object reference can hold the child class objects. That’s how abstract class in Java helps in achieving
run time polymorphism.
In the above example you can see that the reference of the abstract class Payment is created-
Payment payment;
which refers to the child class objects and call the appropriate doPayment() method. - If there is any abstract method in a class then a class has to be declared abstract otherwise you will get compile time error “The type must be an abstract class to define abstract methods”.
- A sub class extending an abstract class must implement all the abstract methods of the super class if it is not then the class should also be declared as an abstract class.
For example-
abstract class ParentClass { abstract void display(); abstract void add(); } class ChildClass extends ParentClass { public void add() { System.out.println("In add method"); } }
Here subclass is not implementing one of the abstract method display() that will result in compile time error “The type ChildClass must implement the inherited abstract method ParentClass.display()”.
If display() method is not implemented by the ChildClass then it should also be declared abstract.
abstract class ChildClass extends ParentClass { public void add() { System.out.println("In add method"); } }
- A constructor in a class can’t be declared as abstract. Abstract is an illegal modifier for a constructor. Any static method also can’t be abstract.
- A class can either be declared abstract or final not both.
Abstract class with no abstract method
You can create a Java abstract class with no abstract method in it i.e. all the methods are concrete and implemented there itself. That’s one way to create a class that can’t be instantiated only inherited.
abstract class ParentClass { public void display() { System.out.println("In parent class method"); } } class ChildClass extends ParentClass { public void add() { System.out.println("In add method"); } } public class MainClass { public static void main(String[] args) { ChildClass obj = new ChildClass(); obj.display(); obj.add(); } }Output
In parent class method In add method
Abstract Classes Compared to Interfaces
Abstract classes in Java are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation.
However, with abstract classes, you can declare fields that are not static and final. With interfaces, all fields are automatically public, static, and final.
In abstract classes you can define public, protected, and private concrete methods. In interfaces all methods that you declare or define (as default methods) are public.
If a class implementing an interface does not implement all the methods of that interface then that class must be declared as abstract.
public interface Payment { void doPayment(double amount); void add(int a, int b); } abstract class TestClass implements Payment{ @Override public void doPayment(double amount) { System.out.println("In payment method"); } }
Since TestClass is not implementing both of the methods of the interface it should be declared as an abstract class otherwise you will get an error “The type TestClass must implement the inherited abstract method Payment.add(int, int)”.
That's all for the topic Abstract Class in Java. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Interface in Java With Examples
- Interface Vs Abstract Class in Java
- Java CopyOnWriteArraySet With Examples
- Java HashMap With Examples
- ArrayBlockingQueue in Java With Examples
- Matrix Multiplication Java Program
- Java Program to Swap Two Numbers Without Using Third Variable
- Spring Java-based Configuration Example
No comments:
Post a Comment