Inheritance in Java
Inheritance in Java
&
Types of Inheritance
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}
Output
Base class method
Child class method
Java Single Inheritance Program
1. class Animal
2. {
3. void eat()
4. {
5. System.out.println("eating...");
6. }
7. }
8. class Dog extends Animal
9. {
10. void bark()
11. {
12. System.out.println("barking...");
13. }
14. }
15. class TestInheritance
16. {
17. public static void main(String args[])
18. {
19. Dog d=new Dog();
20. d.bark();
21. d.eat();
22. }
23. }
Output
barking…
Eating…
Multi-Level Inheritance
• Multi-level inheritance can be considered as a
addon to single inheritance as in this type we
have more than one level of inheritance (shown in
the diagram below).
• In multi-level Inheritance, we have a single Super
Class and a subclass1(level1) which inherits the
properties directly from the Super class & then
we have on more subclass2(level2) which
inherits the properties directly from the subclass
1 class.
1. Class SuperClass
2. {
3. public void methodA()
4. {
5. System.out.println("SuperClass");
6. }
7. }
8. Class SubClass1 extends SuperClass
9. {
10. public void methodB()
11. {
12. System.out.println("SubClass1 ");
13. }
14. }
15.
16. Class SubClass2 extends SubClass1
17. {
18. public void methodC()
19. {
20. System.out.println("SubClass2");
21. }
22. public static void main(String args[])
23. {
24. SubClass2 obj = new SubClass2();
25. SubClass2.methodA(); //calling super class method
Output 26. SubClass2.methodB(); //calling subclass 1 method
SuperClass 27. SubClass2.methodC(); //calling own method
SubClass1 28. }
SubClass2 29. }
Java Multilevel Inheritance Program
1. class Animal {
2. void eat() {
3. System.out.println("eating...");
4. }
5. }
6. class Dog extends Animal {
7. void bark() {
8. System.out.println("barking...");
9. }
10. }
11. class BabyDog extends Dog {
12. void weep() {
13.
14. }
System.out.println("weeping..."); Output:
15. } weeping…
16. class TestInheritance2 {
17. public static void main(String args[]) { barking…
18. BabyDog d = new BabyDog();
19. d.weep(); eating…
20. d.bark();
21. d.eat();
22. }
23. }
Hierarchical Inheritance
• Hierarchical inheritance is again an extenstion
to single inheritance as there are multiple
single inheritance in this type.
• In Hierarchical Inheritance, we have a single
Super Class and a multiple Sub
Classes which inherits the properties directly
from this Super class.
1. Class A
2. {
3. public void methodA()
4. {
5. System.out.println("Super class method");
6. }
7. }
8. Class B extends A
9. {
10. public void methodB()
11. {
12. System.out.println("Sub class Method B");
13. }
14. }
15.
16. Class C extends A
17. {
18. public void methodC()
19. {
20. System.out.println("Sub class Method C");
21. }
22. public static void main(String args[])
23. {
24. A obj1 = new A();
25. B obj2 = new B();
26. C obj3 = new C();
27. obj1.methodA(); //calling super class method
28. obj2.methodA(); //calling A method from subclass
object
29. obj3.methodA(); //calling A method from subclass
object
Java Hierarchical Inheritance Program
1. class Animal {
2. void eat() {
3. System.out.println("eating...");
4. }
5. }
6. class Dog extends Animal {
7. void bark() {
8. System.out.println("barking...");
9. }
10. }
11. class Cat extends Animal {
12. void meow() {
13. System.out.println("meowing...");
14. }
15. }
16. class TestInheritance3 {
17. public static void main(String args[]) {
18. Cat c = new Cat();
19. c.meow();
20. c.eat();
21. //c.bark();//C.T.Error
22. }
23. }
Hybrid Inheritance
• Hybrid Inheritance is a combination of
both Single Inheritance and Multiple
Inheritance.
• Since in Java Multiple Inheritance is not
supported directly we can achieve Hybrid
inheritance also through Interfaces only.
Hybrid Inheritance