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.









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
Method Overriding in JRuby
overriding of methods between the classes. In earlier examples of JRuby you have studied a lot about to create class, inheriting class and calling methods... Method Overriding in JRuby   
constructor overriding - Java Beginners
constructor overriding  Write a program to demonstrate the overriding of constructor methods
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
Java overriding
Java overriding  What restrictions are placed on method overriding
overloading and overriding
overloading and overriding  hello, What is the difference between overloading and overriding?   hiiADS_TO_REPLACE_1 Overloading... name is same but parameters are different. Overriding: Overriding is nothing
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
example of function overriding in c++
example of function overriding in c++  require an example of function overriding in c
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
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
abstract class and overriding - Java Beginners
abstract class and overriding? Interface? Give some example program?   Hi... are derived to implement the methods inherited from the abstract class (base class... and implement to those methods that are used commonly by the subclasses with common
Overriding in java
Some key points about overriding in java The overriding method must have... of argument. The overriding method must have less restrictive access modifier. The return type of overriding method must be the same. If you want your
OVERLOADING AND OVERRIDING - Java Interview Questions
OVERLOADING AND OVERRIDING  When Use OverLoading?When Use OverRiding Can U Explian Brifely
Methods of HttpServlet
Methods of HttpServlet  What are methods of HttpServlet
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
( Inheritance,need help in Polymorphism, Overriding) - Java Beginners
( Inheritance,need help in Polymorphism, Overriding)  can any one...( Inheritance, Polymorphism, Overriding) Employees in a company are divided... Incremental point on annual salary scale The methods used in each class
static methods
static methods  why static methods cannot read or write the instance variables
Agile methods
Agile methods   Why use Agile methods?   This methods focus on shorter iterations, in which the software is brought to a releasable level of quality fairly often, usually somewhere between weekly and monthly. Short
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
methods in the applet - Applet
methods in the applet  import java.awt.*; import java.applet.Applet... provides the Applet class with default implementation for all the applet methods. You can implement these methods in the applet class when you want to override
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
validate() and reset() methods
validate() and reset() methods   Describe validate() and reset() methods
Various methods of httpservletresponse interface
Various methods of httpservletresponse interface  What are the various methods of httpservletresponse interface
( Inheritance, Polymorphism, Overriding)i need help - Java Beginners
( Inheritance, Polymorphism, Overriding)i need help  Advanced Concepts with Classes( Inheritance, Polymorphism, Overriding) i need help to create... The methods used in each class may be summarized as follows: Employee class
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
methods type - Java Beginners
methods type in Java  Give me an example programs of methods types in Java
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
Cookie methods in jsp
Cookie methods in jsp   Define cookie methods in jsp ?    Cookie methods : clone() getComment() getDomain() getMaxAge() getName() getPath() getSecure() getValue() getSecure() getVersion
java object class methods
java object class methods  What are the methods in Object class?  There are lots of methods in object class. the list of some methods are as- clone equals wait finalize getClass hashCode notify notifyAll
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
Java overloaded methods
Java overloaded methods  Can overloaded methods can also be overridden
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
_TO_REPLACE_1 final methods The final method  can be declare as follows: public
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
to create a java class and methods
it with methods that can be used to reverse a list & append two lists.Also to comment on whether the dsign ade has led to make methods for append
Methods in Jsp - Development process
Methods in Jsp   Hi, My interviewer said we should declare & define all methods inside _jspService() method only. is it correct . Thanks Prakash  Hi Friend, Yes, all the methods should be declared and defined

Ads