PREVIOUS
Method overriding
*Already defined method in a super class is
redefined with all argumentin its sub class, then the redefined method
overrides the method in the superclass.
This mean the sub class method us active and the
super class method is hidden.
* If we want to call the method in the superclass.
The syntax
is :
super.name of superclass method();
Advantage 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 -
program :
class A
{
int a=20;
}
class B extends A
{
int a=10;
void dis()
{
System.out.println("val of fisrt class "
+super.a);
System.out.println("val of 2nd class
"+a);
}
}
class sup1
{
public static void main(String ar[])
{
B b=new B();
b.dis();
}
}
No comments:
Post a Comment