Method Override sample in Java

By: Ganesh Iyer  

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

  }
}



Archived Comments

1. A lucid explanantion is "when a method name in its super class is similar to the name of method
View Tutorial          By: harshnil at 2013-01-15 03:57:44

2. i have a problem ie., Animal is the super class and Dog is the sub class, is it possible to create o
View Tutorial          By: renuka at 2012-08-29 15:08:33

3. In the above program, the instance variables like weight is declare but not use in the program and
View Tutorial          By: Abhikumar.M at 2012-08-02 17:25:55

4. nice
View Tutorial          By: narendra at 2012-04-23 09:40:00

5. Simple and easy code..........
View Tutorial          By: mandalson at 2011-12-24 06:50:38

6. Simple and easy code..........
View Tutorial          By: mandalson at 2011-12-24 04:08:12

7. if u need simple definition then that are

Overriding Function:
Function havi

View Tutorial          By: rizvaankhan at 2011-10-10 13:13:36

8. U should give the output also.. then only it will be more good and understandable :>!!!!!
View Tutorial          By: Mohamed Younus at 2011-07-07 10:13:54

9. all are easy to understanding codes. . . thanks
View Tutorial          By: kadhir at 2011-06-27 03:33:30

10. nice
View Tutorial          By: aish at 2011-05-06 07:30:09

11. thanx for providing good knowledge @ overriding
View Tutorial          By: Amol at 2011-05-05 02:28:43

12. THIS IS VERY SIMPLE AND EXCELENT PROGRAM.THIS PROGRAM HELPFUL FOR UNDERSTAND METHOD OVERRIDING
View Tutorial          By: CHIRANJEET at 2011-02-05 12:13:54

13. thanks..nice program to understand overriding concept
View Tutorial          By: dinesh at 2011-01-19 04:48:18

14. this is very good example.
View Tutorial          By: jyoti raman ray at 2010-10-13 22:17:09

15. the example program is very simple to understand and it is very helpful to understand.
thank

View Tutorial          By: Zubilu at 2010-10-06 00:19:13

16. i disagree with shivesh,
as everyone is disagreeing from above member

View Tutorial          By: arpit at 2010-09-21 14:23:09

17. it is bad
View Tutorial          By: sunil at 2010-02-16 00:38:34

18. i totally disagree with Gerardo Saldí[email protected] if it is easy then it should be there at first
View Tutorial          By: Shivesh at 2009-09-24 23:46:42

19. Excelent sample, I'm disagree with Anon, the sample y clear and esay to undestand. Not is necesary c
View Tutorial          By: Gerardo Saldívar at 2009-09-21 15:25:01

20. The code sample in this tutorial is good. It would have been better if you add more description and
View Tutorial          By: Anon at 2009-06-05 03:36:59


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial