Wednesday, 29 April 2015

abstract class in Java

An abstract class is a class that is declared using the abstract keyword. An abstract class may contain methods without any implementation, called abstract methods.
The declaration of an abstract method starts with the abstract keyword and ends with a semicolon, it does not have a method body.

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.

Usage of Abstract class

There are situations when one wants 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. Thus, super class just provides the structure of a given abstraction that must be extended and implemented by the corresponding sub-classes.

Example of an Abstract class

Let's say we have an abstract class shape which defines the general structure for the 2D figures and declare the area method in the super class as abstract. That would mean that any sub-class extending the super class 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;
 }
 // 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 square - " + (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);;
 }
}

Restrictions with Abstract Classes

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

Abstract class and run time polymorphism

As we already know that there can't be an object of an abstract class, but abstract class can be used to create object references. As run time polymorphism in Java is implemented through the use of super class reference and abstract class is deigned to be used as a super class with generalized structure. Thus, it is imperative that abstract class reference could be created which can be used to refer to subclass object.

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 square - " + (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 square - 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 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

Compiler error that class must implement methods declared in Myinterface interface.

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 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 Abstract class in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Polymorphism in Java
  2. Inheritance in Java
  3. Class in Java
  4. final in Java
  5. interface in Java
  6. Core Java basics interview questions

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment