In Single Inheritance there is one super class which is extended by the sub class. In below program class Room is extended by BedRoom class ,both classes have their own data members and methods. The constructor in derived class(BedRoom class) uses super keyword to pass values that are required by the base class(Room class) constructor. Detailed program is shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | class Room{ int length; int breadth; public Room(int x,int y) { length = x; breadth = y; } int area(){ return (length * breadth); } } class BedRoom extends Room { int height; public BedRoom(int x, int y, int z) { super(x, y); height = z; } int volume(){ return (length * breadth * height); } } public class SingleInheritanceExample { public static void main(String[] args) { BedRoom b = new BedRoom(10, 10, 10); int area = b.area(); int volume = b.volume(); System.out.println("AREA : "+area); System.out.println("VOLUME : "+volume); } } |
Output:
AREA : 100
VOLUME : 1000
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.


No comments:
Post a Comment