Modal Box

Method Overriding in java

In java if subclass has the same name function as its super class, then it is known as function overriding.
So in method overloading, there are multiple functions with the same name within a single class, while overriding is the same function name in super class and its sub class.
Method over riding is the example of run time polymorphism.
class A
 {
  public void display()
   {
    System.out.println("Super class display function");
   }
 }
class B extends A
{
  public void display()
   {
     System.out.println("Sub class display function");
   }
}
class CheckMain { public static void main(String args[]) { A obj= new A (); obj.display();

B obj1= new B(); obj1.display();
}
 }
As b class object can also call the super class display function,
but here, the sub class function overrides the super class function.
To avoid the effect of overriding, super keyword is used which is discussed in the next chapter.

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial