SlideShare a Scribd company logo
1 of 25
Inheritance & Polymorphism
(In JAVA)
-KARTIK KAPGATE
Inheritance
Defn.- Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object.
It is an important part of OOPs (Object Oriented programming system).
Important terminology:
● Class
● Super Class:
● Sub Class
CLASS
● Defn. - A class is a group of objects which have common properties. It is a template or blueprint from which
objects are created.
Super-Class/Parent Class
● Super Class: The class whose features are inherited is known as super class(or a base class or a parent class).
Child Class/Subclass
● Sub Class: The class that inherits the other class is known as subclass(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
Types of Inheritance
♦ Single Inheritance
♦Multiple Inheritance
♦Multilevel Inheritance
♦Hierarchical Inheritance
♦Hybrid Inheritance
Single Inheritance
Defn. - When a class extends another one class only then we call it a single inheritance.
♦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.
Code Snippet -
Single Inheritance
Multiple Inheritance(Through Interfaces)
Defn.- In Multiple inheritance ,one class can have more than one superclass and inherit features from all parent classes.
♦Java does not support multiple inheritance with classes. In java, we can achieve multiple inheritance only through
Interfaces.
Code Snippet -
Multilevel Inheritance
Defn. - One can inherit from a derived class, thereby making this derived class the base class for the new class.
A is Superclass to B => B is child of A.
B is Superclass to C => C is child of B and Grandchild of A,
Code Snippet-
Hierarchical Inheritance
Defn. - One class serves as a superclass (base class) for more than one subclass.
♦The class A serves as a base class for the derived class B,C and D.
Code Snippet-
Hybrid Inheritance (Through Interfaces)
Defn. - It is a mix of two or more of the above types of inheritance.
A => B,C (Hierarchical Inheritance)
B,C => D (Multiple Inheritance)
Code Snippet -
Important Points
● Default superclass: Except Object class, which has no superclass, every class has one and only one direct
superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a
subclass of Object class.
● Superclass can only be one: A superclass can have any number of subclasses. But a subclass can have only
one superclass. This is because Java does not support multiple inheritance with classes. Although with
interfaces, multiple inheritance is supported by java.
● Inheriting Constructors: A subclass inherits all the members (fields, methods, and nested classes) from its
superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the
superclass can be invoked from the subclass.
● Private member inheritance: A subclass does not inherit the private members of its parent class. However, if
the superclass has public or protected methods(like getters and setters) for accessing its private fields, these
can also be used by the subclass.
What can be done in a Subclass
● The inherited fields can be used directly, just like any other fields.
● We can declare new fields in the subclass that are not in the superclass.
● The inherited methods can be used directly as they are.
● We can write a new instance method in the subclass that has the same signature as the one in the superclass,
thus overriding it (as in example above, toString() method is overridden).
● We can write a new static method in the subclass that has the same signature as the one in the superclass,
thus hiding it.
● We can declare new methods in the subclass that are not in the superclass.
● We can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using
the keyword super.
POLYMORPHISM (In Java)
Defn. - Polymorphism in Java is a concept by which we can perform a single action in different ways.
1. ♦ In Java polymorphism is mainly divided into two types:
a. Compile time Polymorphism
i. Method Overloading
1. By Changing number of Arguments
2. By Changing Data types of Arguments
ii. Operator Overloading ( ‘+’ Operator )
b. Runtime Polymorphism
i. Method Overriding
Compile time Polymorphism
Defn. - Polymorphism that is resolved during compiler time is known as static polymorphism.
♦It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator
overloading.
Method Overloading
Defn. -When there are multiple functions with same name but different parameters then these functions are said to be overloaded.
Functions can be overloaded by change in number of arguments or/and change in type of arguments.
1.By Changing number of Arguments 2)By Changing Data
types of Arguments
Operator Overloading
Defn. - We can make the operator (‘+’) for string class to concatenate two strings.
Runtime Polymorphism
Defn. - Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime, thats why it
is called runtime polymorphism. It is also known as Dynamic Method Dispatch.
Code Snippet-

More Related Content

What's hot

itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in javaAtul Sehdev
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
Java Inheritance | Java Course
Java Inheritance | Java Course Java Inheritance | Java Course
Java Inheritance | Java Course RAKESH P
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism JavaM. Raihan
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in JavaKurapati Vishwak
 
Java Inheritance
Java InheritanceJava Inheritance
Java InheritanceVINOTH R
 

What's hot (20)

Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
 
inheritance
inheritanceinheritance
inheritance
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Java Inheritance | Java Course
Java Inheritance | Java Course Java Inheritance | Java Course
Java Inheritance | Java Course
 
Java(inheritance)
Java(inheritance)Java(inheritance)
Java(inheritance)
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
javainheritance
javainheritancejavainheritance
javainheritance
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 

Similar to Inheritance and Polymorphism (20)

PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Java session2
Java session2Java session2
Java session2
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
 
Java programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- InheritanceJava programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- Inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
Java
JavaJava
Java
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Oops
OopsOops
Oops
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Unit 2
Unit 2Unit 2
Unit 2
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
 
Topic inheritance
Topic  inheritanceTopic  inheritance
Topic inheritance
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Ayan Das_25300121057.pptx
Ayan Das_25300121057.pptxAyan Das_25300121057.pptx
Ayan Das_25300121057.pptx
 
Unit 4
Unit 4Unit 4
Unit 4
 

Recently uploaded

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 

Recently uploaded (20)

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 

Inheritance and Polymorphism

  • 1. Inheritance & Polymorphism (In JAVA) -KARTIK KAPGATE
  • 2. Inheritance Defn.- Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system). Important terminology: ● Class ● Super Class: ● Sub Class
  • 3. CLASS ● Defn. - A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
  • 4. Super-Class/Parent Class ● Super Class: The class whose features are inherited is known as super class(or a base class or a parent class).
  • 5. Child Class/Subclass ● Sub Class: The class that inherits the other class is known as subclass(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
  • 6.
  • 7. Types of Inheritance ♦ Single Inheritance ♦Multiple Inheritance ♦Multilevel Inheritance ♦Hierarchical Inheritance ♦Hybrid Inheritance
  • 8. Single Inheritance Defn. - When a class extends another one class only then we call it a single inheritance. ♦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.
  • 9. Code Snippet - Single Inheritance
  • 10. Multiple Inheritance(Through Interfaces) Defn.- In Multiple inheritance ,one class can have more than one superclass and inherit features from all parent classes. ♦Java does not support multiple inheritance with classes. In java, we can achieve multiple inheritance only through Interfaces.
  • 12. Multilevel Inheritance Defn. - One can inherit from a derived class, thereby making this derived class the base class for the new class. A is Superclass to B => B is child of A. B is Superclass to C => C is child of B and Grandchild of A,
  • 14. Hierarchical Inheritance Defn. - One class serves as a superclass (base class) for more than one subclass. ♦The class A serves as a base class for the derived class B,C and D.
  • 16. Hybrid Inheritance (Through Interfaces) Defn. - It is a mix of two or more of the above types of inheritance. A => B,C (Hierarchical Inheritance) B,C => D (Multiple Inheritance)
  • 18. Important Points ● Default superclass: Except Object class, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object class. ● Superclass can only be one: A superclass can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance with classes. Although with interfaces, multiple inheritance is supported by java. ● Inheriting Constructors: A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. ● Private member inheritance: A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods(like getters and setters) for accessing its private fields, these can also be used by the subclass.
  • 19. What can be done in a Subclass ● The inherited fields can be used directly, just like any other fields. ● We can declare new fields in the subclass that are not in the superclass. ● The inherited methods can be used directly as they are. ● We can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it (as in example above, toString() method is overridden). ● We can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it. ● We can declare new methods in the subclass that are not in the superclass. ● We can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.
  • 20. POLYMORPHISM (In Java) Defn. - Polymorphism in Java is a concept by which we can perform a single action in different ways. 1. ♦ In Java polymorphism is mainly divided into two types: a. Compile time Polymorphism i. Method Overloading 1. By Changing number of Arguments 2. By Changing Data types of Arguments ii. Operator Overloading ( ‘+’ Operator ) b. Runtime Polymorphism i. Method Overriding
  • 21. Compile time Polymorphism Defn. - Polymorphism that is resolved during compiler time is known as static polymorphism. ♦It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading.
  • 22. Method Overloading Defn. -When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments. 1.By Changing number of Arguments 2)By Changing Data types of Arguments
  • 23. Operator Overloading Defn. - We can make the operator (‘+’) for string class to concatenate two strings.
  • 24. Runtime Polymorphism Defn. - Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime, thats why it is called runtime polymorphism. It is also known as Dynamic Method Dispatch.