Home Answers Viewqa Java-Beginners Overriding methods

 
 


Naulej kumar
Overriding methods
1 Answer(s)      10 months ago
Posted in : Java Beginners

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 Pages:
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
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
constructor overriding - Java Beginners
constructor overriding  Write a program to demonstrate the overriding of constructor methods
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   
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     
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
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
( 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
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?   hii Overloading: Overloading is nothing... but parameters are different. Overriding: Overriding is nothing but giving
( 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
Methods - OOP
Java NotesMethods - OOP Static methods If your method doesn't use... utility methods and main(...), you should probably not be using static methods. A good example of a static methods in Java is the Math or Character
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
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
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
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
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
SCJP Module-4 Question-2
, method overriding is used. 2. Methods can't have the same name. 3. Methods...(int x, float y) { } } Is the following code correct ,with same name methods.... Answer : (1) Explanation : In this code , method overriding is used. In method
Methods - Vocabulary
/class methods, instance methods, overriding, overloading, ... However, each... other methods can call this method. keywordaccess noneIf you don't... package to call the method. Some common methods that are declared
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
Methods of HttpServlet
Methods of HttpServlet  What are methods of HttpServlet
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
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
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
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
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
Final Methods - Java Tutorials
methods The final method  can be declare as follows: public final String
methods type - Java Beginners
methods type in Java  Give me an example programs of methods types in Java
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.