METHOD OVERRIDING
METHOD OVERRIDING
If subclass (child class) has the same method as
declared in the parent class, it is known as
method overriding in java.
Usage of Java Method Overriding:
Method overriding is used to provide specific
Implementation of a method that is already
provided by its super class.
Method overriding is used for runtime
polymorphism
Rules for Java Method Overriding
Method must have same name as in the parent
class.
Method must have same parameter as in the
parent class.
Must be IS-A relationship (inheritance).
class Vehicle
{
void run(){System.out.println("Vehicle is running");}
}
class Bike2 extends Vehicle
{
void run(){System.out.println("Bike is running safely");
}
public static void main(String args[])
{
Bike2 obj = new Bike2();
obj.run();
} Output: Bike is running safely
Real Example of Java Method
Overriding
Consider a scenario, Bank is a class that provides
functionality to get rate of interest. But, rate of interest
varies according to banks. For example, SBI, ICICI
and AXIS banks could provide 8%, 7% and 9% rate of
interest.
We cannot override static method:
because static method is bound with class
whereas instance method is bound with object.
Static belongs to class area and instance belongs
to heap area.
Difference Between Method Overriding
& Method Overloading
POLYMORPHISM
Polymorphism in java is a concept by which we
can perform a single action by different ways.
Polymorphism is derived from 2 greek words:
poly and morphs. The word "poly" means many
and "morphs" means forms. So polymorphism
means many forms.
TYPES OF POLYMORPHISM
There are two types of polymorphism in java:
1. Compile time polymorphism: Method
Overloading.
2. Runtime polymorphism: Method Overriding.
RunTime Polymorphism
Runtime polymorphism or Dynamic Method
Dispatch is a process in which a call to an
overridden method is resolved at runtime.
In this process, an overridden method is called
through the reference variable of a superclass.
Upcasting
When reference variable of Parent class refers to
the object of Child class, it is known as upcasting.
class A{ }
class B extends A{ }
A a=new B(); //upcasting
Example of Java Runtime Polymorphism
class Bike{
void run(){System.out.println("running");}
}
class Splender extends Bike{
void run(){System.out.println("running safely with 60km");}
public static void main(String args[]){
Bike b = new Splender();//upcasting
b.run();
}
} Output:running safely with 60km.
Java Runtime Polymorphism with Data Member
Method is overridden not the datamembers, so runtime
polymorphism can't be achieved by data members.
class Bike
{
int speedlimit=90;
}
class Honda3 extends Bike
{
int speedlimit=150;
public static void main(String args[])
{
Bike obj=new Honda3();
System.out.println(obj.speedlimit); //90
}
}
Upcasting is casting to a supertype, while
downcasting is casting to a subtype.
Upcasting is always allowed, but downcasting
involves a type check and can throw a
ClassCastException.
Upcasting: happens automatically, no need to do
anything explicitly.
Downcasting : is not directly possible in Java,
explicitly we have to do.
Downcasting with java instanceof operator
When Subclass type refers to the object of Parent
class, it is known as downcasting.
Ifwe perform it directly, compiler gives
Compilation error.
If you perform it by typecasting,
ClassCastException is thrown at runtime.
But if we use instanceof operator, downcasting
is possible.
class Animal{ }
class Dog1 extends Animal
{ //Dog inherits Animal
public static void main(String args[])
{
Dog1 d=new Dog1();
System.out.println(d instanceof Animal);//true
}
}
Dog d=new Animal();//Compilation error
If we perform downcasting by typecasting,
ClassCastException is thrown at runtime.
Dog d=(Dog)new Animal();
//Compiles successfully but ClassCastException
is thrown at runtime
Possibility of downcasting with instanceof
class Animal { }
class Dog3 extends Animal {
static void method(Animal a) {
if(a instanceof Dog3)
{
Dog3 d=(Dog3)a; //downcasting
System.out.println ("ok downcasting performed");
}
}
public static void main (String [] args) {
Animal a=new Dog3();
Dog3.method(a);
}
}
Downcasting without the use of java instanceof
class Animal { }
class Dog4 extends Animal {
static void method(Animal a) {
Dog4 d=(Dog4)a;//downcasting
System.out.println("ok downcasting performed");
}
public static void main (String [] args) {
Animal a=new Dog4();
Dog4.method(a);
}
}