Home           J2EE Concepts           JMS           Java Language           Contact           Back           Next           Bookmark this Page          










Some useful tips related to:
 - health & fitness
 - vehicle care
 - cooking
 - misc household tasks

Method Overriding


In Java, when a superclass & subclass method have same signature, then the method in the subclass will override the method in its parent class. When such overridden method is called from a subclass, Java will execute the method defined by the subclass. The version of the method defined by the superclass will be shadowed.

Consider following example:

class Human{
  String name;
  public String getName(){
      return name;
  }
  public void disp(){
     System.out.println("Human");
  }
 }

class Student extends Human{
  int rollNo;
  getRollNo{
    return rollNo;
 }
  public void disp(){
     System.out.println("Student");
 }
}

class demo{
  public static void main(){
  Human h = new Human();
  Student s = new Student();
  h.disp();// This would print "Human"
  s.disp();// This would print "Student"
 }
}