SlideShare uma empresa 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

Mais conteúdo relacionado

Destaque (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
 

Semelhante a Chapter 8.1

Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
PRN USM
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
Terry Yoast
 

Semelhante 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
 

Mais de sotlsoc

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

Mais 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

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
baharayali
 

Último (20)

UEFA Euro 2024 Squad Check-in Which team is Top favorite.docx
UEFA Euro 2024 Squad Check-in Which team is Top favorite.docxUEFA Euro 2024 Squad Check-in Which team is Top favorite.docx
UEFA Euro 2024 Squad Check-in Which team is Top favorite.docx
 
UEFA Euro 2024 Squad Check-in Who is Most Favorite.docx
UEFA Euro 2024 Squad Check-in Who is Most Favorite.docxUEFA Euro 2024 Squad Check-in Who is Most Favorite.docx
UEFA Euro 2024 Squad Check-in Who is Most Favorite.docx
 
European Football Icons that Missed Opportunities at UEFA Euro 2024.docx
European Football Icons that Missed Opportunities at UEFA Euro 2024.docxEuropean Football Icons that Missed Opportunities at UEFA Euro 2024.docx
European Football Icons that Missed Opportunities at UEFA Euro 2024.docx
 
Unveiling the Mystery of Main Bazar Chart
Unveiling the Mystery of Main Bazar ChartUnveiling the Mystery of Main Bazar Chart
Unveiling the Mystery of Main Bazar Chart
 
Technical Data | Sig Sauer Easy6 BDX 1-6x24 | Optics Trade
Technical Data | Sig Sauer Easy6 BDX 1-6x24 | Optics TradeTechnical Data | Sig Sauer Easy6 BDX 1-6x24 | Optics Trade
Technical Data | Sig Sauer Easy6 BDX 1-6x24 | Optics Trade
 
Hire 💕 8617697112 Kasauli Call Girls Service Call Girls Agency
Hire 💕 8617697112 Kasauli Call Girls Service Call Girls AgencyHire 💕 8617697112 Kasauli Call Girls Service Call Girls Agency
Hire 💕 8617697112 Kasauli Call Girls Service Call Girls Agency
 
WhatsApp Chat: 📞 8617697112 Birbhum Call Girl available for hotel room package
WhatsApp Chat: 📞 8617697112 Birbhum  Call Girl available for hotel room packageWhatsApp Chat: 📞 8617697112 Birbhum  Call Girl available for hotel room package
WhatsApp Chat: 📞 8617697112 Birbhum Call Girl available for hotel room package
 
Netherlands Players expected to miss UEFA Euro 2024 due to injury.docx
Netherlands Players expected to miss UEFA Euro 2024 due to injury.docxNetherlands Players expected to miss UEFA Euro 2024 due to injury.docx
Netherlands Players expected to miss UEFA Euro 2024 due to injury.docx
 
Ramban Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts In...
Ramban  Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts In...Ramban  Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts In...
Ramban Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts In...
 
Personal Brand Exploration - By Bradley Dennis
Personal Brand Exploration - By Bradley DennisPersonal Brand Exploration - By Bradley Dennis
Personal Brand Exploration - By Bradley Dennis
 
JORNADA 6 LIGA MURO 2024TUXTEPECOAXACA.pdf
JORNADA 6 LIGA MURO 2024TUXTEPECOAXACA.pdfJORNADA 6 LIGA MURO 2024TUXTEPECOAXACA.pdf
JORNADA 6 LIGA MURO 2024TUXTEPECOAXACA.pdf
 
Muzaffarpur Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Muzaffarpur Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMuzaffarpur Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Muzaffarpur Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Cricket Api Solution.pdfCricket Api Solution.pdf
Cricket Api Solution.pdfCricket Api Solution.pdfCricket Api Solution.pdfCricket Api Solution.pdf
Cricket Api Solution.pdfCricket Api Solution.pdf
 
Albania Vs Spain South American coaches lead Albania to Euro 2024 spot.docx
Albania Vs Spain South American coaches lead Albania to Euro 2024 spot.docxAlbania Vs Spain South American coaches lead Albania to Euro 2024 spot.docx
Albania Vs Spain South American coaches lead Albania to Euro 2024 spot.docx
 
Slovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docx
Slovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docxSlovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docx
Slovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docx
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Italy vs Albania Italy Euro 2024 squad Luciano Spalletti's full team ahead of...
Italy vs Albania Italy Euro 2024 squad Luciano Spalletti's full team ahead of...Italy vs Albania Italy Euro 2024 squad Luciano Spalletti's full team ahead of...
Italy vs Albania Italy Euro 2024 squad Luciano Spalletti's full team ahead of...
 
JORNADA 5 LIGA MURO 2024INSUGURACION.pdf
JORNADA 5 LIGA MURO 2024INSUGURACION.pdfJORNADA 5 LIGA MURO 2024INSUGURACION.pdf
JORNADA 5 LIGA MURO 2024INSUGURACION.pdf
 
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
 
Spain Vs Italy Spain to be banned from participating in Euro 2024.docx
Spain Vs Italy Spain to be banned from participating in Euro 2024.docxSpain Vs Italy Spain to be banned from participating in Euro 2024.docx
Spain Vs Italy Spain to be banned from participating in Euro 2024.docx
 

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