Run time polymorphism
(Method Overriding) (Late data binding)
It is a
technique of defining a function more than one time without changing its
signature (having same name and same parameter list).
Note: --
Method overriding cannot be achieved within single class function of base class
is overrided in its derived class.
Method
overriding is also called run time polymorphism or late data binding because
the which function should be called by which statement is decided at time of execution.
SEE this
program (What do you think is it method overriding?)à
import java.util.*;
class base
{
void
show()
{
System.out.println("this
is base class");
}
}
class derived1 extends base
{
void
show()
{
System.out.println("this
is derived1 class");
}
}
class derived2 extends base
{
void
show()
{
System.out.println("this
is derived2 class");
}
}
public class abc
{
public
static void main(String as[])
{
base
b;
b=new
base(); //instantiating with base class
cnstructor
b.show();
b=new
derived1(); //instantiating withderived1
class cnstructor
b.show();
b=new
derived2(); //instantiating with derived2
class cnstructor
b.show();
}
}
Output:--
this is base class
this is derived1 class
this is derived2 class
yesssssssssssss
this is method overriding
Here class
derived1 and derived2 both class is getting inherited by class base hence
method show() is also inherited in both classes and both derived1 and derived2
both class has its own show().
What is
happening here is derived classes doesn’t have their own show() method but
actually they are overriding the show() which comes from base class.
More in
overriding (Abstract class) click HERE
http://shvicyberteam.blogspot.com/p/abstract-class-in-ava.html
No comments:
Post a Comment