SlideShare a Scribd company logo
INHERITANCE AND
POLYMORPHISM IN JAVA
Object-Oriented Programming
www.beginwithjava.comwww.beginwithjava.com
WHAT YOU SHOULD LEARN
www.beginwithjava.com
  What Is Inheritance?
  Calling the Superclass Constructor
 Overriding Methods
 Protected Member
 Multilevel Inheritance
  Polymorphism
 Abstract Classes and Abstract Method
 Interfaces
WHAT IS INHERITANCE
 Inheritance is the mechanism that allows
programmers to create new classes from existing
class. By using inheritance programmers can re-use
code they've already written.
 Any new class that you create from an existing
class is called sub class; existing class is
called super class.
www.beginwithjava.com
www.beginwithjava.com
The complete program of above diagram is available at : www.beginwithjava.com
The sub class gets all of the methods and state variables of the super
class by default.
The sub class can add new methods and state variables.
CALLING THE SUPER CLASS
CONSTRUCTOR
 A subclass can have its own private data members,
so a subclass can also have its own constructors.
 The constructors of the subclass can initialize only
the instance variables of the subclass. 
 When a subclass object is instantiated the subclass
object must also automatically execute one of the
constructors of the super class.
 To call a super class constructor
the super keyword is used.
www.beginwithjava.com
The complete example program is available at : www.beginwithjava.com
OVERRIDING SUPER CLASS
METHODS
 In a class hierarchy, when a method in a subclass
has the same name and type signature as a method
in its super class, then the method in the subclass
is said to override the method in the super class. 
 The version of the method defined by the super
class will be hidden inside subclass.
www.beginwithjava.com
The complete example program is available at : www.beginwithjava.com
PROTECTED MEMBER
 The private members of a class cannot be directly
accessed outside the class. Only methods of that class
can access the private members directly.
 However, sometimes it may be necessary for a subclass
to access a private member of a super class.
 If you make a private member public, then anyone can
access that member. So, if a member of a super class
needs to be (directly) accessed in a subclass and yet still
prevent its direct access outside the class, you must
declare that member protected.
www.beginwithjava.com
Modifier Class Subclass World
public Y Y Y
protected Y Y N
private Y N N
 continue….
Visit www.beginwithjava.com

More Related Content

What's hot (20)

PPTX
Inheritance in java
Tech_MX
 
PPTX
Inheritance
yugandhar vadlamudi
 
PPT
Java Programming - Inheritance
Oum Saokosal
 
PPTX
Dynamic method dispatch
yugandhar vadlamudi
 
PDF
Inheritance In Java
Arnab Bhaumik
 
PPTX
Multiple inheritance possible in Java
Kurapati Vishwak
 
PPTX
Java Inheritance
VINOTH R
 
PPT
Inheritance in java
Lovely Professional University
 
PDF
java-06inheritance
Arjun Shanka
 
PPTX
Ppt on this and super keyword
tanu_jaswal
 
PPTX
Inheritance in Java
Elizabeth alexander
 
PPS
Inheritance chepter 7
kamal kotecha
 
PPTX
Inheritance ppt
Nivegeetha
 
PPTX
Inheritance In Java
Darpan Chelani
 
PPT
Csharp4 inheritance
Abed Bukhari
 
PDF
javainheritance
Arjun Shanka
 
PPT
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
PPT
Java inheritance
Arati Gadgil
 
PPTX
Inheritance in java
yash jain
 
PPTX
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Inheritance in java
Tech_MX
 
Inheritance
yugandhar vadlamudi
 
Java Programming - Inheritance
Oum Saokosal
 
Dynamic method dispatch
yugandhar vadlamudi
 
Inheritance In Java
Arnab Bhaumik
 
Multiple inheritance possible in Java
Kurapati Vishwak
 
Java Inheritance
VINOTH R
 
Inheritance in java
Lovely Professional University
 
java-06inheritance
Arjun Shanka
 
Ppt on this and super keyword
tanu_jaswal
 
Inheritance in Java
Elizabeth alexander
 
Inheritance chepter 7
kamal kotecha
 
Inheritance ppt
Nivegeetha
 
Inheritance In Java
Darpan Chelani
 
Csharp4 inheritance
Abed Bukhari
 
javainheritance
Arjun Shanka
 
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
Java inheritance
Arati Gadgil
 
Inheritance in java
yash jain
 
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 

Viewers also liked (14)

PPTX
Lean Concepts & Agile Software Methodologies
Brad Smith
 
PDF
Glossary of Agile Terms
Valtech UK
 
PPTX
Concurrency control
Subhasish Pati
 
PDF
Software development PROCESS
Ivano Malavolta
 
PPTX
Software development methodologies
Ankita Lachhwani
 
PPT
Osi model(open system interconnection)
Dipu Thomas joy
 
PPTX
polymorphism
Imtiaz Hussain
 
PPT
OSI MODEL - A PROJECT
Abhi shreshtha
 
PPTX
Polymorphism
Ahmed Za'anin
 
PPTX
Polymorphism
Sagar K Savale
 
PPTX
Polymorphism
Kumar Gaurav
 
PDF
Polymorphism
Raffaele Doti
 
PPTX
List of Software Development Model and Methods
Riant Soft
 
PPT
OSI Model
Rahul Bandhe
 
Lean Concepts & Agile Software Methodologies
Brad Smith
 
Glossary of Agile Terms
Valtech UK
 
Concurrency control
Subhasish Pati
 
Software development PROCESS
Ivano Malavolta
 
Software development methodologies
Ankita Lachhwani
 
Osi model(open system interconnection)
Dipu Thomas joy
 
polymorphism
Imtiaz Hussain
 
OSI MODEL - A PROJECT
Abhi shreshtha
 
Polymorphism
Ahmed Za'anin
 
Polymorphism
Sagar K Savale
 
Polymorphism
Kumar Gaurav
 
Polymorphism
Raffaele Doti
 
List of Software Development Model and Methods
Riant Soft
 
OSI Model
Rahul Bandhe
 
Ad

Similar to Inheritance polymorphism-in-java (20)

PPTX
Chapter 9 java
Ahmad sohail Kakar
 
PPT
9781111530532 ppt ch10
Terry Yoast
 
PPTX
Chapter 3i
siragezeynu
 
DOCX
Core java by amit
Thakur Amit Tomer
 
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
PDF
Lecture 10
Debasish Pratihari
 
PDF
JAVA UNIT 2 BCA students' notes IPU university
n32310997
 
DOC
116824015 java-j2 ee
homeworkping9
 
PDF
Java - Inheritance Concepts
Victer Paul
 
DOCX
Master of Computer Application (MCA) – Semester 4 MC0078
Aravind NC
 
DOC
Java classes and objects interview questions
Dhivyashree Selvarajtnkpm
 
PDF
Java inheritance
SmrutiShah9
 
PPTX
Chapter 05 polymorphism extra
Nurhanna Aziz
 
PPTX
Chap3 inheritance
raksharao
 
PPT
9781439035665 ppt ch10
Terry Yoast
 
PPTX
inheritance in Java with sample program.pptx
V.V.Vanniaperumal College for Women
 
PDF
Java/J2EE interview Qestions
Arun Vasanth
 
PDF
java_inheritance.pdf
JayMistry91473
 
DOCX
Java Core Parctical
Gaurav Mehta
 
PPT
البرمجة الهدفية بلغة جافا - الوراثة
Mahmoud Alfarra
 
Chapter 9 java
Ahmad sohail Kakar
 
9781111530532 ppt ch10
Terry Yoast
 
Chapter 3i
siragezeynu
 
Core java by amit
Thakur Amit Tomer
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Lecture 10
Debasish Pratihari
 
JAVA UNIT 2 BCA students' notes IPU university
n32310997
 
116824015 java-j2 ee
homeworkping9
 
Java - Inheritance Concepts
Victer Paul
 
Master of Computer Application (MCA) – Semester 4 MC0078
Aravind NC
 
Java classes and objects interview questions
Dhivyashree Selvarajtnkpm
 
Java inheritance
SmrutiShah9
 
Chapter 05 polymorphism extra
Nurhanna Aziz
 
Chap3 inheritance
raksharao
 
9781439035665 ppt ch10
Terry Yoast
 
inheritance in Java with sample program.pptx
V.V.Vanniaperumal College for Women
 
Java/J2EE interview Qestions
Arun Vasanth
 
java_inheritance.pdf
JayMistry91473
 
Java Core Parctical
Gaurav Mehta
 
البرمجة الهدفية بلغة جافا - الوراثة
Mahmoud Alfarra
 
Ad

More from Deepak Singh (20)

PDF
Computer networks - CBSE New Syllabus (083) Class - XII
Deepak Singh
 
PDF
Chpater29 operation-on-file
Deepak Singh
 
PDF
Chapter28 data-file-handling
Deepak Singh
 
PDF
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
 
PDF
Chapter26 inheritance-ii
Deepak Singh
 
PDF
Chapter25 inheritance-i
Deepak Singh
 
PDF
Chapter24 operator-overloading
Deepak Singh
 
PDF
Chapter23 friend-function-friend-class
Deepak Singh
 
PDF
Chapter22 static-class-member-example
Deepak Singh
 
PDF
Chapter21 separate-header-and-implementation-files
Deepak Singh
 
PDF
Chapter20 class-example-program
Deepak Singh
 
PDF
Chapter19 constructor-and-destructor
Deepak Singh
 
PDF
Chapter18 class-and-objects
Deepak Singh
 
PDF
Chapter17 oop
Deepak Singh
 
PDF
Chapter16 pointer
Deepak Singh
 
PDF
Chapter15 structure
Deepak Singh
 
PDF
Chapter13 two-dimensional-array
Deepak Singh
 
PDF
Chapter12 array-single-dimension
Deepak Singh
 
PDF
Chapter 11 Function
Deepak Singh
 
PDF
Chapter 10 Library Function
Deepak Singh
 
Computer networks - CBSE New Syllabus (083) Class - XII
Deepak Singh
 
Chpater29 operation-on-file
Deepak Singh
 
Chapter28 data-file-handling
Deepak Singh
 
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
 
Chapter26 inheritance-ii
Deepak Singh
 
Chapter25 inheritance-i
Deepak Singh
 
Chapter24 operator-overloading
Deepak Singh
 
Chapter23 friend-function-friend-class
Deepak Singh
 
Chapter22 static-class-member-example
Deepak Singh
 
Chapter21 separate-header-and-implementation-files
Deepak Singh
 
Chapter20 class-example-program
Deepak Singh
 
Chapter19 constructor-and-destructor
Deepak Singh
 
Chapter18 class-and-objects
Deepak Singh
 
Chapter17 oop
Deepak Singh
 
Chapter16 pointer
Deepak Singh
 
Chapter15 structure
Deepak Singh
 
Chapter13 two-dimensional-array
Deepak Singh
 
Chapter12 array-single-dimension
Deepak Singh
 
Chapter 11 Function
Deepak Singh
 
Chapter 10 Library Function
Deepak Singh
 

Recently uploaded (20)

PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Introduction to Indian Writing in English
Trushali Dodiya
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Difference between write and update in odoo 18
Celine George
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
infertility, types,causes, impact, and management
Ritu480198
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 

Inheritance polymorphism-in-java

  • 1. INHERITANCE AND POLYMORPHISM IN JAVA Object-Oriented Programming www.beginwithjava.comwww.beginwithjava.com
  • 2. WHAT YOU SHOULD LEARN www.beginwithjava.com   What Is Inheritance?   Calling the Superclass Constructor  Overriding Methods  Protected Member  Multilevel Inheritance   Polymorphism  Abstract Classes and Abstract Method  Interfaces
  • 3. WHAT IS INHERITANCE  Inheritance is the mechanism that allows programmers to create new classes from existing class. By using inheritance programmers can re-use code they've already written.  Any new class that you create from an existing class is called sub class; existing class is called super class. www.beginwithjava.com
  • 5. The complete program of above diagram is available at : www.beginwithjava.com The sub class gets all of the methods and state variables of the super class by default. The sub class can add new methods and state variables.
  • 6. CALLING THE SUPER CLASS CONSTRUCTOR  A subclass can have its own private data members, so a subclass can also have its own constructors.  The constructors of the subclass can initialize only the instance variables of the subclass.   When a subclass object is instantiated the subclass object must also automatically execute one of the constructors of the super class.  To call a super class constructor the super keyword is used. www.beginwithjava.com The complete example program is available at : www.beginwithjava.com
  • 7. OVERRIDING SUPER CLASS METHODS  In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its super class, then the method in the subclass is said to override the method in the super class.   The version of the method defined by the super class will be hidden inside subclass. www.beginwithjava.com
  • 8. The complete example program is available at : www.beginwithjava.com
  • 9. PROTECTED MEMBER  The private members of a class cannot be directly accessed outside the class. Only methods of that class can access the private members directly.  However, sometimes it may be necessary for a subclass to access a private member of a super class.  If you make a private member public, then anyone can access that member. So, if a member of a super class needs to be (directly) accessed in a subclass and yet still prevent its direct access outside the class, you must declare that member protected. www.beginwithjava.com Modifier Class Subclass World public Y Y Y protected Y Y N private Y N N