Overriding methods

Overriding methods

How to use overriding method in the java progrem?

View Answers

June 29, 2012 at 1:06 PM

The overriding method are define in the subclass that has same name,same argument and same return type as a method in the superclass.

class Animal {
    public void eat(String str) {
        System.out.println("Eating for grass");

    }
}

class Cat extends Animal {
    public void eat(String str) {
        System.out.println("Drinking for milk");

    }
}

class Dog extends Animal {
    public void eat(String str) {
        System.out.println("Eating for bread");

    }
}

class Goat extends Animal {
    public void eat(String str) {
        System.out.println("");

    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Animal();
        Cat b = new Cat();
        Dog c = new Dog();
        Goat d = new Goat();
        a.eat("grass");
        b.eat("milk");
        c.eat("bread");
        d.eat("grass");
    }
}

Output:

Eating for grass
Drinking for milk
Eating for bread

Description:- The above code demonstrates you the overriding method.we have created a class named Animal, Animal are the supper class .we have seen that a method defined in the supper class is inherited by its subclass and is used by the objects created bye the subclass.now define a method in the subclass that has the same name, same arguments and same return type as a method in the superclass.

Ads









Related Tutorials/Questions & Answers:
Overriding methods
Overriding methods  How to use overriding method in the java progrem?   The overriding method are define in the subclass that has same name...:- The above code demonstrates you the overriding method.we have created a class
Why doesn't Java allow overriding of static methods?
Why doesn't Java allow overriding of static methods?  Why doesn't Java allow overriding of static methods
Advertisements
overriding
overriding  how to write overriding program in java   Please go through the following link: Java Method Overriding
methods
methods  PrintStream class has two formatting methods,what
methods
methods  PrintStream class has two formatting methods,what
methods
methods  PrintStream class has two formatting methods,what
Overriding equals and hashCode in Java
Overriding equals and hashCode in Java  Overriding equals and hashCode in Java
Method overriding
Method overriding  can a method declared in one package be over ridden in a different package?   A subclass in a different package can only override the non-final methods declared public or protected
examples for overloading and overriding
examples for overloading and overriding  examples for overloading and overriding
overloading and overriding in c
overloading and overriding in c  can anyone explain the concept of overloading and overriding in reference to c programming...through an example if possible
example of function overriding in c++
example of function overriding in c++  require an example of function overriding in c
method overriding in java
method overriding in java  program to compute area of square,rectangle,triangle,circle and volume of sphere,cylinder and perimeter of a cube using method overriding
method overriding in java
method overriding in java  program to compute area of square,rectangle,triangle,circle and volume of sphere,cylinder and perimeter of a cube using method overriding
Method Overriding
Method Overriding       Overriding is another useful feature of object-oriented programming technique... by a subclass. The method overriding is used to invoking the parent class method
Java method Overriding
. In overriding methods of both subclass and superclass possess same signatures. Overriding is used in modifying  the methods of the super class... Java method Overriding     
Method Overriding in Java
Method Overriding in Java means a Subclass uses extends keyword to override a super class method. In Overriding both subclass and superclass must have same parameters. Method Overriding in Java is used so that a subclass can implement
Polymorphism : Method Overriding
In this tutorial you will learn another concept of polymorphism that is method overriding
What is use of method overloading and overriding?
What is use of method overloading and overriding?  Method overriding... in advance   Overriding is useful when you want to change the functionality of some method based on a class. For example, by overriding the toString
Overriding jaxb binding at client side?
Overriding jaxb binding at client side?  How can override jaxb binding at client side? I have a webservice client which creates xmlgregoriancalendar.... Overriding jaxb binding at client side
static methods
static methods  why static methods cannot read or write the instance variables
native methods
native methods  what is native methods in java?   A native method is a method that is implemented in a language other than Java. The Java native method is a great way to gain and merge the power of C or C++ programming
overriding in derived class from abstract class
overriding in derived class from abstract class  why should override base class method in derived class? Instead of override abstract class method, we can make method implementation in derived class itself know
Various methods of httpservletresponse interface
Various methods of httpservletresponse interface  What are the various methods of httpservletresponse interface
Method overriding
Tell me where we are using overloading and overriding in realtime project?
Tell me where we are using overloading and overriding in realtime project?  Hi,Tell me where we are using overloading and overriding in realtime project? please give me answer
ModuleNotFoundError: No module named 'methods'
ModuleNotFoundError: No module named 'methods'  Hi, My Python... 'methods' How to remove the ModuleNotFoundError: No module named 'methods... to install padas library. You can install methods python with following command
GET and POST methods
GET and POST methods   What are the differences between GET and POST methods in form submitting, give the case where we can use GET and we can use POST methods
factory methods in java?
factory methods in java?  what are factory methods in java?   Hi Friend, Factory methods are static methods that return an instance of the native class like Pattern.compile(), Calendar.getInstance
Final Methods - Java Tutorials
methods The final method  can be declare as follows: public final String
Servlet Methods
Servlet Methods In this section we will read about the various methods... methods that are used to initialize a Servlet, handles the request received..., the Servlet methods are called life-cycle methods of Servlet. Following
abstract methods in java
abstract methods in java  what is abstract methods in java.give better examples for understanding   Hi Friend, Please visit the following link: http://www.roseindia.net/java/master-java/abstract-class.shtml Thanks

Ads