When a method in a sub class has the same name and type as a method in its super class,then the method in the subclass is said to be override in the super class.When an overridden method is called from within a subclass,then it will always refer to the version of that methoddefined by the subclass.The version of the method defined by the super class will be hidden.
class A
{
void show( )
{
System.out.println( "\nsuper class !");
}
}
class B extends A
{
void show( )
{
System.out.println( "base class!" );
}
}
class Overriding( )
{
public static void main(String args[ ] )
{
B obj = new B( );
obj.show( );
super.show( );
}
}
OUTPUT
base class!
super class
here the versions of show( ) inside B overrides version declared in A.To access the super class version of overridden function it can do so by using super keyword.In the programe it is illustrated as
super.show( );
and it will call the super class function.
class A
{
void show( )
{
System.out.println( "\nsuper class !");
}
}
class B extends A
{
void show( )
{
System.out.println( "base class!" );
}
}
class Overriding( )
{
public static void main(String args[ ] )
{
B obj = new B( );
obj.show( );
super.show( );
}
}
OUTPUT
base class!
super class
here the versions of show( ) inside B overrides version declared in A.To access the super class version of overridden function it can do so by using super keyword.In the programe it is illustrated as
super.show( );
and it will call the super class function.
No comments:
Post a Comment