Method Override sample in Java
 


A method is said to be overridden when one is in parent class and another is in child class with the same name, same return type, same parameter.

class Animal
 {
  int height=10;
  int weight=20;

 void talk()
  {
    System.out.println(\"Animal talking\");
  }

  void food()
  {
    System.out.println(\"Animal Eating\");

  }
 }

class Cat extends Animal                   
 {                                         
   void talk()                             
   {                                       
     System.out.println(\"meo... meo\");   
   }                                        

   void food()
   {
     System.out.println(\"Drink MILK\");
   }

 }


class functionOverride
{

  public static void main(String[] args)
  {
   Animal a = new Animal();
   Cat c = new Cat();

   /* c.height=20 */;

   c.talk();
   c.food();

   System.out.println(\"Height \"+c.height);

  }
}
 
 
 
 
 
COPYRIGHT © 2017 java-samples.com. ALL RIGHTS RESERVED.