KEMBAR78
Inheritance in Java - An Introduction & types | PPTX
JAVA PROGRAMMING
Mrs M.Vijetha
Assistant Professor
SRCAS
SINGLE INHERITANCE
 Single inheritance is damn easy to understand.
When a class extends another one class only then
we call it a single inheritance.
 The below flow diagram shows that class B
extends only one class which is A. Here A is
a parent class of B and B would be a child
class of A.
 When one subclass inherits the features of one
superclass, this would be the case of Single
inheritance.
 In the example given below, the base class A will
be inherited by a subclass B.
SINGLE INHERITANCE
 Single Inheritance example program in Java
 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 } }
MULTIPLE INHERITANCE
 “Multiple Inheritance” refers to the concept of
one class extending (Or inherits) more than one
base class.
 The inheritance we learnt earlier had the concept
of one base class or parent.
 The problem with “multiple inheritance” is that
the derived class will have to manage the
dependency on two base classes.
 In the case of multiple inheritances, there could
be more than one parent classes for a given
subclass.
 For the example given below, C is the subclass
having more than one parent class i.e. A and the
B. Multiple inheritances are not supported in
Java through classes but this is possible through
interfaces and the default implementation of
methods in Java 8 and the later versions.
 If two parent class has similar methods with the
same signature, the default method is used to
override the other. If we remove the
implementation of default methods from the
subclass then it will show the compiler error
again.
MULTIPLE INHERITANCE
 Note 1: Multiple Inheritance is very rarely used in
software projects.
 Using Multiple inheritance often leads to problems in
the hierarchy. This results in unwanted complexity
when further extending the class.
 Note 2: Most of the new OO languages like Small
Talk, Java, C# do not support Multiple
inheritance. Multiple Inheritance is supported in C+
+.
MULTILEVEL INHERITANCE
 Multilevel inheritance refers to a mechanism
in OO technology where one can inherit from a
derived class, thereby making this derived class
the base class for the new class.
 As you can see in below flow diagram C is
subclass or child class of B and B is a child class
of A.
 In the case of multi-level inheritance, a subclass
that is inheriting one parent class will also act as
the base class for another class.
 Based on the example given below, B is the
subclass that is inhering the features of parent
class A and acting as the base class for C
subclass.
MULTILEVEL INHERITANCE EXA
MPLE PROGRAM IN JAVA
 public static void main(String args[])
 { Z obj = new Z(); obj.methodX();
 //calling grand parent class method
obj.methodY();
 //calling parent class method obj.methodZ();
 //calling local method }
HIERARCHICAL INHERITANCE
 In such kind of inheritance one class is inherited
by many sub classes.
 In below example class B,C and D inherits the
same class A.
 A is parent class (or base class) of B,C & D.
HIERARCHICAL INHERITANCE
 In the case of Hierarchical Inheritance, there is
one base class for multiple subclasses.
 For the example given below, A is the base class
that is inherited by multiple subclasses B, C, and
D.
HYBRID INHERITANCE
 In simple terms you can say that Hybrid
inheritance is a combination
of Single and Multiple inheritance.
 A typical flow diagram would look like below. A
hybrid inheritance can be achieved in the java in
a same way as multiple inheritance can be!!
 Using interfaces. yes you heard it right. By
using interfaces you can have multiple as well
as hybrid inheritance in Java.
 This is mix or two or more types of inheritance
discussed earlier.
 Like the multiple inheritances, hybrid
inheritance is also not supported in Java through
classes but you could make it possible again
through interfaces.
A FEW INTERESTING FACTS ABOUT
INHERITANCE IN JAVA
 The object class has no superclass and every
class could have only one and one superclass.
This is also named as the Single Inheritance. If
the explicit superclass is missing then every class
is an example of the implicit subclass of the
object class.
 The superclass is only one but it could have
multiple subclasses derived from the same.
Interestingly Java does not support the concept
of multiple inheritances with classes. At the
same time, multiple inheritance are supported by
using interfaces in Java.
 A subclass has the capability to access all
features from the base class like field, methods,
or nested class etc.
 However, this is possible to define the attributes
either private or public. This is not possible to
access the private member of a class in Java. On
the other hand, if methods are defined as either
public or protected then they can be quickly
accessed by the subclass.
 When there “Is-A” relationship exists between
two classes, the inheritance concept is used. It
can be described with extends keyword in coding.
HOW POWERFUL COULD A SUBCLASS BE?
 For the sub class, we can inherit members, hide
them, replace them or supplement them with new
members as per the convenience.
 This is possible to use the inherited members
directly like you are using the other fields of a class.
Apart from the inherited fields, you could always
add the new attributes to make it even more
powerful.
 This is possible to define the new instance method
for the subclass that would be given the same
signature as the superclass. In this case, method
overriding concept will be applicable. We could
define new subclass constructors that can be invoked
from the superclass with the keyword Super.
WHY ARE MULTIPLE INHERITANCES NOT
SUPPORTED IN JAVA?
 As the name suggests, this is a popular
Oops concept where properties of more than one
parent class can be inherited. There comes an
issue when a method has the same signature in
both parent class and the subclass. When a
method call request is put, java compiler gets
confused which method should be invoked
actually either of parent class or subclass.
 In the final output, there would be a compiler
error that needs to resolve quickly. For this
reason, Java does not support the concept of
multiple inheritances of classes which is quite
complex
Inheritance in Java - An Introduction & types

Inheritance in Java - An Introduction & types

  • 1.
  • 2.
    SINGLE INHERITANCE  Singleinheritance is damn easy to understand. When a class extends another one class only then we call it a single inheritance.  The below flow diagram shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A.
  • 3.
     When onesubclass inherits the features of one superclass, this would be the case of Single inheritance.  In the example given below, the base class A will be inherited by a subclass B.
  • 4.
  • 5.
     Single Inheritanceexample program in Java  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 } }
  • 6.
    MULTIPLE INHERITANCE  “MultipleInheritance” refers to the concept of one class extending (Or inherits) more than one base class.  The inheritance we learnt earlier had the concept of one base class or parent.  The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes.
  • 7.
     In thecase of multiple inheritances, there could be more than one parent classes for a given subclass.  For the example given below, C is the subclass having more than one parent class i.e. A and the B. Multiple inheritances are not supported in Java through classes but this is possible through interfaces and the default implementation of methods in Java 8 and the later versions.  If two parent class has similar methods with the same signature, the default method is used to override the other. If we remove the implementation of default methods from the subclass then it will show the compiler error again.
  • 8.
  • 9.
     Note 1:Multiple Inheritance is very rarely used in software projects.  Using Multiple inheritance often leads to problems in the hierarchy. This results in unwanted complexity when further extending the class.  Note 2: Most of the new OO languages like Small Talk, Java, C# do not support Multiple inheritance. Multiple Inheritance is supported in C+ +.
  • 10.
    MULTILEVEL INHERITANCE  Multilevelinheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class.  As you can see in below flow diagram C is subclass or child class of B and B is a child class of A.
  • 11.
     In thecase of multi-level inheritance, a subclass that is inheriting one parent class will also act as the base class for another class.  Based on the example given below, B is the subclass that is inhering the features of parent class A and acting as the base class for C subclass.
  • 13.
    MULTILEVEL INHERITANCE EXA MPLEPROGRAM IN JAVA  public static void main(String args[])  { Z obj = new Z(); obj.methodX();  //calling grand parent class method obj.methodY();  //calling parent class method obj.methodZ();  //calling local method }
  • 14.
    HIERARCHICAL INHERITANCE  Insuch kind of inheritance one class is inherited by many sub classes.  In below example class B,C and D inherits the same class A.  A is parent class (or base class) of B,C & D.
  • 15.
  • 16.
     In thecase of Hierarchical Inheritance, there is one base class for multiple subclasses.  For the example given below, A is the base class that is inherited by multiple subclasses B, C, and D.
  • 17.
    HYBRID INHERITANCE  Insimple terms you can say that Hybrid inheritance is a combination of Single and Multiple inheritance.  A typical flow diagram would look like below. A hybrid inheritance can be achieved in the java in a same way as multiple inheritance can be!!  Using interfaces. yes you heard it right. By using interfaces you can have multiple as well as hybrid inheritance in Java.
  • 18.
     This ismix or two or more types of inheritance discussed earlier.  Like the multiple inheritances, hybrid inheritance is also not supported in Java through classes but you could make it possible again through interfaces.
  • 19.
    A FEW INTERESTINGFACTS ABOUT INHERITANCE IN JAVA  The object class has no superclass and every class could have only one and one superclass. This is also named as the Single Inheritance. If the explicit superclass is missing then every class is an example of the implicit subclass of the object class.  The superclass is only one but it could have multiple subclasses derived from the same. Interestingly Java does not support the concept of multiple inheritances with classes. At the same time, multiple inheritance are supported by using interfaces in Java.
  • 20.
     A subclasshas the capability to access all features from the base class like field, methods, or nested class etc.  However, this is possible to define the attributes either private or public. This is not possible to access the private member of a class in Java. On the other hand, if methods are defined as either public or protected then they can be quickly accessed by the subclass.  When there “Is-A” relationship exists between two classes, the inheritance concept is used. It can be described with extends keyword in coding.
  • 21.
    HOW POWERFUL COULDA SUBCLASS BE?  For the sub class, we can inherit members, hide them, replace them or supplement them with new members as per the convenience.  This is possible to use the inherited members directly like you are using the other fields of a class. Apart from the inherited fields, you could always add the new attributes to make it even more powerful.  This is possible to define the new instance method for the subclass that would be given the same signature as the superclass. In this case, method overriding concept will be applicable. We could define new subclass constructors that can be invoked from the superclass with the keyword Super.
  • 22.
    WHY ARE MULTIPLEINHERITANCES NOT SUPPORTED IN JAVA?  As the name suggests, this is a popular Oops concept where properties of more than one parent class can be inherited. There comes an issue when a method has the same signature in both parent class and the subclass. When a method call request is put, java compiler gets confused which method should be invoked actually either of parent class or subclass.  In the final output, there would be a compiler error that needs to resolve quickly. For this reason, Java does not support the concept of multiple inheritances of classes which is quite complex