Sunday, February 28, 2016

Java - Inheritance and Method Overriding, simple example



Inheritance means one class deriving attributes and methods from another class.
The class that inherits is called a subclass and the class from which it inherits
is called as superclass.
In other words, if two classes have common attributes and methods between them.
Those common elements need not be coded in each of the classes but can be shared.
In Java by using the 'extends' keyword while defining the subclass,
those common elements can be extended from the superclass to the subclass

Let us take an easy example.
We have cellphones of different types
Basic cellphone - one can make only calls
Cellphone with camera - one can make calls and also take pictures.
Cellphone with Flash Camera - one can make calls and take pictures in day and night.

But, all the three types have one common element which is making calls.
And the last two cell phones have common elements Camera
but with different functionalities.
We will see, how to implement inheritance and also method overriding.

Program : 

/* CellPhone class has two methods
   'Call' method to print the type of the phone
   'price' method which takes price as argument and prints it*/

 class CellPhone {
  int price;
    void call(){
        System.out.println( "With this cell you can make calls");
    }
   void price(int p){
      System.out.println( "Price : "+ p);
     
   }
}

/* CameraCellPhone as subclass inherits 'call' and 'price' method  from the 
   superclass CellPhone.
  Method 'camera' defined in this class prints "take pictures in day time only".
 */

class CameraCellPhone extends CellPhone{
       void camera(){
       System.out.println("take pictures in day time only");
   }
}

/* FlashCameraCellPhone inherits 'call', 'price' and 'camera' method 
   from superclass  CameraCellPhone.
   But, if you notice, 'camera' method prints 'take pictures only in day time',
   Which would be wrong for Flash Camera class.
   so, using the same name Camera, we define a method which prints
   " take pictures in day and night".
   This is called method overriding.
   That is 'camera' method in the subclass overrides the 'camera' method in the superclass
*/
   class FlashCameraCellPhone extends CameraCellPhone{
     
       void Camera(){
           System.out.println ("take pictures in day and night");
        
       }
   }

/* Main Program */

public class Inheritance {

        public static void main(String[] args) {
      
        CellPhone cp = new CellPhone();
        CameraCellPhone ccp = new CameraCellPhone();
        FlashCameraCellPhone fccp = new FlashCameraCellPhone();

        /* out put from Cell Phone object */

        System.out.println("[1] Basic Cell Phone");
        cp.call();
        cp.price(100); // calling price method with 100 as argument

        /* out put from CameraCellPhone Object */

        System.out.println("[2] Camera Cell Phone");
        ccp.call();
        ccp.camera();
        ccp.price(500); 

        /* out put from FlashCameraCellPhone object */

        System.out.println("[3] Flash Camera Cell Phone ");
        fccp.call();
        fccp.Camera();  // Java executes the subclass method (fccp.Camera()),
                                  // since it is available and overrides the superclass
                                 // (ccp.camera()) method
        fccp.price(700); 
          
       
    }
     
}

Output :
run:
[1] Basic Cell Phone
With this cell you can make calls
Price : 100
[2] Camera Cell Phone
With this cell you can make calls
and take pictures in day time only
Price : 500
[3] Flash Camera Cell Phone
With this cell you can make calls
and take pictures in day and night
Price : 700



Basic Overriding Rule :  Name,order of arguments, return type of both
                                           overridden and overriding method must be the same

No comments:

Post a Comment