Method Overriding in Java

Method Overriding means when a method is exists in super class and same method name is exists in sub class or derived class . Here is an example

  • Without Method Overriding :

                   public class Demo
                    {
                          void display()
                           {
                              System.out.println("without method override  ");
                           }
                     }

                  public class DemoTest extends Demo
                   {
   
                      public static void main(String[] args)
                       {
                           DemoTest myTest = new DemoTest();
                           myTest.display();
  
                      }
                 }

               Output : without method override

  • Method Overriding :

                    public class MethodOverridingDemo
                       {
                             void display()
                             {
                                System.out.println("without method override  ");
                             }
                        }

                      public class DemoTest extends MethodOverridingDemo
                       {
                             void display()
                               {
                                    System.out.println(" method override success  ");
                                }
 
                                public static void main(String[] args)
                                  {
                                     DemoTest myObject = new DemoTest();
                                      myObject.display();
                                   }
                          }

                Output : method override success


Find us :
        Facebook : @apnaandroid
        Google+   : Apna Java
        YouTube : Android & Java Tutorial

Comments