SlideShare a Scribd company logo
1 of 25
Download to read offline
Inheritance
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
2
Inheritance
 Inheritance can be defined as the process where one class
acquires the properties (methods and fields) of another.
 The class which inherits the properties of other is known
as subclass (derived class, child class) and the class whose
properties are inherited is known as superclass (base
class, parent class).
 Therefore, a subclass is a specialized version of a
superclass.
 It inherits all of the instance variables and methods
defined by the superclass and adds its own, unique
elements.
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
3
Important terminologies
 Super Class: The class whose features are inherited.
 Sub Class: The class that inherits the other class.
 The subclass can add its own fields and methods in addition to the
superclass fields and methods.
 Reusability: Inheritance supports the concept of
“reusability”,
 i.e. when we want to create a new class and there is already a
class that includes some of the code that we want, we can derive
our new class from the existing class. By doing this, we are reusing
the fields and methods of the existing class.
 To inherit from a class, use the extends keyword.
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
4
Syntax
 extends is the keyword used to inherit the properties of a
class.
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
5
Syntax
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
6
Inheritance Demo
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
7
Inheritance Demo
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Try it!!
 Although a subclass includes all of the members of its
superclass, it cannot access those members of the
superclass that have been declared as private.
 The final Keyword
 If you don't want other classes to inherit from a class, use the
final keyword.
8Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Using super to Call Superclass
 A subclass can call a constructor defined by its superclass
by use of the following form of super:
 arg-list specifies any arguments needed by the constructor
in the superclass.
 super() must always be the first statement executed
inside a subclass’ constructor.
9Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Using super to Call Superclass
10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Using super to access members
 A subclass can access the data members and methods of
superclass by use of the following form of super:
 member can be either a method or an instance variable.
 This second form of super is most applicable to situations
in which member names of a subclass hide (same name)
members by the same name in the superclass
11Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Using super to access members
12Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Types of Inheritance
13Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Single Inheritance
 When a class inherits another class, it is known as a single inheritance.
14Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Multi level Inheritance
 When there is a chain of inheritance, it is known as multilevel inheritance.
15Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Hierarchical Inheritance
 When two or more classes inherits a single class, it is hierarchical inheritance
16Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Multiple Inheritance
 To reduce the complexity and simplify the language, multiple
inheritance is not supported in java.
 Consider a scenario where A, B, and C are three classes.
 The C class inherits A and B classes.
 If A and B classes have the same method and you call it from child
class object, there will be ambiguity to call the method of A or B
class.
 In java, we can achieve multiple inheritance only through Interfaces.
17Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Method Overriding
 In a class hierarchy, when a method in a subclass has the same name
and type signature as a method in its superclass,
 then the method in the subclass is said to override the method in the
superclass.
 When an overridden method is called from within a subclass, it will
always refer to the version of that method defined by the subclass.
 The version of the method defined by the superclass will be hidden.
18Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Method Overriding
How to call the show()
method of Class A????
Two possible ways!!!!!
19Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Final Keyword
 The keyword final has three uses.
 First, it can be used to create the equivalent of a named
constant.
 The other two uses of final apply to inheritance.
 Using final to Prevent Overriding
 Using final to Prevent Inheritance
20Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Final Keyword
21Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Important facts about inheritance
 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.
 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 for accessing its private fields, these can also be
used by the subclass.
22Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
The instanceof Keyword
 The java instanceof
operator is used to test
whether the object is an
instance of the specified
type (class or subclass or
interface).
 The instanceof in java is
also known as type
comparison operator
because it compares the
instance with type. It
returns either true or
false.
23Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
The Object Class
 There is one special class, Object, defined by Java.
 All other classes are subclasses of Object.
 That is, Object is a superclass of all other classes. This
means that a reference variable of type
 Object can refer to an object of any other class.
Check what are the methods associated with Object class!!!!
24Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
The End…
25Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam

More Related Content

What's hot

Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questionsnishajj
 
Java classes and objects interview questions
Java classes and objects interview questionsJava classes and objects interview questions
Java classes and objects interview questionsDhivyashree Selvarajtnkpm
 
Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaRavi_Kant_Sahu
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritanceadil raja
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceEng Teong Cheah
 
EEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answerEEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answerJeba Moses
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationHoneyChintal
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingArslan Waseem
 
Final presentation programming
Final presentation programmingFinal presentation programming
Final presentation programminghaider ali
 

What's hot (17)

Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Java classes and objects interview questions
Java classes and objects interview questionsJava classes and objects interview questions
Java classes and objects interview questions
 
Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in Java
 
OOPs Lecture 2
OOPs Lecture 2OOPs Lecture 2
OOPs Lecture 2
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
EEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answerEEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answer
 
Packages
PackagesPackages
Packages
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Final presentation programming
Final presentation programmingFinal presentation programming
Final presentation programming
 

Similar to Java - Inheritance Concepts

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 inheritanceJava inheritance
Java inheritanceSmrutiShah9
 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Designİbrahim Kürce
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
 
Chapter 05 polymorphism extra
Chapter 05 polymorphism extraChapter 05 polymorphism extra
Chapter 05 polymorphism extraNurhanna Aziz
 
البرمجة الهدفية بلغة جافا - الوراثة
البرمجة الهدفية بلغة جافا - الوراثةالبرمجة الهدفية بلغة جافا - الوراثة
البرمجة الهدفية بلغة جافا - الوراثةMahmoud Alfarra
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritanceraksharao
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 
Mca 2nd sem u-3 inheritance
Mca 2nd  sem u-3 inheritanceMca 2nd  sem u-3 inheritance
Mca 2nd sem u-3 inheritanceRai University
 
Bca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritanceBca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritanceRai University
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078Aravind NC
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism JavaM. Raihan
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdfWaqarRaj1
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxRudranilDas11
 

Similar to Java - Inheritance Concepts (20)

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
 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Design
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Chapter 05 polymorphism extra
Chapter 05 polymorphism extraChapter 05 polymorphism extra
Chapter 05 polymorphism extra
 
البرمجة الهدفية بلغة جافا - الوراثة
البرمجة الهدفية بلغة جافا - الوراثةالبرمجة الهدفية بلغة جافا - الوراثة
البرمجة الهدفية بلغة جافا - الوراثة
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritance
 
Chapter 9 java
Chapter 9 javaChapter 9 java
Chapter 9 java
 
Chapter 3i
Chapter 3iChapter 3i
Chapter 3i
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Mca 2nd sem u-3 inheritance
Mca 2nd  sem u-3 inheritanceMca 2nd  sem u-3 inheritance
Mca 2nd sem u-3 inheritance
 
Bca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritanceBca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritance
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 

More from Victer Paul

OOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - LabOOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - LabVicter Paul
 
OOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - LabOOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - LabVicter Paul
 
OOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation ConceptsOOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation ConceptsVicter Paul
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings ConceptsVicter Paul
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages ConceptsVicter Paul
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java BasicsVicter Paul
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling ConceptsVicter Paul
 
Java - Class Structure
Java - Class StructureJava - Class Structure
Java - Class StructureVicter Paul
 
Java - Object Oriented Programming Concepts
Java - Object Oriented Programming ConceptsJava - Object Oriented Programming Concepts
Java - Object Oriented Programming ConceptsVicter Paul
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic ConceptsVicter Paul
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output ConceptsVicter Paul
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays ConceptsVicter Paul
 
Java applet programming concepts
Java  applet programming conceptsJava  applet programming concepts
Java applet programming conceptsVicter Paul
 

More from Victer Paul (13)

OOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - LabOOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - Lab
 
OOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - LabOOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - Lab
 
OOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation ConceptsOOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation Concepts
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java Basics
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Java - Class Structure
Java - Class StructureJava - Class Structure
Java - Class Structure
 
Java - Object Oriented Programming Concepts
Java - Object Oriented Programming ConceptsJava - Object Oriented Programming Concepts
Java - Object Oriented Programming Concepts
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic Concepts
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays Concepts
 
Java applet programming concepts
Java  applet programming conceptsJava  applet programming concepts
Java applet programming concepts
 

Recently uploaded

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 

Recently uploaded (20)

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 

Java - Inheritance Concepts

  • 1. Inheritance Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 2. 2 Inheritance  Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another.  The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).  Therefore, a subclass is a specialized version of a superclass.  It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements. Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 3. 3 Important terminologies  Super Class: The class whose features are inherited.  Sub Class: The class that inherits the other class.  The subclass can add its own fields and methods in addition to the superclass fields and methods.  Reusability: Inheritance supports the concept of “reusability”,  i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.  To inherit from a class, use the extends keyword. Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 4. 4 Syntax  extends is the keyword used to inherit the properties of a class. Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 5. 5 Syntax Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 6. 6 Inheritance Demo Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 7. 7 Inheritance Demo Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 8. Try it!!  Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private.  The final Keyword  If you don't want other classes to inherit from a class, use the final keyword. 8Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 9. Using super to Call Superclass  A subclass can call a constructor defined by its superclass by use of the following form of super:  arg-list specifies any arguments needed by the constructor in the superclass.  super() must always be the first statement executed inside a subclass’ constructor. 9Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 10. Using super to Call Superclass 10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 11. Using super to access members  A subclass can access the data members and methods of superclass by use of the following form of super:  member can be either a method or an instance variable.  This second form of super is most applicable to situations in which member names of a subclass hide (same name) members by the same name in the superclass 11Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 12. Using super to access members 12Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 13. Types of Inheritance 13Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 14. Single Inheritance  When a class inherits another class, it is known as a single inheritance. 14Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 15. Multi level Inheritance  When there is a chain of inheritance, it is known as multilevel inheritance. 15Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 16. Hierarchical Inheritance  When two or more classes inherits a single class, it is hierarchical inheritance 16Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 17. Multiple Inheritance  To reduce the complexity and simplify the language, multiple inheritance is not supported in java.  Consider a scenario where A, B, and C are three classes.  The C class inherits A and B classes.  If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class.  In java, we can achieve multiple inheritance only through Interfaces. 17Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 18. Method Overriding  In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass,  then the method in the subclass is said to override the method in the superclass.  When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass.  The version of the method defined by the superclass will be hidden. 18Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 19. Method Overriding How to call the show() method of Class A???? Two possible ways!!!!! 19Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 20. Final Keyword  The keyword final has three uses.  First, it can be used to create the equivalent of a named constant.  The other two uses of final apply to inheritance.  Using final to Prevent Overriding  Using final to Prevent Inheritance 20Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 21. Final Keyword 21Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 22. Important facts about inheritance  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.  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 for accessing its private fields, these can also be used by the subclass. 22Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 23. The instanceof Keyword  The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface).  The instanceof in java is also known as type comparison operator because it compares the instance with type. It returns either true or false. 23Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 24. The Object Class  There is one special class, Object, defined by Java.  All other classes are subclasses of Object.  That is, Object is a superclass of all other classes. This means that a reference variable of type  Object can refer to an object of any other class. Check what are the methods associated with Object class!!!! 24Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 25. The End… 25Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam