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

PDF
itft-Inheritance in java
Atul Sehdev
 
PPTX
Java inheritance
BHUVIJAYAVELU
 
PPTX
Inheritance
Sapna Sharma
 
PPTX
Inheritance in Java
Tamanna Akter
 
PPTX
Inheritance and Polymorphism Java
M. Raihan
 
PPT
Java: Inheritance
Tareq Hasan
 
PPSX
Seminar on java
shathika
 
itft-Inheritance in java
Atul Sehdev
 
Java inheritance
BHUVIJAYAVELU
 
Inheritance
Sapna Sharma
 
Inheritance in Java
Tamanna Akter
 
Inheritance and Polymorphism Java
M. Raihan
 
Java: Inheritance
Tareq Hasan
 
Seminar on java
shathika
 

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
 
Ad

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
 

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
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PDF
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PDF
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
Understanding operators in c language.pptx
auteharshil95
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 

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