Monday, August 29, 2011

Overridden Example in java


/*If we call a method from subclass having same name,same return type and same parameter list as in super class, then the method in the sub class is invoked insted of super class. This is known as method overridden*/
//SuperClass.java
public class SuperClass
{
void Display()
{
System.out.println("I am in Super class");
}
}

//SubClass.java
public class SubClass extends SuparClass
{
void Display()
{
System.out.println("I am in subclass");
}
}

//MainClass.java
public class MainClass
{
public static void main(String args[])
{
SubClass sub = new SubClass();
sub.Display();
}
}

Output =>
I am in subclass
[NOTE: If we want that a method of super class has never been override by the subclass then we make the method of super class final with the help of final keyword.]

No comments:

Post a Comment