© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
Quick Links
Easy Studies

Implementation Of Inheritance Using Java


Inheritance is the way a Subclass uses method definitions and variables from a Super class just as if they belonged to the Subclass itself. In Java inheritance can be achieved by extending another class. If the class is a subclass of any class, then it can access the member of that class. In this program Animal class is the super class and the Dog class is the subclass. Dog class can use the eat () method using its own reference and also with the super class reference. Objects of the same class will not be the same. Here d and d1 are the objects of class Dog but they are not equal.

Implementation Of Inheritance Using Java:

class Animal { public void eat(){ System.out.println("Animal eat method"); } } class Dog2 extends Animal { public void sleep() { System.out.println("Dog method"); } public static void main(String [] args) { Animal a = new Animal(); Dog2 d = new Dog2(); Dog2 d1 = new Dog2(); if(d==d1){ System.out.println("Equal"); }else{ System.out.println("Not"); } d.eat(); d1.sleep(); a.eat(); } }

Sample ScreenShot:

SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑