Abstract Class in Java

Class that contains at least one Abstract method is referred to as Abstract class.

Now, What is Abstract Method ? , When a Parent is quite sure that the implementation of the method will be provided by the child and implementation of the method by each of the child should be unique.

abstract class Plane{
     abstract void takeOff(); // this is abstract method which does not have definition
     abstract void fly();
     abstract void land();
}

class PassengerPlane extends Plane{
     void takeOff(){
         System.out.println("plane is about takeoff");
     }
     void fly(){
         System.out.println("plane is flying");
     }
     void land(){
         System.out.println("plane landed");
     }
}

class PlaneDemo{
     public static void main(String args[]){
     //rest of the class statements 
     }
}

Here, In the plane class there are three methods, takeOff(), fly(), land() that are declared in the plane class but its definition is not given,  so we predicted that the definition will be provided by the child class. So, as per the rule we can make the plane class as abstract.

Now, in PassengerPlane class all method’s definitions are given.

 

Important thing to remember:

  1. If a class contains only abstract methods then it referred to as a pure abstract class.
  2. If a class contains both abstract and normal method then it is referred as an impure abstract class.

If a child class can not implement an abstract method of parent class then the method must be declared as abstract in the child class and a child class must be declared as abstract.

abstract class Plane{
 abstract void takeOff(); // this is abstract method which does not have definition
 abstract void fly();
 abstract void land();
}

abstract class PassengerPlane extends Plane{
     abstract void takeOff(); //if definition is not given then it should be abstract
     void fly(){
         System.out.println("plane is flying");
     }
     void land(){
         System.out.println("plane landed");
     }
}
// rest of the program

Note: An object of an abstract class cannot be created.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: