Programming Tutorials

Method Override sample in Java

By: Ganesh Iyer in Java Tutorials on 2009-06-05  

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);

 }
}





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)