Method Override sample in Java

By: Ganesh Iyer Emailed: 1768 times Printed: 2516 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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

  }
}

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(17)


1. View Comment

The code sample in this tutorial is good. It would have been better if you add more description and explanation of the code.

View Tutorial          By: Anon at 2009-06-05 03:36:59
2. View Comment

Excelent sample, I'm disagree with Anon, the sample y clear and esay to undestand. Not is necesary comment nothing. Greetings.

View Tutorial          By: Gerardo Saldívar at 2009-09-21 15:25:01
3. View Comment

i totally disagree with Gerardo Saldívar@ if it is easy then it should be there at first place.Good programming is never complete without proper code comments.

View Tutorial          By: Shivesh at 2009-09-24 23:46:42
4. View Comment

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


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

this is very good example.

View Tutorial          By: jyoti raman ray at 2010-10-13 22:17:09
6. View Comment

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
7. View Comment

thanx for providing good knowledge @ overriding

View Tutorial          By: Amol at 2011-05-05 02:28:43
8. View Comment

nice

View Tutorial          By: aish at 2011-05-06 07:30:09
9. View Comment

all are easy to understanding codes. . . thanks

View Tutorial          By: kadhir at 2011-06-27 03:33:30
10. View Comment

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
11. View Comment

if u need simple definition then that are

Overriding Function:
Function having same name, data types in parameters but with different definition and contained in different class(child).

When overriding function is called in child class, it overrides that function from parent class.
withdraw function is present in parent class and child class. Overriding means, when withdraw function is called, the function present in Child class will be executed rather than function in parent class.

by rizvaankhan


View Tutorial          By: rizvaankhan at 2011-10-10 13:13:36
12. View Comment

Simple and easy code..........

View Tutorial          By: mandalson at 2011-12-24 04:08:12
13. View Comment

Simple and easy code..........

View Tutorial          By: mandalson at 2011-12-24 06:50:38
14. View Comment

nice

View Tutorial          By: narendra at 2012-04-23 09:40:00
15. View Comment

In the above program, the instance variables like weight is declare but not use in the program and it is a waste of memory and give more example for this

View Tutorial          By: Abhikumar.M at 2012-08-02 17:25:55
16. View Comment

i have a problem ie., Animal is the super class and Dog is the sub class, is it possible to create object in this manner, "Animal A=new Dog", if yes, how we can do this?

View Tutorial          By: renuka at 2012-08-29 15:08:33
17. View Comment

A lucid explanantion is "when a method name in its super class is similar to the name of method in its sub class then method over ridding occuers.""
and renuka i have the same doubt no comments from any 1 please help me out with same que of renuka..???????


View Tutorial          By: harshnil at 2013-01-15 03:57:44

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ganesh Iyer
Converting a number into its equalant value in words in Java
Method Overloading sample in Java
Method Override sample in Java
A Java program which takes contents of one file and sends the output to another file by adding line numbers to each line.
Taking the size of an Array at runtime & generate random numbers to populate the Array
How to reverse the elements in a Single dimension array and display the output
How to count the number of digits in any Input number other than negative numbers

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
InetAddress Example program in Java
Read from a COM port using Java program
Stack example in Java - push(), pop(), empty(), search()
How to use ArrayList in Java
Using StringTokenizer in Java
The Basic Structure of a Simple Java program
FileReader and FileWriter example program in Java
Transient vs Volatile modifiers in Java
Vector example in Java
Method Overriding in Java
Method Overloading (function overloading) in Java
instanceof sample program in Java
append() in Java
Student marks calculation program in Java
Reading from a file and writing to a file using Java program
Most Emailed Articles (in Java)
Text to Speech conversion program in Java
inheritance in Java
How to initialize an Array and how to copy the array
Using Charts in JasperReports
What is Unicode?
Operator Precedence in Java
Of Object, equals (), == and hashCode ()
DateFormat sample program in Java
A Simple whois program in Java
CharArrayReader example program in Java
ByteArrayOutputStream - sample program in Java
Vector example in Java
Arrays example in Java - asList(), binarySearch(), fill(), sort(), equals()
HashSet Sample program in Java
Disadvantages of using Native methods in Java