Inheritance java Example

Ads
 

Inheritance java Example

How can we use inheritance in java program?

View Answers

June 13, 2012 at 5:12 PM

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 example demonstrates you the concept of inheritance. Inheritance is a way of establishing a subtype from an existing object. In other words, it is the property of accessing properties, methods of super class by a sub class. With the use of extends keyword, the subclasses will be able to inherit all the properties of the superclass except for the private properties of the superclass. In the given example, the class Animal is a superclass, whose method eat() has been accessed by its sub classes Cat, Dog and Goat. The objects of the subclasses is then created in the main class to test. Through the objectsof these class, their respective methods has been called.

Ads









Related Tutorials/Questions & Answers:
Inheritance java Example
Inheritance java Example  How can we use inheritance in java program... for bread Description:- The above example demonstrates you the concept of inheritance. Inheritance is a way of establishing a subtype from an existing object
Inheritance Example In Java
Inheritance Example In Java In this section we will read about the Inheritance using a simple example. Inheritance is an OOPs feature that allows to inherit... the inheritance feature in Java programming. This example will demonstrate you
Advertisements
Inheritance in Java with example
Here we are going to discuss Inheritance in Java with a simple example... for inheritance in Java. Inheritance is used in java for the reusability of code... of java.lang.Object.java. Java supports three types of inheritance, they are: Simple
Inheritance,Inheritance in Java,Java Inheritance
the following example. The concept of inheritance is used to make the things from... kinds of inheritance are there in java.   Simple Inheritance  .... Java does not support multiple inheritance but the multiple inheritance can
Java inheritance
Java inheritance   can the child access protected content of the parent if the child is in a different project   If you have considered different project as different package then the child cannot access the content
Inheritance program in java
. Example of Single Inheritance in Java: class A { int x; int y; int get(int p...In this tutorial we will discuss examples of Simple Inheritance in Java and Multilevel Inheritance in Java. Inheritance is an OOPS (Object Oriented
Does Java support multiple Inheritance?
example. Does Java support multiple Inheritance? Thanks   Hi, Good... tutorial: Inheritance in Java with example Thanks...Does Java support multiple Inheritance?  Hi, I am beginner in Java
Does Java support multiple Inheritance?
example. Does Java support multiple Inheritance? Thanks   Hi, Good... tutorial: Inheritance in Java with example Thanks...Does Java support multiple Inheritance?  Hi, I am beginner in Java
Does Java support multiple Inheritance?
example. Does Java support multiple Inheritance? Thanks   Hi, Good... tutorial: Inheritance in Java with example Thanks...Does Java support multiple Inheritance?  Hi, I am beginner in Java
java inheritance
java inheritance  problem with following code.... class Employee { int empno; String name; String edesig; Employee() { empno=0; } Employee(int eno,String ename,String ed) { empno=eno; name=ename
java inheritance
java inheritance  ACME Bike shop has created a new scheme to reward employees for selling bicycles. Sales are only made by either, a sales person...% and to the sales agents at a rate of 10%. Write a Java application
Java code to implement MULTIPATH INHERITANCE
Java code to implement MULTIPATH INHERITANCE  HI, I am new to java programming. I want to implement multipath inheritance in one java program. For example, Student
Java code to implement MULTIPATH INHERITANCE
Java code to implement MULTIPATH INHERITANCE  Hi, I am new to java .so please write code for this inheritance example... Student | ----------- | | Test Sports
Concept of Inheritance in Java
Concept of Inheritance in Java Concept of Inheritance in Java is considered... class from parent class. Let's understand the concept of inheritance in Java... that is used for reusability purpose. There are three types of inheritance in java
how can i achieve multiple inheritance in java without using inheritance ?
how can i achieve multiple inheritance in java without using inheritance ?  how can i achieve multiple inheritance in java without using inheritance
Java Inheritance Concept.
Java Inheritance Concept.  Yesterday i got confused with following question in an aptitude exam. Question:Class A,B,C have method named doit(). Class B extends Class A,Class C extends Class B.How will you call method doit
doubt in inheritance program - Java Beginners
doubt in inheritance program  how will we get the result 6 2 5 in the inheritance program in the given example i got 6 &2 but i am confused about 5
Java : Multilevel Inheritance
Java : Multilevel Inheritance This section contains concept of multilevel Inheritance in java. Multilevel Inheritance : In multilevel inheritance more than... way to implement multiple inheritance. Example : class A { void show
Multilevel Inheritance in java
Multilevel Inheritance in java We are going to discuss about multilevel inheritance in java. The Multilevel inheritance is inherit of classes more than two.... Java, every class is a subclass of  java.lang.Object. Example of Multilevel
inheritance
inheritance  how does one implement inheritance in java
Does Java support multiple inheritance?
Does Java support multiple inheritance?  Hi, Does Java support multiple inheritance? thanks   hi, Please visit the following link: http://www.roseindia.net/java/language/inheritance.shtml Hope
Inheritance in Java
. Example of Inheritance in Java: <html>     <head>         <... Inheritance in Java enhances the properties of the class, which means... of inheritance in Java: Simple Inheritance Multilevel Inheritance
inheritance
inheritance  hi.. pleaseeeeee i need a quick help(answer) in creating aprogrm in java by using inheritance for car in such away that .... Car is parent class and both of Private and Public Car extends it then both of Bus
inheritance
inheritance  Hi, I have a class mall,how can i use this in case of inheritance.Tell me with example
Inheritance in Java 7
This tutorial describe concept of Inheritance. It is one of OOPs concept
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java
java persistence example
java persistence example  java persistence example
Inheritance in Spring
Inheritance in Spring       Inheritance Demo, In the example given below we are going to tell about the inheritance in the Spring framework. By inheritance we mean a way
Inheritance in Spring
Inheritance in Spring       Inheritance Demo, In the example given below we are going to tell about the inheritance in the Spring framework. By inheritance we mean
Java Client Application example
Java Client Application example  Java Client Application example

Ads