Chapter 11 - Part1
Chapter 11 - Part1
1
Introduction
Suppose you want to define classes to model
circles, rectangles, and triangles. These classes
have many common features. What is the best way
to design these classes so to avoid redundancy?
The answer is to use inheritance.
2
11.2 Super classes and Subclasses
Inheritance enables you to define a general class (a
superclass) and later extend it to more specialized
classes (subclasses).
3
11.2 Super classes and Subclasses
In Java terminology, a class C1 extended from another
class C2, is called a subclass, and C2 is called a
superclass.
4
11.2 Super classes and Subclasses
Consider geometric objects. Suppose you want to design
the classes to model geometric objects such as circles
and rectangles. Geometric objects have many common
properties and behaviors. They can be drawn in a certain
color and be filled or unfilled.
Thus a general class GeometricObject (superclass) can
be used to model all geometric objects. This class
contains the properties color and filled and their
appropriate get and set methods.
Assume that this class also contains a toString() method
that returns a string representation of the object.
5
11.2 Super classes and Subclasses
Since a circle is a special type of geometric object, it
shares common properties and methods with other
geometric objects.
Thus it makes sense to define the Circle as a subclass of
GeometricObject. This means that Circle class inherits
from (extends) the GeometricObject class. Likewise,
Rectangle can also be defined as a subclass of
GeometricObject.
The UML diagrams in the next slide show the
relationship among these classes. A triangular arrow
pointing to the superclass is used to denote the
inheritance relationship between the two classes involved.
6
11.2 Super classes and Subclasses
GeometricObject
-color: String The color of the object (default: white).
-filled: boolean Indicates whether the object is filled with a color (default: false).
7
Class GeometricObject
public class GeometricObject {
private String color ;
private boolean filled;
public GeometricObject() { // No arg constructor
color = "white";
filled = false;
}
public GeometricObject(String c, boolean f) {
color = c;
filled = f;
}
public String getColor() {
public String toString() {
return color;
String s = "The color is " + color;
} if (filled)
public void setColor(String c) { s+= ".\nThe geometric object is filled";
color = c; else
} s+= ".\nThe geometric object is not filled";
public boolean isFilled() { return s;
}
return filled;
} // end class GeometricObject
}
public void setFilled(boolean f) {
filled = f;
}
8
Class Circle
public class Circle extends GeometricObject {
private double radius;
public Circle(String c, boolean f, double r) {
setColor(c);
setFilled(f);
radius = r;
}
public double getRadius() { return radius; }
public void setRadius(double radius) {
this.radius = radius;
}
public double area() {
return radius * radius * Math.PI;
}
public double perimeter() {
return 2 * radius * Math.PI;
}
public String toString() {
String s = "The color is " + getColor();
if (isFilled())
s+= ".\nThe circle is filled";
else
s+= ".\nThe circle is not filled";
s+= ".\nThe radius is " + radius;
return s;
}
} // end class Circle 9
Class Rectangle
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle(String c, boolean f,double width, double height)
{
setColor(c);
setFilled(f);
this.width = width;
public double perimeter() {
this.height = height;
return 2 * (width + height);
}
}
public double getWidth() {
return width; public String toString() {
} String s = "The color is " +
public void setWidth(double width) { getColor();
this.width = width; if (isFilled())
} s += ".\nThe rectangle is
public double getHeight() { filled";
return height; else
} s += ".\nThe rectangle is
not filled";
public void setHeight(double height) {
s += ".\nThe height is " + height;
this.height = height;
s += ".\nThe width is " + width;
} return s;
public double area() { }
return width * height; } // end class Rectangle
}
10
Application
public class TestingInheritance {
11
11.2 Super classes and Subclasses
The Circle class inherits all accessible data fields and
methods from the GeometricObject class. In addition, it
has a new data field, radius, and its associated get
and set methods. The Circle class also contains the area( ),
perimeter( ), and toString( ) methods for returning the
area, perimeter, and a string representation of the circle.
The Rectangle class inherits all accessible data fields and
methods from the GeometricObject class. In addition, it
has the data fields width and height and their
associated get and set methods. It also contains the area( ),
perimeter( ), and toString( ) methods for returning the
area, perimeter, and a string representation of the rectangle.
12
11.2 Super classes and Subclasses
As explained in the previous slide a subclass inherits
(extends) data fields and methods from the superclass.
13