Sei sulla pagina 1di 11

LECTURE-10

RUNTIME
POLYMORPHISM, DYNAMIC
BINDING, OVERRIDING
Swapnali Kurhade
Sardar Patel Institute of Technology
Method Overriding
 Declaring a method in subclass which is already
present in parent class is known as method
overriding.
Advantage
 The class can give its own specific implementation to
a inherited method without even modifying the
parent class(base class).
 Method overriding is used for runtime polymorphism
Example…
class Human{
public void walk() { System.out.println("Human walks"); }
}
class Boy extends Human{
public void walk() { System.out.println("Boy walks"); }

public static void main( String args[]) {


Boy obj = new Boy();
obj.walk();
}
}
Output:
Boy walks
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).
 private, static and final methods cannot be
overridden as they are local to the class
Static Binding
 The binding which can be resolved at compile time by
compiler is known as static or early binding.
 Example:-

class Human{ .... }


class Boy extends Human{
public void walk(){ System.out.println("Boy walks"); }
public static void main( String args[]) {
Boy obj1 = new Boy();
obj1.walk();
}}
Dynamic Binding
 When compiler is not able to resolve the
call/binding at compile time, such binding is known
as Dynamic or late Binding.
 Binding of overridden methods happen at runtime
which is known as Dynamic Binding
 Thus while calling the overridden method, the
compiler gets confused between parent and child
class method (since both the methods have same
name).
Static Binding vs Dynamic Binding
 Static binding happens at compile-time while
dynamic binding happens at runtime.
 Binding of private, static and final methods always
happen at compile time since these methods cannot
be overridden. Binding of overridden methods
happen at runtime.
 Java uses static binding for overloaded methods
and dynamic binding for overridden methods.
Difference between method
overloading and method overriding
Method Overloading Method Overriding

1) Method overloading is used to increase Method overriding is used to provide the


the readability of the program. specific implementation of the method that is
already provided by its super class.

2) Method overloading is performed within Method overriding occurs in two classes that
class. have IS-A (inheritance) relationship.

3) In case of method overloading, In case of method overriding, parameter must


parameter must be different. be same.

4) Method overloading is the example of Method overriding is the example of run time
compile time polymorphism. polymorphism.

5) In java, method overloading can't be Return type must be same in method


performed by changing return type of the overriding.
method only. Return type can be same or
different in method overloading. But you
must have to change the parameter.
Method Overriding and Dynamic
Method Dispatch
class ABC{ public static void main( String args[])
//Overridden method {
public void disp() /* When Parent class reference
refers to the parent class object then
{ System.out.println("disp() method in this case overridden method (the
of parent class"); method of parent class) is called. */
} ABC obj = new ABC();
} obj.disp();
class Demo extends ABC{ /* When parent class reference
//Overriding method refers to the child class object * then
public void disp() the overriding method (method of
{ System.out.println("disp() method child class) is called. * This is called
of Child class"); dynamic method dispatch and
runtime polymorphism */
}
ABC obj2 = new Demo();
public void newMethod(){
System.out.println("new method of obj2.disp(); } }
child class"); } disp() method of parent class
disp() method of Child class

Potrebbero piacerti anche