Wednesday, April 6, 2022

Java Abstract Class and Abstract Method

An abstract class in Java is a class that is declared using the abstract keyword. An abstract class may contain methods without any implementation, called abstract methods along with methods with implementations.

The declaration of an abstract method starts with the abstract keyword and ends with a semicolon, it does not have a method body.

General form of abstract method in Java

abstract type method_Name(parameter_list);

If a class contains an abstract method, either declared or inherited from another class, it must be declared as an abstract class.

Note that a class with all its methods implemented can also be declared as abstract. But declaring the class as abstract is mandatory if there is any abstract method in a class.


Usage of Abstract class in Java

There are scenarios when you would want to define a generalized structure using a super class without providing a complete implementation of every method. In that case sub classes have the responsibility to provide implementation for the methods.

In such scenario where base class just provides the general template and the sub-classes provide the specific implementation, base class can be defined as an abstract class with implementation for methods that are common for the sub-classes. Methods for which implementation varies are marked as abstract methods in the base class thus leaving it to the sub-classes to provide implementation for such methods.

Restrictions with Abstract Classes in Java

  • Any class that contains one or more abstract methods must also be declared abstract.
  • Abstract class can not be directly instantiated so there can't be any object of an abstract class.
  • Any class that extends an abstract class must provide implementation of all the abstract methods in the super class; otherwise the sub-class must be declared abstract itself.
  • There can't be an abstract constructor or abstract static method.

Java Abstract class Example

Let's say we have an abstract class Shape which defines the general structure for the 2D figures and declares area method which is abstract. That would mean any sub-class extending the super class Shape has to provide an implementation of meaningful area method.

abstract class Shape{
 double length;
 double breadth;
 //Constructor
 Shape(double length, double breadth){
  this.length = length;
  this.breadth = breadth;
 }
 // common method
 public String getFigureType(){
  return "2D figure";
 }
 // Abstract method
 abstract void area();
}

class Triangle extends Shape{
 //constructor to initialize length
 Triangle(double i, double j){
  super(i, j); // calling the super class constructor  
 }
 // Abstract method implementation for Triangle class
 void area(){
  System.out.println("In area method of Triangle");
  System.out.println("Area of Triangle - " + (length * breadth)/2);
 }
}

class Rectangle extends Shape{
 //constructor to initialize length
 Rectangle(double i, double j){
  super(i, j); 
 }
 // Abstract method implementation for Rectangle class
 void area(){
  System.out.println("In area method of Rectangle");
  System.out.println("Area of Rectangle - " + length * breadth);
 }
}

In the above program you can see that the abstract class Shape provides a generalized structure for the 2D figures. There are two fields with in the Shape class representing 2D figure. These two fields can be used to initialize length and breadth of the figure (in case of Rectangle) or the base and height of the figure (in case of Triangle). Since the abstract class Shape is for 2D figure so method getFigureType() will always return same value "2D figure" for any subclass so it is placed with in the abstract class to be used by all the sub classes.

How area is calculated depends on the figure that is why it is declared as abstract in the Shape class so that the class extending the Shape class can provide the appropriate implementation of the area method.

Java Abstract class and run time polymorphism

As we already know there can't be an object of an abstract class, but abstract class can be used to create object references.

We have also seen that abstract class is deigned to be used as a super class with generalized structure. Since run time polymorphism in Java is implemented through the use of super class reference thus it is obvious that abstract class reference (super class reference) could be used to refer to subclass object.

Run time polymorphism with Abstract class Example

If we take the same Shape class as used above
abstract class Shape{
 double length;
 double breadth;
 //Constructor
 Shape(double length, double breadth){
  this.length = length;
  this.breadth = breadth;
 }
 // Abstract method
 abstract void area();
 
}

class Triangle extends Shape{
 //constructor to initialize length
 Triangle(double i, double j){
  super(i, j); // calling the super class constructor  
 }
 // Abstract method implementation for Triangle class
 void area(){
  System.out.println("In area method of Triangle");
  System.out.println("Area of Triangle - " + (length * breadth)/2);
 }
}

class Rectangle extends Shape{
 //constructor to initialize length
 Rectangle(double i, double j){
  super(i, j); 
 }
 // Abstract method implementation for Rectangle class
 void area(){
  System.out.println("In area method of Rectangle");
  System.out.println("Area of Rectangle - " + length * breadth);;
 }
}

public class PolymorphicTest {
 public static void main(String[] args){
  // Abstract class reference
  Shape shape;
  Triangle triangle = new Triangle(5, 6); 
  Rectangle rectangle = new Rectangle(7, 5); 
  // shape dynamically bound to the Triangle object
  shape = triangle;
  // area method of the triangle called
  shape.area(); 
   // shape dynamically bound to the Rectangle object
  shape = rectangle;
  // area method of the rectangle called 
  shape.area(); 
 }
}

Output

In area method of Triangle
Area of Triangle - 15.0
In area method of Rectangle
Area of Rectangle - 35.0

It can be seen that a reference of an abstract class is created

Shape shape;

That shape reference refers to the object of Triangle class at run time first and then object of Rectangle class and appropriate area method is called.

Abstract class in Java with interfaces

If a class implements an interface but does not implement all the methods of that interface then that class must be declared as abstract.

public interface MyInterface {
 void method1();
 String method2(String Id);
}
abstract class in Java
Error if all methods of interface are not implemented

But we can declare the class as abstract in that case

public abstract class AbstractClassDemo implements MyInterface {
 public static void main(String[] args) {
  System.out.println();
 }
}

Points to note-

  • There can be a class declared as abstract that provides implementation of all the methods in a class i.e. no abstract method in the class but vice versa is not true, if there is any abstract method in a class then the class must be declared abstract.
  • Abstract class in Java may have some methods with implementation and some methods as abstract.
  • Abstract classes can not be instantiated to create an object.
  • An object reference of an abstract class can be created.
  • If a class implements an interface and does not provide implementation for all the interface methods, it must be declared abstract.

That's all for this topic Java Abstract Class and Abstract Method. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. Difference Between Abstract Class And Interface in Java
  2. Inheritance in Java
  3. Polymorphism in Java
  4. final Keyword in Java With Examples
  5. Core Java Basics Interview Questions And Answers

You may also like-

  1. Marker interface in Java
  2. Type Casting in Java With Conversion Examples
  3. static block in Java
  4. Creating Custom Exception Class in Java
  5. Multi-Catch Statement in Java Exception Handling
  6. How and Why to Synchronize ArrayList in Java
  7. Java CyclicBarrier With Examples
  8. Deadlock in Java Multi-Threading

No comments:

Post a Comment