Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Overriding methods
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Naulej kumar
Overriding methods
1 Answer(s)      2 years and 5 months ago
Posted in : Java Beginners


How to use overriding method in the java progrem?


Advertisement
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
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
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
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     
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
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
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
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
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
Advertisements
 

 

 

DMCA.com