Professional Documents
Culture Documents
Practical-3: 1.1 Program To Learn Different Types of Inheritance in Java
Practical-3: 1.1 Program To Learn Different Types of Inheritance in Java
● CODE:-
1). Single Inheritance
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class TestInheritance{
8. public static void main(String args[]){
9. Dog d=new Dog();
10. d.bark();
11. d.eat();
12. }
13. }
● OUTPUT:-
2). Multilevel Inheritance
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class BabyDog extends Dog{
8. void weep(){System.out.println("weeping...");}
9. }
10. class TestInheritance2{
11. public static void main(String args[]){
12. BabyDog d=new BabyDog();
13. d.weep();
14. d.bark();
15. d.eat();
16.}}
● OUTPUT:-
3). Hierarchical Inheritance
1. class one
2. {
3. public void print_geek()
4. {
5. System.out.println("Geeks");
6. }
7. }
8.
9. class two extends one
10. {
11. public void print_for()
12. {
13. System.out.println("for");
14. }
15. }
16.
17. class three extends one
18. {
19. /*............*/
20. }
21.
22. // Derived class
23. public class Main
24. {
25. public static void main(String[] args)
26. {
27. three g = new three();
28. g.print_geek();
29. two t = new two();
30. t.print_for();
31. g.print_geek();
32. }
33. }
● OUTPUT:-
1. interface one
2. {
3. public void print_geek();
4. }
5.
6. interface two
7. {
8. public void print_for();
9. }
10.
11. interface three extends one,two
12. {
13. public void print_geek();
14. }
15. class child implements three
16. {
17. @Override
18. public void print_geek() {
19. System.out.println("Geeks");
20. }
21.
22. public void print_for()
23. {
24. System.out.println("for");
25. }
26. }
27.
28. // Derived class
29. public class Main
30. {
31. public static void main(String[] args)
32. {
33. child c = new child();
34. c.print_geek();
35. c.print_for();
36. c.print_geek();
37. }
38. }
● OUTPUT:-