View
111
Download
1
Category
Preview:
Citation preview
DEFINING CLASSES FOR
INHERITANCE
Chapter 8.1:
INHERITANCE: What is
Inheritance?
Inheritance let us to create a new class from theexisting classes.
The new class is called subclass and the existingclass is called superclass.
The subclass inherits the properties of thesuperclass.
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 inheritancein Java.
Syntax
class SubClassName extends SuperClassName
{
// properties & methods}
E.g.
class Box extends Rectangle
{
// properties and coding}
Rectangle
Box
E.g. : Rectangle classpublic 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
Recommended