AIM: Write a program in Java to demonstrate single inheritance, multilevel inheritance and hierarchical inheritance.
SingleInheritance.java
Output
MultilevelInheritance.java
Output
HierarchicalInheritance.java
Output
Happy Coding :)
SingleInheritance.java
// Single Inheritance class ClassA { public void dispA() { System.out.println("disp() method of ClassA"); } } class SingleInheritance extends ClassA { public void dispB() { System.out.println("disp() method of SingleInheritance"); } public static void main(String args[]) { //Assigning SingleInheritance object to SingleInheritance reference SingleInheritance b = new SingleInheritance(); //call dispA() method of ClassA b.dispA(); //call dispB() method of SingleInheritance b.dispB(); } }
Output
Single Inheritance in java by practical server by practical server |
MultilevelInheritance.java
// Multilevel Inheritance class ClassA { public void dispA() { System.out.println("disp() method of ClassA"); } } class ClassB extends ClassA { public void dispB() { System.out.println("disp() method of ClassB"); } } class MultilevelInheritance extends ClassB { public void dispC() { System.out.println("disp() method of MultilevelInheritance"); } public static void main(String args[]) { //Assigning MultilevelInheritance object to MultilevelInheritance reference MultilevelInheritance c = new MultilevelInheritance(); //call dispA() method of ClassA c.dispA(); //call dispB() method of ClassB c.dispB(); //call dispC() method of MultilevelInheritance c.dispC(); } }
Output
Multilevel inheritance in java by practical server |
HierarchicalInheritance.java
class ClassA { public void dispA() { System.out.println("disp() method of ClassA"); } } class ClassB extends ClassA { public void dispB() { System.out.println("disp() method of ClassB"); } } class ClassC extends ClassA { public void dispC() { System.out.println("disp() method of ClassC"); } } class ClassD extends ClassA { public void dispD() { System.out.println("disp() method of ClassD"); } } public class HierarchicalInheritance { public static void main(String args[]) { //Assigning ClassB object to ClassB reference ClassB b = new ClassB(); //call dispB() method of ClassB b.dispB(); //call dispA() method of ClassA b.dispA(); //Assigning ClassC object to ClassC reference ClassC c = new ClassC(); //call dispC() method of ClassC c.dispC(); //call dispA() method of ClassA c.dispA(); //Assigning ClassD object to ClassD reference ClassD d = new ClassD(); //call dispD() method of ClassD d.dispD(); //call dispA() method of ClassA d.dispA(); } }
Output
Hierarchical inheritance in java by practical server |
Happy Coding :)
0 Comments
Post a Comment