Home Answers Viewqa Java-Beginners Inheritance java Example

 
 


Naulej kumar
Inheritance java Example
1 Answer(s)      11 months ago
Posted in : Java Beginners

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.









Related Pages:
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
Inheritance
the following example. The concept of inheritance is used to make the things from... kinds of inheritance are there in java.   Simple Inheritance  ... one base class into a single class is known as multiple inheritance. Java does
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
inheritance  how does one implement inheritance in java
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 in java - Java Beginners
Inheritance in java  I want a example program using the concept of (simple and multiple) inheritance
Inheritance in java - Java Beginners
Inheritance in java  I want a example program using the concept of (single and multiple) inheritance.  Hi Friend, Please visit the following link: http://www.roseindia.net/java/language/inheritance.shtml Thanks
inheritance
inheritance  Hi, I have a class mall,how can i use this in case of inheritance.Tell me with example
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
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
Inheritance in java with example
Inheritance in java with example          Inheritance is one of the concept of the Object- Oriented programming. The advantage of using the inheritance
Inheritance
Classes in Java      ... other features like creation and implementation of the object, Inheritance etc... with the example :  Here is the code of the program : 
Inheritance and Composition - Java Beginners
Inheritance and Composition  import java.util.*; public class... arrangement in the design of your solution. In an object-oriented design of a Java... object. For example, it might be useful if the coffee cup object of your
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
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 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
Inheritance - Java Beginners
friend this is an example...{ System.out.println("Using inheritence example!"); BufferedReader buff = new... information: http://www.roseindia.net/java/language/inheritance.shtml Thanks
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
inheritance code program - Java Beginners
QUADRANT IF ITS 1,2,3,4 QUADRANT. EXAMPLE OUTPUT: Using inheritance example...inheritance code program  class Point { private int x ; private... IOException{ System.out.println("Using inheritence example!"); BufferedReader
multiple inheritance.
multiple inheritance.  hello, can java support multiple inheritance???   hi, java does not support multiple inheritance
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
Multiple inheritance using interface - Java Beginners
Multiple inheritance using interface  Hi, I understand.... But the use of the interface is when we use multiple inheritance with more than one.... Please let me show that sample example to understand the use of interface
Java: Inheritance
Java: Inheritance More on virtual by default Nested Classes, Part 2 Copyleft 2004 Fred Swartz MIT License
Java Inheritance and abstract - Java Interview Questions
Java Inheritance and abstract  1) Why we need to write the separate Interface in which we are not doing any operation except declaring empty abstract... the correct real time example in which i can decide i have to used i have to used
multiple inheritance
multiple inheritance  why java doesn't support multiple inheritance? we can achieve this by using interface but not with classes? what is the main reason that java community implemented like
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
Multiple Inheritance
Multiple Inheritance  Why Java Doesn't have Multiple Inheritance It is Advantage or Disadvantage.........   Hi Friend, When we extends... and inconsistencies Java does not support Multiple Inheritance. Thanks
Multiple Inheritance
class so java any how do multiple inheritance? Can you please explain...Multiple Inheritance  All are saying java doesn't support multiple inheritance but by default Object class is super class for all the user defined
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 - Java Beginners
inheritance  help me on inheritance concept in java specialy on overloading and overridding concept
constructor inheritance
constructor inheritance  can we inherit constructor ?if not then why yhis example is given class A { public A() { System.out.println("In A ctor"); } } class B extends
multiple inheritance - Java Beginners
multiple inheritance  Why java does not support Multiple Inheritance...)why java classes does not support Multiple Inheritance? JAVA CLASSES ONLY... Inheritance. Ans...>java classes doesn't support multiple inheritance,the practical
Inheritance example in JRuby
Inheritance example in JRuby   ... to implement Inheritance in JRuby.  In this example we are defining one class named... # Example program of Inheritance in JRuby   class Base
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
Inheritance - Java Beginners
Inheritance  pls help me!!! Question: write an inheritance hierachy for classes Quadrilateral,Trapezoid,Parallelogram, Rectangle and Square. Use Quadrilateral as superclass. make hierachy as deep as possible. Specify
Inheritance in Java 7
This tutorial describe concept of Inheritance. It is one of OOPs concept
Inheritance
Inheritance        OOPs.... Abstraction 4. Inheritance In OOPs, these concepts are implemented through a class... we will discuss Inheritance briefly:  -- Inheritance is the mechanism
Inheritance Overloading - Java Beginners
Inheritance Overloading  The problem is: I have this program: class Base{ int i = 1; int j = 2; Base(int i, int j){ i = i; j = j...://www.roseindia.net/java/beginners/constructoroverloading.shtml
Java Inheritance - Java Beginners
Java Inheritance  Since every object inherits from Object class, why the following statement is not true. Assuming the code tries to open a file then instead of using throws IOException why not use throws Object? import
Inheritance and Composition - Java Beginners
Inheritance and Composition  CAN YOU FIND THE ERROR OF MY CODE JAVA.THIS IS A TEST CLASS.I CANT FIND THE ERROR,CAN YOU FIX IT FRIEND.THANK YOU SO MUCH. import java.util.*; public class TestPet { public static void main
Inheritance and Composition - Java Beginners
Inheritance and Composition  import java.util.*; public class TestPet { public static void main(String[]kevs) { Scanner console=new Scanner(System.in); String type, name, home; int weight, years; char gender
Inheritance problem - Java Beginners
Inheritance problem  hello friend can u please help me that why this program is printing show method of Base_Class b=5 even though i am assigning the Base_Class object into the Super_Class reference i.e Super_Class bc=new
Inheritance and Composition - Java Beginners
Inheritance and Composition  HELLO FRIEND THIS IS THE CODE,CAN YOU FIND WHAT IS THE ERROR AND IF ITS OK CAN YOU FIX IT .THANK YOU. public class Pet extends Animal { private String home, bite; //private Boolean bites
Inheritance - Java Beginners
of Inheritance within the java programming. class Points{ private double x... and set the center of thye base. THANKS SO MUCH. I am really a java beginner
Inheritance and Composition - Java Beginners
Inheritance and Composition  HELLO FRIEND THIS IS THE CODE,CAN YOU FIND WHAT IS THE ERROR AND IF ITS OK CAN YOU FIX IT .THANK YOU.10000X THANK YOU.HOPE YOU HELP ME. public class Animal { private int lifeExpectancy, weight
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 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
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

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.