SlideShare a Scribd company logo
1 of 21
Programming in Java
Lecture 7: Inheritance
Contents
• Inheritance
• Method Overloading
• Method Overriding
• Dynamic Method Dispatch
Introduction
• Object-oriented programming allows classes to inherit commonly
used state and behavior from other classes.
• In the Java programming language, each class is allowed to have one
direct super-class, and each super-class has the potential for an
unlimited number of subclasses.
• Inheritance is a fundamental object-oriented design technique used to
create and organize reusable classes.
Inheritance
• Inheritance defines an is-a relationship between a super-
class and its subclasses. It means that the subclass (child) is a
more specific version of the super-class (parent).
• An object of a subclass can be used wherever an object of the
super-class can be used.
• Inheritance is used to build new classes from existing classes.
• The inheritance relationship is transitive: if class y extends
class x, and a class z extends class y, then z will also inherit
from class x.
Inheritance
 Using inheritance, we can create a general class that defines traits(state
and behaviors) common to a set of related items.
 This class can then be inherited by other, more specific classes, each
adding those things that are unique to it.
 In Java, a class that is inherited is called a super-class.
 The class that does the inheriting is called a subclass.
 A Subclass inherits all of the instance variables and methods defined by
the super-class and adds its own, unique elements.
Inheritance Example
class Shape {
int area(){…}
}
class Rectangle extends Shape{
int area() { area = length * width;}
int length; int width;
}
class Square extends Rectangle {
int area() {…}
int length; int width = length;
Key Points
• Private members of the super-class are inherited but they can not be
accessed by the subclass directly.
• Members that have default accessibility in the super-class are also not
inherited by subclasses in other packages.
• Constructors and initializer blocks are not inherited by a subclass.
• A subclass can extend only one super-class.
Types of Inheritance
The following kinds of inheritance are there in java.
 Simple Inheritance
 Multilevel Inheritance
 Simple Inheritance: A subclass is derived simply from it's parent
class.
 There is only a sub class and it's parent class. It is also called single
inheritance or one level inheritance.
 Multi-level Inheritance: A subclass is derived from a derived class.
Multiple Inheritance
• The mechanism of inheriting the features of more than one base class
into a single class is known as multiple inheritance.
• Java does not support multiple inheritance but the multiple
inheritance can be achieved by using the interface.
• In Java Multiple Inheritance can be achieved through use of
Interfaces by implementing more than one interfaces in a class.
Method Overloading
 Method overloading means having two or more methods with the
same name but different signatures in the same scope.
 These two methods may exist in the same class or one in base class
and another in derived class.
 It allows creating several methods with the same name which differ
from each other in the type of the input and the output of the method.
 It is simply defined as the ability of one method to perform different
tasks.
Example
class Area11
{
void area(int a)
{
int area = a*a;
System.out.println("area of square is:" + area);
}
void area (int a, int b)
{
int area = a*b;
System.out.println("area of rectangle is:" + area);
}
}
class OverloadDemo
{
public static void main (String arr[])
{
Area11 ar= new Area11();
ar.area(10);
ar.area(10,5);
}
}
Method Overriding
• Method overriding means having a different
implementation of the same method in the inherited class.
• These two methods would have the same signature, but
different implementation.
• One of these would exist in the base class and another in the
derived class. These cannot exist in the same class.
•The version of a method that is executed will be
determined by the object that is used to invoke it.
•If an object of a parent class is used to invoke the
method, then the version in the parent class will be
executed.
•If an object of the subclass is used to invoke the
method, then the version in the child class will be
executed.
class Override
{
public void display()
{
System.out.println("Hello...This is superclass display");
}
}
class Override1 extends Override
{
public void display()
{
System.out.println("Hi...This is overriden method in subclass");
}
}
class OverrideDemo
{
public static void main(String arr[])
{
Override o = new Override();
o.display();
Override1 o1 = new Override1();
o1.display();
}
}
Dynamic Method Dispatch
• Dynamic method dispatch is the mechanism by which a call to an
overridden method is resolved at run time, rather than compile time.
• Dynamic method dispatch is important because this is how Java
implements run-time polymorphism.
• class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
•
class B extends A {
void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A {
void callme() {
System.out.println("Inside C's callme method");
}
}
class Dispatch {
public static void main(String args[]) {
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
r = c; // r refers to a C object
r.callme(); // calls C's version of callme
}
}
L7 inheritance

More Related Content

What's hot

Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructorsJan Niño Acierto
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritanceKalai Selvi
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in javaAtul Sehdev
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphismmcollison
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPTPooja Jaiswal
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingShivam Singhal
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#foreverredpb
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
 
Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - InheritanceOum Saokosal
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword pptVinod Kumar
 

What's hot (20)

Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
javainheritance
javainheritancejavainheritance
javainheritance
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 
Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - Inheritance
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Java Methods
Java MethodsJava Methods
Java Methods
 
Java keywords
Java keywordsJava keywords
Java keywords
 

Viewers also liked

Lili, MA resume
Lili, MA resumeLili, MA resume
Lili, MA resumeLili Ma
 
Daniela
DanielaDaniela
Daniela951118
 
Excelsior owners welfare association white paper
Excelsior owners welfare association   white paperExcelsior owners welfare association   white paper
Excelsior owners welfare association white papernehaj8260
 
Paper2_CSE6331_Vivek_1001053883
Paper2_CSE6331_Vivek_1001053883Paper2_CSE6331_Vivek_1001053883
Paper2_CSE6331_Vivek_1001053883Vivek Sharma
 
Publicacion # 4 Gestion visual
Publicacion # 4 Gestion visualPublicacion # 4 Gestion visual
Publicacion # 4 Gestion visualproalnet
 
Mapping in Support of Law of the Sea
Mapping in Support of Law of the SeaMapping in Support of Law of the Sea
Mapping in Support of Law of the SeaLarry Mayer
 
Kins origin malware with unique ATSEngine.
Kins origin malware with unique ATSEngine.Kins origin malware with unique ATSEngine.
Kins origin malware with unique ATSEngine.Senad Aruc
 
Cell structure
Cell structureCell structure
Cell structureSuni Pm
 
Факультет социальной работы, педагогики и психологии ПИ ВоГУ
Факультет социальной работы, педагогики и психологии ПИ ВоГУФакультет социальной работы, педагогики и психологии ПИ ВоГУ
Факультет социальной работы, педагогики и психологии ПИ ВоГУlabdua
 
Recurrent vulvovaginal Candidiasis
Recurrent vulvovaginal CandidiasisRecurrent vulvovaginal Candidiasis
Recurrent vulvovaginal CandidiasisAboubakr Elnashar
 

Viewers also liked (13)

Lili, MA resume
Lili, MA resumeLili, MA resume
Lili, MA resume
 
Daniela
DanielaDaniela
Daniela
 
Fire Safety Certificate of Completion.PDF
Fire Safety Certificate of Completion.PDFFire Safety Certificate of Completion.PDF
Fire Safety Certificate of Completion.PDF
 
Excelsior owners welfare association white paper
Excelsior owners welfare association   white paperExcelsior owners welfare association   white paper
Excelsior owners welfare association white paper
 
October_FP&A-Foresights
October_FP&A-ForesightsOctober_FP&A-Foresights
October_FP&A-Foresights
 
Paper2_CSE6331_Vivek_1001053883
Paper2_CSE6331_Vivek_1001053883Paper2_CSE6331_Vivek_1001053883
Paper2_CSE6331_Vivek_1001053883
 
Publicacion # 4 Gestion visual
Publicacion # 4 Gestion visualPublicacion # 4 Gestion visual
Publicacion # 4 Gestion visual
 
Mapping in Support of Law of the Sea
Mapping in Support of Law of the SeaMapping in Support of Law of the Sea
Mapping in Support of Law of the Sea
 
Kins origin malware with unique ATSEngine.
Kins origin malware with unique ATSEngine.Kins origin malware with unique ATSEngine.
Kins origin malware with unique ATSEngine.
 
Entropie delhi profile
Entropie delhi profileEntropie delhi profile
Entropie delhi profile
 
Cell structure
Cell structureCell structure
Cell structure
 
Факультет социальной работы, педагогики и психологии ПИ ВоГУ
Факультет социальной работы, педагогики и психологии ПИ ВоГУФакультет социальной работы, педагогики и психологии ПИ ВоГУ
Факультет социальной работы, педагогики и психологии ПИ ВоГУ
 
Recurrent vulvovaginal Candidiasis
Recurrent vulvovaginal CandidiasisRecurrent vulvovaginal Candidiasis
Recurrent vulvovaginal Candidiasis
 

Similar to L7 inheritance (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
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Inheritance
InheritanceInheritance
Inheritance
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
 
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)
 
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUEITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
 
Unit 4
Unit 4Unit 4
Unit 4
 
Java - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptxJava - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptx
 
Java presentation
Java presentationJava presentation
Java presentation
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritance
 
Core java oop
Core java oopCore java oop
Core java oop
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 

More from teach4uin

Master pages
Master pagesMaster pages
Master pagesteach4uin
 
.Net framework
.Net framework.Net framework
.Net frameworkteach4uin
 
Scripting languages
Scripting languagesScripting languages
Scripting languagesteach4uin
 
State management
State managementState management
State managementteach4uin
 
security configuration
security configurationsecurity configuration
security configurationteach4uin
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tagsteach4uin
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tagsteach4uin
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentationteach4uin
 
.Net overview
.Net overview.Net overview
.Net overviewteach4uin
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lessonteach4uin
 
storage clas
storage classtorage clas
storage clasteach4uin
 

More from teach4uin (20)

Controls
ControlsControls
Controls
 
validation
validationvalidation
validation
 
validation
validationvalidation
validation
 
Master pages
Master pagesMaster pages
Master pages
 
.Net framework
.Net framework.Net framework
.Net framework
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
 
Css1
Css1Css1
Css1
 
Code model
Code modelCode model
Code model
 
Asp db
Asp dbAsp db
Asp db
 
State management
State managementState management
State management
 
security configuration
security configurationsecurity configuration
security configuration
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tags
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tags
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentation
 
.Net overview
.Net overview.Net overview
.Net overview
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
 
enums
enumsenums
enums
 
memory
memorymemory
memory
 
array
arrayarray
array
 
storage clas
storage classtorage clas
storage clas
 

Recently uploaded

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
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
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
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
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
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
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
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
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
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
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 

L7 inheritance

  • 2. Contents • Inheritance • Method Overloading • Method Overriding • Dynamic Method Dispatch
  • 3. Introduction • Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. • In the Java programming language, each class is allowed to have one direct super-class, and each super-class has the potential for an unlimited number of subclasses. • Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes.
  • 4. Inheritance • Inheritance defines an is-a relationship between a super- class and its subclasses. It means that the subclass (child) is a more specific version of the super-class (parent). • An object of a subclass can be used wherever an object of the super-class can be used. • Inheritance is used to build new classes from existing classes. • The inheritance relationship is transitive: if class y extends class x, and a class z extends class y, then z will also inherit from class x.
  • 5. Inheritance  Using inheritance, we can create a general class that defines traits(state and behaviors) common to a set of related items.  This class can then be inherited by other, more specific classes, each adding those things that are unique to it.  In Java, a class that is inherited is called a super-class.  The class that does the inheriting is called a subclass.  A Subclass inherits all of the instance variables and methods defined by the super-class and adds its own, unique elements.
  • 6.
  • 7. Inheritance Example class Shape { int area(){…} } class Rectangle extends Shape{ int area() { area = length * width;} int length; int width; } class Square extends Rectangle { int area() {…} int length; int width = length;
  • 8. Key Points • Private members of the super-class are inherited but they can not be accessed by the subclass directly. • Members that have default accessibility in the super-class are also not inherited by subclasses in other packages. • Constructors and initializer blocks are not inherited by a subclass. • A subclass can extend only one super-class.
  • 9. Types of Inheritance The following kinds of inheritance are there in java.  Simple Inheritance  Multilevel Inheritance  Simple Inheritance: A subclass is derived simply from it's parent class.  There is only a sub class and it's parent class. It is also called single inheritance or one level inheritance.  Multi-level Inheritance: A subclass is derived from a derived class.
  • 10. Multiple Inheritance • The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance. • Java does not support multiple inheritance but the multiple inheritance can be achieved by using the interface. • In Java Multiple Inheritance can be achieved through use of Interfaces by implementing more than one interfaces in a class.
  • 11. Method Overloading  Method overloading means having two or more methods with the same name but different signatures in the same scope.  These two methods may exist in the same class or one in base class and another in derived class.  It allows creating several methods with the same name which differ from each other in the type of the input and the output of the method.  It is simply defined as the ability of one method to perform different tasks.
  • 12. Example class Area11 { void area(int a) { int area = a*a; System.out.println("area of square is:" + area); } void area (int a, int b) { int area = a*b; System.out.println("area of rectangle is:" + area); } }
  • 13. class OverloadDemo { public static void main (String arr[]) { Area11 ar= new Area11(); ar.area(10); ar.area(10,5); } }
  • 14. Method Overriding • Method overriding means having a different implementation of the same method in the inherited class. • These two methods would have the same signature, but different implementation. • One of these would exist in the base class and another in the derived class. These cannot exist in the same class.
  • 15. •The version of a method that is executed will be determined by the object that is used to invoke it. •If an object of a parent class is used to invoke the method, then the version in the parent class will be executed. •If an object of the subclass is used to invoke the method, then the version in the child class will be executed.
  • 16. class Override { public void display() { System.out.println("Hello...This is superclass display"); } } class Override1 extends Override { public void display() { System.out.println("Hi...This is overriden method in subclass"); } }
  • 17. class OverrideDemo { public static void main(String arr[]) { Override o = new Override(); o.display(); Override1 o1 = new Override1(); o1.display(); } }
  • 18. Dynamic Method Dispatch • Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. • Dynamic method dispatch is important because this is how Java implements run-time polymorphism.
  • 19. • class A { void callme() { System.out.println("Inside A's callme method"); } } • class B extends A { void callme() { System.out.println("Inside B's callme method"); } } class C extends A { void callme() { System.out.println("Inside C's callme method"); } }
  • 20. class Dispatch { public static void main(String args[]) { A a = new A(); // object of type A B b = new B(); // object of type B C c = new C(); // object of type C A r; // obtain a reference of type A r = a; // r refers to an A object r.callme(); // calls A's version of callme r = b; // r refers to a B object r.callme(); // calls B's version of callme r = c; // r refers to a C object r.callme(); // calls C's version of callme } }