Tuesday, 3 September 2019

Inheritance and Polymorphism


Acquiring methods and variables from parent class to child class is called as Inheritance.

Types of Inheritance:
  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchy Inheritance
  • Multiple Inheritance (cannot achieve using class)
  • Hybrid Inheritance (combination of Hierarchy Inheritance and Multiple Inheritance) 
Example:

class A /Parent class/Super class/base class
{
}


class B extends A /Child class/sub class/derived class
{
}

class C extends B  ->c contains both A and B
{
}

  

Method Overriding
Method overriding is Re-define the same method from parent class into child class. When method is present in parent class as well as in child class with the same name and same number of arguments ,is called method Overriding.

Example:

class A
{
void m1(int a,int b)
{
----
}
}


           
class B extends A
{
void m1(int a,int y) //Overridden Method
{
----
}
}
 

Method Overloading vs Method Overriding:

Overloading:                                 
No inheritance required.               
Definition should not be same.      

Overriding:
At least we should have 2 classes (1 parent,1 child).
Definition should be same,but body can be changed.

 Polymorphism

 Polymorphism are of two types

  • Static Polymorphism or Compile time polymorphism
  • Dynamic Polymorphism or Run time Polymorphism
 
Static Polymorphism or Compile time polymorphism:

When we create an object of a child class.we can access the overridden methods,parent class methods and child class methods during the compile time.

Dynamic Polymorphism or Run time Polymorphism:
Child class object can be referred by parent class reference variable 

Example:

Parent class:

public class Car {
   
    public void start() {
        System.out.println("Car---Start car");
    }
   
    public void stop() {
        System.out.println("Car---Stop car");
    }
   
    public void refuel() {
        System.out.println("Car---Refuel car");
    }
}

Inherited child class:

public class BMWCar extends Car {             //"has a relationship"
   
    // when method is present in parent class as well as in child class with the same name and same number of arguments ,is called method Overriding
   
    public void start() {                            //Overridden method
        System.out.println("BMW---Start BMW Car");
    }
   
    public void theftSafety() {
        System.out.println("BMW---Theft Safety");
    }

}

Main Method:
public static void main(String[] args) {
       
                                                                      //Static polymorphism--compile time polymorphism
        BMWCar bmw=new BMWCar();
        bmw.start();
        bmw.theftSafety();
        bmw.stop();
        bmw.refuel();
       
        Car car=new Car();
        car.start();
        car.stop();
        car.refuel();
       
                                                                         //Top-casting
//child class object can be referred by parent class reference variable---dynamic polymorphism--run time polymorphism
        Car car1=new BMWCar();
        car1.start();
        car1.stop();
        car1.refuel();
       
                                                                         //Down-casting
        BMWCar bmw1=(BMWCar)new Car();
        bmw1.theftSafety();  // throw ClassCastException
    }




No comments:

Post a Comment