Method Override sample in Java

By: Ganesh Iyer Viewed: 153231 times  Printer Friendly Format    


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

  }
}


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. The code sample in this tutorial is good. It would
View Tutorial          By: Anon at 2009-06-05 03:36:59

2. Excelent sample, I'm disagree with Anon, the sampl
View Tutorial          By: Gerardo Saldívar at 2009-09-21 15:25:01

3. i totally disagree with Gerardo Saldív
View Tutorial          By: Shivesh at 2009-09-24 23:46:42

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

5. i disagree with shivesh,
as everyone is dis

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

6. the example program is very simple to understand a
View Tutorial          By: Zubilu at 2010-10-06 00:19:13

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

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

9. THIS IS VERY SIMPLE AND EXCELENT PROGRAM.THIS PROG
View Tutorial          By: CHIRANJEET at 2011-02-05 12:13:54

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

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

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

13. U should give the output also.. then only it will
View Tutorial          By: Mohamed Younus at 2011-07-07 10:13:54

14. if u need simple definition then that are

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

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

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

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

18. In the above program, the instance variables like
View Tutorial          By: Abhikumar.M at 2012-08-02 17:25:55

19. i have a problem ie., Animal is the super class an
View Tutorial          By: renuka at 2012-08-29 15:08:33

20. A lucid explanantion is "when a method name i
View Tutorial          By: harshnil at 2013-01-15 03:57:44