1Single Inheritance and multilevel inheritance
class dog { String name; public void rv1() { System.out.println("Bow Bow"); } } class cat extends dog { public void rv2() { System.out.println("Meow Meow"); } } public class prg1 { public static void main(String[] args) { cat c=new cat(); c.rv2(); c.rv1(); } } Output: Meow Meow Bow Bow
2)b)Multilevel inheritance
Source Code:
class colour { public void red() { System.out.println("Apple -red color"); } } class fruit extends colour { public void orange() { System.out.println("orange – orange color"); } } class name extends fruit { public void yellow() { System.out.println("lemon – yellow color"); } } public class prg11 { public static void main(String[] args) { name a=new name(); a.red(); a.orange(); a.yellow(); } } Output: Apple - red color Orange – orange color Lemon – yellow colo
c)Hierarchical inheritance Source Code:
class cs { public void HOD() { System.out.println("Head of Department"); } } class first extends cs { public void first() { System.out.println("first year"); } } class second extends cs { public void second() { System.out.println("second year"); } } class third extends cs { public void third() { System.out.println("final year"); } } public class prg01 { public static void main(String[] args) { first f=new first(); second s=new second(); third t=new third(); f.first(); s.second(); t.third(); } } Output: first year second year final year RESULT:
d)Hybrid Inheritance Source Code
class x { public void fun() { System.out.println("x for xerox"); } } class a extends x { public void fun1() { System.out.println("a for ant"); } } class b extends x { public void fun2() { System.out.println("b for ballon "); } } class c extends x { public static void fun3() { System.out.println("c for cat"); } } public class prg1st { public static void main(String[] args) { c x=new c(); c.fun3(); } } Output: c for ca
Comments
Post a Comment