SlideShare una empresa de Scribd logo
1 de 14
DEFINING CLASSES FOR
INHERITANCE
Chapter 8.1:
INHERITANCE: What is
Inheritance?
 Inheritance let us to create a new class from the
existing classes.
 The new class is called subclass and the existing
class is called superclass.
 The subclass inherits the properties of the
superclass.
 The properties refer to the method or the attribute
(data)
 Inheritance is ‘is-a’ relation
 Example : if a Circle inherits the Shape class,
hence the Circle is a Shape. (see the next figure)
INHERITANCE: Example
The class Circle and Rectangle are derived from Shape and the class Box
is derived from Rectangle.
Every Circle and every Rectangle is Shape and every Box is a Rectangle
INHERITANCE: When it is used?
 If we need a new class with a same properties or
method of the current class, we do not need to
define a new class.
 A new class might be inherited from the current
class.
INHERITANCE: Types of
Inheritances?
 Single inheritance
 Is a subclass that derived from a single/one superclass
(existing class)
 Multiple inheritance
 Is a subclass that derived from more than one superclass
 Not supported by Java
Geometry
Circle Triangle Square
Sphere Cone Cylinder Cubes
Single Inheritance
Multiple Inheritance
INHERITANCE:
Properties/Characteristics?
 Involve 2 classes :
 Super class.
 Sub class
Rectangle
Box
Super class
Sub class
INHERITANCE: Super class and
Sub class
Rectangle
double length
double width
Box
double height
 Super class – Rectangle
 Sub class – Box
 Attributes for Rectangle : length, width
 Attribute for Box : height
Box class inherits the length and width of the
Rectangle class (superclass)
INHERITANCE: keyword extends
 extends is the keyword to implement inheritance
in Java.
 Syntax
class SubClassName extends SuperClassName
{
// properties & methods
}
 E.g.
class Box extends Rectangle
{
// properties and coding
}
Rectangle
Box
E.g. : Rectangle class
public class Rectangle
{
private double length;
private double width;
public Rectangle()
{
length = 0;
width = 0;
}
public Rectangle(double L, double W)
{
setDimension(L,W);
}
public void setDimension(double L, double W)
{
if (L >= 0)
length = L;
else
length = 0;
if (W >= 0)
width = W;
else
width = 0;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double area()
{
return length * width;
}
public void print()
{
System.out.print(“length = “ + length);
System.out.print(“width = “ + width);
}
} // end for Rectangle class
Rectangle
- length : double
- width : double
+ Rectangle()
+ Rectangle(double,double)
+ setDimension(double,double) : void
+ getLength() : double
+ getWidth() : double
+ area() : double
+ print() : void
The class Rectangle has 9 members
UML class diagram of the Rectangle class
E.g. : Box class
public class Box extends Rectangle
{
private double height;
public Box()
{
super();
height = 0;
}
public Box(double L, double W, double H)
{
super(L,W);
height = H;
}
public void setDimension(double L, double W, double H)
{
setDimension(L,W);
if (H >= 0)
height = H;
else
height = 0;
}
public double getHeight()
{
return height;
}
public double area()
{
return 2 * (getLength() * getWidth()
+ getLength() * height
+ getWidth() * height);
}
public double volume()
{
return super.area() * height;
}
public void print()
{
super.print();
system.out.print(“height = “ + height);
}
} // end for class Box extends
UML class diagram of the class Box
Box
- height : double
- length : double (can’t access directly)
- width : double (cant’ access directly)
+ Box()
+ Box(double,double)
+ setDimension(double,double) : void
+ setDimension(double,double,double) : void
(overloads parent’s setDimension())
+ getLength() : double
+ getWidth() : double
+ getHeight() : double
+ area() : double (overrides parent’s area())
+ volume() : double
+ print() : void (overrides parent’s print())
The class Box has 13 members
 Declaring Arrays and Accessing Array Components
 The class Box is derived from the class Rectangle
 Therefore, all public members of Rectangle are public members of
Box
 The class Box overrides the method print and area
 The class Box has 3 data members : length, width and height
 The instance variable length and width are private members of
the class Rectangle
 So, it cannot be directly accessed in class Box
 Use the super keyword to call a method of the superclass.
 To print the length and width, a reserve word super should be put
into the print method of class Box to call the parent’s print()
method.
 The same case happen in the volume method where super.area()
is being used to determine the base area of box.
 The method area of the class Box determines the surface area of
the box.
 To retrieve the length and width, the methods getLength and
getWidth in class Rectangle is being used.
 Here, the reserve word super is not used because the class Box
does not override the methods getLength and getWidth

Más contenido relacionado

Destacado

Final flipbook presentation
Final flipbook presentationFinal flipbook presentation
Final flipbook presentationTaryn Pilon
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3sotlsoc
 
Chapter 3.2
Chapter 3.2Chapter 3.2
Chapter 3.2sotlsoc
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6sotlsoc
 
Chapter 7.2
Chapter 7.2Chapter 7.2
Chapter 7.2sotlsoc
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1sotlsoc
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3sotlsoc
 
Cyberbullying by alex pitcher
Cyberbullying by alex pitcherCyberbullying by alex pitcher
Cyberbullying by alex pitcherPhillip Pitcher
 
Presentacion leydi perezI
Presentacion leydi perezIPresentacion leydi perezI
Presentacion leydi perezIleydip2009
 
Dangers to social media
Dangers to social mediaDangers to social media
Dangers to social mediaRyan Ward
 
Smart phone. addiction. blindness
Smart phone. addiction. blindnessSmart phone. addiction. blindness
Smart phone. addiction. blindnessSunnyBabyBabe
 

Destacado (11)

Final flipbook presentation
Final flipbook presentationFinal flipbook presentation
Final flipbook presentation
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3
 
Chapter 3.2
Chapter 3.2Chapter 3.2
Chapter 3.2
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
Chapter 7.2
Chapter 7.2Chapter 7.2
Chapter 7.2
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
 
Cyberbullying by alex pitcher
Cyberbullying by alex pitcherCyberbullying by alex pitcher
Cyberbullying by alex pitcher
 
Presentacion leydi perezI
Presentacion leydi perezIPresentacion leydi perezI
Presentacion leydi perezI
 
Dangers to social media
Dangers to social mediaDangers to social media
Dangers to social media
 
Smart phone. addiction. blindness
Smart phone. addiction. blindnessSmart phone. addiction. blindness
Smart phone. addiction. blindness
 

Similar a Chapter 8.1

Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1PRN USM
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceKuntal Bhowmick
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceKuntal Bhowmick
 
RajLec10.ppt
RajLec10.pptRajLec10.ppt
RajLec10.pptRassjb
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritanceNurhanna Aziz
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keywordtanu_jaswal
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in pythontuan vo
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppthenokmetaferia1
 
Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3Narayana Swamy
 

Similar a Chapter 8.1 (20)

Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
 
Inheritance
InheritanceInheritance
Inheritance
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritance
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritance
 
RajLec10.ppt
RajLec10.pptRajLec10.ppt
RajLec10.ppt
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritance
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
10. inheritance
10. inheritance10. inheritance
10. inheritance
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in python
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Java02
Java02Java02
Java02
 
Object and class
Object and classObject and class
Object and class
 
Chap11
Chap11Chap11
Chap11
 
Inheritance
InheritanceInheritance
Inheritance
 
Java unit2
Java unit2Java unit2
Java unit2
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
 
Inheritance
Inheritance Inheritance
Inheritance
 
Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3
 

Más de sotlsoc

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 newsotlsoc
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 newsotlsoc
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 newsotlsoc
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 newsotlsoc
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3sotlsoc
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4sotlsoc
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5sotlsoc
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2sotlsoc
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0sotlsoc
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1sotlsoc
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0sotlsoc
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0sotlsoc
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4sotlsoc
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1sotlsoc
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0sotlsoc
 
Chapter 9.2
Chapter 9.2Chapter 9.2
Chapter 9.2sotlsoc
 
Chapter 8.3
Chapter 8.3Chapter 8.3
Chapter 8.3sotlsoc
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2sotlsoc
 
Chapter 5.2
Chapter 5.2Chapter 5.2
Chapter 5.2sotlsoc
 
Chapter 7.4
Chapter 7.4Chapter 7.4
Chapter 7.4sotlsoc
 

Más de sotlsoc (20)

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 new
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 new
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 new
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0
 
Chapter 9.2
Chapter 9.2Chapter 9.2
Chapter 9.2
 
Chapter 8.3
Chapter 8.3Chapter 8.3
Chapter 8.3
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2
 
Chapter 5.2
Chapter 5.2Chapter 5.2
Chapter 5.2
 
Chapter 7.4
Chapter 7.4Chapter 7.4
Chapter 7.4
 

Último

ALL NFL NETWORK CONTACTS- April 29, 2024
ALL NFL NETWORK CONTACTS- April 29, 2024ALL NFL NETWORK CONTACTS- April 29, 2024
ALL NFL NETWORK CONTACTS- April 29, 2024Brian Slack
 
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改atducpo
 
08448380779 Call Girls In Karol Bagh Women Seeking Men
08448380779 Call Girls In Karol Bagh Women Seeking Men08448380779 Call Girls In Karol Bagh Women Seeking Men
08448380779 Call Girls In Karol Bagh Women Seeking MenDelhi Call girls
 
Albania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docx
Albania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docxAlbania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docx
Albania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docxWorld Wide Tickets And Hospitality
 
Croatia vs Albania Clash of Euro Cup 2024 Squad Preparations and Euro Cup Dre...
Croatia vs Albania Clash of Euro Cup 2024 Squad Preparations and Euro Cup Dre...Croatia vs Albania Clash of Euro Cup 2024 Squad Preparations and Euro Cup Dre...
Croatia vs Albania Clash of Euro Cup 2024 Squad Preparations and Euro Cup Dre...Eticketing.co
 
TAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdf
TAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdfTAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdf
TAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdfSocial Samosa
 
Chennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts servicevipmodelshub1
 
08448380779 Call Girls In IIT Women Seeking Men
08448380779 Call Girls In IIT Women Seeking Men08448380779 Call Girls In IIT Women Seeking Men
08448380779 Call Girls In IIT Women Seeking MenDelhi Call girls
 
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service 🦺
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service  🦺CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service  🦺
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service 🦺anilsa9823
 
Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...
Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...
Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...Neil Horowitz
 
08448380779 Call Girls In Lajpat Nagar Women Seeking Men
08448380779 Call Girls In Lajpat Nagar Women Seeking Men08448380779 Call Girls In Lajpat Nagar Women Seeking Men
08448380779 Call Girls In Lajpat Nagar Women Seeking MenDelhi Call girls
 
Who Is Emmanuel Katto Uganda? His Career, personal life etc.
Who Is Emmanuel Katto Uganda? His Career, personal life etc.Who Is Emmanuel Katto Uganda? His Career, personal life etc.
Who Is Emmanuel Katto Uganda? His Career, personal life etc.Marina Costa
 
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...Eticketing.co
 
08448380779 Call Girls In International Airport Women Seeking Men
08448380779 Call Girls In International Airport Women Seeking Men08448380779 Call Girls In International Airport Women Seeking Men
08448380779 Call Girls In International Airport Women Seeking MenDelhi Call girls
 
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual serviceanilsa9823
 
Tableaux 9ème étape circuit fédéral 2024
Tableaux 9ème étape circuit fédéral 2024Tableaux 9ème étape circuit fédéral 2024
Tableaux 9ème étape circuit fédéral 2024HechemLaameri
 
ppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my Interestppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my InterestNagaissenValaydum
 

Último (20)

Call Girls In Vasundhara 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In Vasundhara 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICECall Girls In Vasundhara 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In Vasundhara 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
 
ALL NFL NETWORK CONTACTS- April 29, 2024
ALL NFL NETWORK CONTACTS- April 29, 2024ALL NFL NETWORK CONTACTS- April 29, 2024
ALL NFL NETWORK CONTACTS- April 29, 2024
 
Call Girls 🫤 Paharganj ➡️ 9999965857 ➡️ Delhi 🫦 Russian Escorts FULL ENJOY
Call Girls 🫤 Paharganj ➡️ 9999965857  ➡️ Delhi 🫦  Russian Escorts FULL ENJOYCall Girls 🫤 Paharganj ➡️ 9999965857  ➡️ Delhi 🫦  Russian Escorts FULL ENJOY
Call Girls 🫤 Paharganj ➡️ 9999965857 ➡️ Delhi 🫦 Russian Escorts FULL ENJOY
 
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
 
08448380779 Call Girls In Karol Bagh Women Seeking Men
08448380779 Call Girls In Karol Bagh Women Seeking Men08448380779 Call Girls In Karol Bagh Women Seeking Men
08448380779 Call Girls In Karol Bagh Women Seeking Men
 
Albania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docx
Albania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docxAlbania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docx
Albania Vs Spain Albania is Loaded with Defensive Talent on their Roster.docx
 
Croatia vs Albania Clash of Euro Cup 2024 Squad Preparations and Euro Cup Dre...
Croatia vs Albania Clash of Euro Cup 2024 Squad Preparations and Euro Cup Dre...Croatia vs Albania Clash of Euro Cup 2024 Squad Preparations and Euro Cup Dre...
Croatia vs Albania Clash of Euro Cup 2024 Squad Preparations and Euro Cup Dre...
 
TAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdf
TAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdfTAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdf
TAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdf
 
Chennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts service
 
08448380779 Call Girls In IIT Women Seeking Men
08448380779 Call Girls In IIT Women Seeking Men08448380779 Call Girls In IIT Women Seeking Men
08448380779 Call Girls In IIT Women Seeking Men
 
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service 🦺
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service  🦺CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service  🦺
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service 🦺
 
Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...
Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...
Atlanta Dream Exec Dan Gadd on Driving Fan Engagement and Growth, Serving the...
 
08448380779 Call Girls In Lajpat Nagar Women Seeking Men
08448380779 Call Girls In Lajpat Nagar Women Seeking Men08448380779 Call Girls In Lajpat Nagar Women Seeking Men
08448380779 Call Girls In Lajpat Nagar Women Seeking Men
 
Who Is Emmanuel Katto Uganda? His Career, personal life etc.
Who Is Emmanuel Katto Uganda? His Career, personal life etc.Who Is Emmanuel Katto Uganda? His Career, personal life etc.
Who Is Emmanuel Katto Uganda? His Career, personal life etc.
 
Call Girls 🫤 Malviya Nagar ➡️ 9999965857 ➡️ Delhi 🫦 Russian Escorts FULL ENJOY
Call Girls 🫤 Malviya Nagar ➡️ 9999965857  ➡️ Delhi 🫦  Russian Escorts FULL ENJOYCall Girls 🫤 Malviya Nagar ➡️ 9999965857  ➡️ Delhi 🫦  Russian Escorts FULL ENJOY
Call Girls 🫤 Malviya Nagar ➡️ 9999965857 ➡️ Delhi 🫦 Russian Escorts FULL ENJOY
 
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
 
08448380779 Call Girls In International Airport Women Seeking Men
08448380779 Call Girls In International Airport Women Seeking Men08448380779 Call Girls In International Airport Women Seeking Men
08448380779 Call Girls In International Airport Women Seeking Men
 
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service
 
Tableaux 9ème étape circuit fédéral 2024
Tableaux 9ème étape circuit fédéral 2024Tableaux 9ème étape circuit fédéral 2024
Tableaux 9ème étape circuit fédéral 2024
 
ppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my Interestppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my Interest
 

Chapter 8.1

  • 2. INHERITANCE: What is Inheritance?  Inheritance let us to create a new class from the existing classes.  The new class is called subclass and the existing class is called superclass.  The subclass inherits the properties of the superclass.  The properties refer to the method or the attribute (data)  Inheritance is ‘is-a’ relation  Example : if a Circle inherits the Shape class, hence the Circle is a Shape. (see the next figure)
  • 3. INHERITANCE: Example The class Circle and Rectangle are derived from Shape and the class Box is derived from Rectangle. Every Circle and every Rectangle is Shape and every Box is a Rectangle
  • 4. INHERITANCE: When it is used?  If we need a new class with a same properties or method of the current class, we do not need to define a new class.  A new class might be inherited from the current class.
  • 5. INHERITANCE: Types of Inheritances?  Single inheritance  Is a subclass that derived from a single/one superclass (existing class)  Multiple inheritance  Is a subclass that derived from more than one superclass  Not supported by Java
  • 6. Geometry Circle Triangle Square Sphere Cone Cylinder Cubes Single Inheritance Multiple Inheritance
  • 7. INHERITANCE: Properties/Characteristics?  Involve 2 classes :  Super class.  Sub class Rectangle Box Super class Sub class
  • 8. INHERITANCE: Super class and Sub class Rectangle double length double width Box double height  Super class – Rectangle  Sub class – Box  Attributes for Rectangle : length, width  Attribute for Box : height Box class inherits the length and width of the Rectangle class (superclass)
  • 9. INHERITANCE: keyword extends  extends is the keyword to implement inheritance in Java.  Syntax class SubClassName extends SuperClassName { // properties & methods }  E.g. class Box extends Rectangle { // properties and coding } Rectangle Box
  • 10. E.g. : Rectangle class public class Rectangle { private double length; private double width; public Rectangle() { length = 0; width = 0; } public Rectangle(double L, double W) { setDimension(L,W); } public void setDimension(double L, double W) { if (L >= 0) length = L; else length = 0; if (W >= 0) width = W; else width = 0; } public double getLength() { return length; } public double getWidth() { return width; } public double area() { return length * width; } public void print() { System.out.print(“length = “ + length); System.out.print(“width = “ + width); } } // end for Rectangle class
  • 11. Rectangle - length : double - width : double + Rectangle() + Rectangle(double,double) + setDimension(double,double) : void + getLength() : double + getWidth() : double + area() : double + print() : void The class Rectangle has 9 members UML class diagram of the Rectangle class
  • 12. E.g. : Box class public class Box extends Rectangle { private double height; public Box() { super(); height = 0; } public Box(double L, double W, double H) { super(L,W); height = H; } public void setDimension(double L, double W, double H) { setDimension(L,W); if (H >= 0) height = H; else height = 0; } public double getHeight() { return height; } public double area() { return 2 * (getLength() * getWidth() + getLength() * height + getWidth() * height); } public double volume() { return super.area() * height; } public void print() { super.print(); system.out.print(“height = “ + height); } } // end for class Box extends
  • 13. UML class diagram of the class Box Box - height : double - length : double (can’t access directly) - width : double (cant’ access directly) + Box() + Box(double,double) + setDimension(double,double) : void + setDimension(double,double,double) : void (overloads parent’s setDimension()) + getLength() : double + getWidth() : double + getHeight() : double + area() : double (overrides parent’s area()) + volume() : double + print() : void (overrides parent’s print()) The class Box has 13 members
  • 14.  Declaring Arrays and Accessing Array Components  The class Box is derived from the class Rectangle  Therefore, all public members of Rectangle are public members of Box  The class Box overrides the method print and area  The class Box has 3 data members : length, width and height  The instance variable length and width are private members of the class Rectangle  So, it cannot be directly accessed in class Box  Use the super keyword to call a method of the superclass.  To print the length and width, a reserve word super should be put into the print method of class Box to call the parent’s print() method.  The same case happen in the volume method where super.area() is being used to determine the base area of box.  The method area of the class Box determines the surface area of the box.  To retrieve the length and width, the methods getLength and getWidth in class Rectangle is being used.  Here, the reserve word super is not used because the class Box does not override the methods getLength and getWidth