Вы находитесь на странице: 1из 22

Inheritance in Java

&
Types of Inheritance

PROGRAMMING WITH JAVA


Inheritance
• Inheritance in Java is an important concept of OOP(Object
Oriented Programming).
• It is the mechanism in java by which one class is allow
to inherit the features(fields and methods) of another class.
• The idea behind inheritance in java is that you can create new
classes that are built upon existing classes.
• When you inherit from an existing class, you
can reuse methods and fields of parent class, and you can add
new methods and fields also.
• The class which inherits the properties of other is known
as subclass (derived class, child class) and the class whose
properties are inherited is known as superclass (base class,
parent class).
Important terminology:
• Super Class: The class whose features are
inherited is known as super class(or a base
class or a parent class).
• Sub Class: The class that inherits the other
class is known as sub class(or a derived class,
extended class, or child class). The subclass
can add its own fields and methods in addition
to the superclass fields and methods.
Why use inheritance in java?
• For Method Overriding (so runtime
polymorphism can be achieved).
• For Code Reusability.
How to use inheritance in Java?
• The keyword used for inheritance is extends.
• Following is the syntax of extends keyword.
inheritance syntax(General Form)
class Super {
.....
.....
}
class Sub extends Super {
.....
.....}
Java inheritance Program Example
class Employee
{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
} Output:
Programmer salary is:40000.0
Bonus of programmer is:10000
Program Explanation
Types of inheritance in java
Single Inheritance in Java with
Program Example
• Single inheritance is the most simplest type of
inheritance in java.
• In single Inheritance, we have a single Super
Class and a single Sub Class which inherits the
properties from the Super class.
Class A
{
public void methodA()
{
System.out.println("Base class method");
}
}

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

As we can see in the above diagram ClassA is the Parent for


both ClassB and ClassC which is Single Inheritance and
again ClassB and ClassC again act as Parent for ClassC(Multiple Inheritance
which is not supported by Java).
Implementation of Hybrid Inheritance
with Classes
1. public class ClassA 30. public class ClassD extends ClassB,ClassC
2. { 31. {
3. public void dispA() 32. public void dispD()
4. { 33. {
5. System.out.println("disp() method of ClassA"); 34. System.out.println("disp() method of ClassD");
6. } 35. }
7. } 36. public static void main(String args[])
8. public class ClassB extends ClassA 37. {
9. { 38. ClassD d = new ClassD();
10. public void show() 39. d.dispD();
11. { 40. d.show();//Confusion happens here which show method to
12. System.out.println("show() method of ClassB"); call
13. } 41. }
14. public void dispB() 42. }
15. {
16. System.out.println("disp() method of ClassB");
17. } • Output :
18. } • Error!!
19. public class ClassC extends ClassA • We all know that we cannot extend ClassB and ClassC to ClassD at the
20. { same time but the point to be noted here is why it is not allowed. Since
we have same show() method in both ClassB and ClassDcompiler will
21. public void show() not be able to differentiate which method to call whereas if we are
22. { using interface we can avoid that ambiguity.
23. System.out.println("show() method of ClassC");
24. }
25. public void dispC()
26. {
27. System.out.println("disp() method of ClassC");
28. }
29. }
Implementation of Hybrid Inheritance
with Interfaces
1. public class ClassA implementation");
2. { 21. }
3. public void dispA() 22. public void dispD()
4. { 23. {
5. System.out.println("disp() method of 24. System.out.println("disp() method of
ClassA"); ClassD");
6. } 25. }
7. } 26. public static void main(String args[])
8. public interface InterfaceB 27. {
9. { 28. ClassD d = new ClassD();
10. public void show(); 29. d.dispD();
11. } 30. d.show();
12. public interface InterfaceC 31. }
13. { 32. }
14. public void show(); • Output :
15. } • disp() method of ClassD
16. public class ClassD implements • show() method implementation
InterfaceB,InterfaceC
17. {
18. public void show()
19. {
20. System.out.println("show() method
Why multiple inheritance is not
supported in java?
• To reduce the complexity and simplify the
language, multiple inheritance is not supported in
java.
• Consider a scenario where A, B and C are three
classes. The C class inherits A and B classes.
• If A and B classes have same method and you call
it from child class object, there will be ambiguity
to call method of A or B class.
• In C++ we have virtual keyword to tackle this
ambiguity however, in Java Multiple Inheritance is
possible only via Interfaces.

Вам также может понравиться