Day32: Method Overriding

 Method Overriding

Whenever a child class has method with SAME PROTOTYPE as parent class METHOD, then we say that the child class has overridden the parent class method.

class A
{
public void show()
{
.............
.............
.............
}
}

class B extends A
{
public void show()
{
.................
}
}

Prototype: Access modifier, return type, Argument list, Name
  • Both the classes access modifier is same.
  • Both the classes have return type is Void.
  • Both the classes show name is show().
  • Both the classes show is non parameterized.
It means Class B 'show()' overide the Class A 'show()'.

Why do we perform method overriding?

Sometimes it happens that the functionality (logic ) of  a method defined in Parent class may not be suitable for the child class.
For Example : The logic of sound() method in the class CAT is not suitable for the class TIGER because Tiger makes a different sound.
In this case the programmer of TIGER class must compulsory override the method sound() inherited from CAT class.
If he doesn't oveeride the sound() method the code will generate wrong outpt.

Thus we can say that if we want to keep the declaration of the method same but we want to change the body of the inherited method of parent class then we will have to override the method in the child class.

class Cat
{
public void sound()
{
System.out.println("meow...");
}
}

class Tiger extends Cat
{
public void sound2()
{
System.out.println("roar...");
}
}

//Driver class
class UseCat
{
public static void main(String [ ] args)
{
Tiger sheru = new Tiger();
sheru.sound();
}
}

Output: meow...
But here you want sound of tiger then you go with overriding of sound.

class Papa
{
public void property()
{
}
public void marry()
{
System.out.println("Married father's friends daughter");
}
}
class Son extends Papa
{
public void marry()
{
System.out.println("Married Gf");
}
}

Output : Married Gf


Difference between Method Overloading and Method Overriding

Overriding
=========
The prototype of the method defined in CHILD CLASS must be exactly same as the prototype of method inherited from PARENT CLASS.

Requires Inheritance, without inheritance we can never override a parent class method in a child class. It means we can never perform OVERRIDING in a single class.



Overloading
============
The argument of the method in child class must be different than the argument of the method inherited from parent class.

Can be done without inheritance or with inheritance also. This is in a single class also we can preform overloading in case of Inheritance.


What role does "Super" play in method overriding?

If the child class programmer has overriden a method of parent class, but now he also wants the parent class method to execute, than from the child class method, we will have to call the parent class method using the keyword "super". 

class Papa
{
public void property()
{
SOP("house+cash");
}
}

class Son extends Papa
{
public void property()
{
SOP("sal");
super.property(); //This line will call property() method of class "Papa".
}
}

//Driver Class
Son ajay = new Son();
ajay.property();

Exercise : Calcuate Area of Cylinder by using Circle Class

//Circle.java
class Circle
{
private int radius;
public Circle(int radius)
{
this.radius=radius;
}
public double getArea()
{
return Math.PI*Math.pow(radius,2);
}
public int getRadius()
{
return radius;
}
}
//Cylinder.java
class  Cylinder extends Circle
{
private int height;
public Cylinder(int radius, int height)
{
super(radius);
this.height=heigth;
}
public double getArea()
{
return 2*super.getArea() + 2*Math.PI*getRadius()*height;
}
public double getVolume()
{
return super.getArea()*height;
}
}
//UseCylinder.java // Driver Class

class UseCylinder
{
public static void main(String [] args)
{
Cylinder obj = new Cylinder(10,20);
System.out.println("Cylinder Area: "+obj.getArea());
System.out.println("Volume : "+obj.getVolume());

}
}

Relationship Between Parent Class Reference and Child Class Object

  1. In Java there is a special rule regarding Reference of parent class and Object of child class.
  2. The Rule is that of Parent class is allowed to point to the object of its child class.
  3. Means, if Emp is parent class and Manager is its child class then the following code is perfectly valid: Emp e = new Manager();
  4. This is because when Manager will inherit Emp class, then all the properties of Emp will pass on to the Manager object also. Thus we can very well say that Every Manager is also an Employee.
  5. But its vice-versa is not true. That is Reference of Child Class can Never point to the object of its Parent Class.
  6. This is because no property of child is passed on to parent and logically it will be invalid to say that Ever Emp is Also a Manager. So the following code will not even compile: Manager m = new Emp();
  7. Further there is one more point to remember when a PARENT CLASS reference is Pointing to the child class object.
  8. The point to remember is that the parent class reference while pointing to a child class object can only call those methods which have been inherited from the parent class those method which have been added extra by the child class. For Example : If Emp class has a method called setSal() and Manager class has  a method called setBonus(), then the following code is perfectly valid:
            Emp e = new Manager(); 
            e.setSal(50000);
            But the Following code will not even complete:
            e.setBonus(25000);

//File name : Emp.java

class Emp
{

}

File Name : Manager.Java

class Manager extends Emp
{

}

File Name : UseManager.java

class UseManager
{
public static void main(String[] args) {
Emp e = new Manager(); //OK
Manager m =new Emp(); //Error
e.setBonus(25000); //Error

// Fruit---->Apple
// Fruit f = new Apple(); //OK
// Apple a = new Fruit(); //Error

// Vehicle --> Car --> SportCar
// Vehicle v = new Car(); //OK
// Vehicle v = new SportsCar(); //OK
// Car c = new SportsCar(); //OK

// SportsCar s = new Car(); //Error
// Car c = new Vehicle(); //Error
// SportsCar s = new Vehicle(); //Error

// Object obj = new SportsCar(); //OK
}
}

Comments

Popular posts from this blog

Day2 : Compiling Java Program with Examples

Day1: Introduction to Java and its feature

What does System.out.printf( "%-15s%03d\n", s1, x) do?