SlideShare a Scribd company logo
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.
Inheritance in Java - An Introduction & types
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

More Related Content

PDF
java_vyshali.pdf
PPTX
Inheritance ppt
PPTX
Inheritance,single,multiple.access rulepptx
PPT
Java inheritance
DOCX
Ganesh groups
PPTX
INHERTANCE , NARROW AND WIDENING
PDF
Object Oriented Programming - 7.1. Inheritance
PPT
Unit 3 Java
java_vyshali.pdf
Inheritance ppt
Inheritance,single,multiple.access rulepptx
Java inheritance
Ganesh groups
INHERTANCE , NARROW AND WIDENING
Object Oriented Programming - 7.1. Inheritance
Unit 3 Java

Similar to Inheritance in Java - An Introduction & types (20)

PPTX
Inheritance in java
PPTX
Java interview-quetions
PPTX
Inheritance In Java
PPTX
Inheritance in C++ (Programming Fundamentals)
PPTX
Inheritance in oops
PPTX
Inheritance in java
PPTX
Java OOPS Concept
PDF
4th_class.pdf
PDF
Inheritance used in java
PDF
Inheritance and Substitution
PPTX
Inheritance
PPTX
Inheritance
PPT
E -COMMERCE.ppt
PPT
web program-Inheritance,pack&except in Java.ppt
PPTX
Multiple inheritance in java3 (1).pptx
PPTX
Inheritance in java
PPTX
Java(inheritance)
PPTX
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
PPTX
Inheritance in java
PDF
java inheritance that is used in oop cls
Inheritance in java
Java interview-quetions
Inheritance In Java
Inheritance in C++ (Programming Fundamentals)
Inheritance in oops
Inheritance in java
Java OOPS Concept
4th_class.pdf
Inheritance used in java
Inheritance and Substitution
Inheritance
Inheritance
E -COMMERCE.ppt
web program-Inheritance,pack&except in Java.ppt
Multiple inheritance in java3 (1).pptx
Inheritance in java
Java(inheritance)
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java
java inheritance that is used in oop cls
Ad

Recently uploaded (20)

PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Cell Structure & Organelles in detailed.
PDF
Insiders guide to clinical Medicine.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Institutional Correction lecture only . . .
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Complications of Minimal Access Surgery at WLH
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Cell Structure & Organelles in detailed.
Insiders guide to clinical Medicine.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
102 student loan defaulters named and shamed – Is someone you know on the list?
VCE English Exam - Section C Student Revision Booklet
Final Presentation General Medicine 03-08-2024.pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
O7-L3 Supply Chain Operations - ICLT Program
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pharma ospi slides which help in ospi learning
Institutional Correction lecture only . . .
Microbial diseases, their pathogenesis and prophylaxis
PPH.pptx obstetrics and gynecology in nursing
TR - Agricultural Crops Production NC III.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Complications of Minimal Access Surgery at WLH
Ad

Inheritance in Java - An Introduction & types

  • 2. 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.
  • 3.  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.
  • 5.  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 } }
  • 6. 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.
  • 7.  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.
  • 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  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.
  • 11.  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.
  • 13. 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 }
  • 14. 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.
  • 16.  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.
  • 17. 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.
  • 18.  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.
  • 19. 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.
  • 20.  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.
  • 21. 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.
  • 22. 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