0% found this document useful (0 votes)
23 views13 pages

Chapter 11 - Part1

The document discusses inheritance in Java by using geometric shapes as an example. It explains that a superclass called GeometricObject can be defined to represent common properties of shapes like color and filled. Subclasses Circle and Rectangle can then extend GeometricObject to represent specific shapes, inheriting the common properties while adding their own fields like radius or width/height. This allows code reuse and avoids redundancy between the shape classes.

Uploaded by

ali bzeih
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views13 pages

Chapter 11 - Part1

The document discusses inheritance in Java by using geometric shapes as an example. It explains that a superclass called GeometricObject can be defined to represent common properties of shapes like color and filled. Subclasses Circle and Rectangle can then extend GeometricObject to represent specific shapes, inheriting the common properties while adding their own fields like radius or width/height. This allows code reuse and avoids redundancy between the shape classes.

Uploaded by

ali bzeih
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Chapter 11 Inheritance and Polymorphism

11.2 Super classes and Subclasses

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).

Different classes may have some common properties and


behaviors, which can be generalized in a class that can
be shared by other classes. You can define a specialized
class that extends the generalized class. The specialized
classes inherit the properties (data fields) and methods
from the general class.

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.

A superclass is also referred to as a parent class or a


base class, and a subclass as a child class, an extended
class, or a derived class.

A subclass inherits accessible data fields and methods


from its superclass and may also add new data fields and
methods.

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).

+GeometricObject() Creates a GeometricObject.


+GeometricObject(color: String, Creates a GeometricObject with the specified color and filled values.
filled: boolean)
+getColor(): String Returns the color.
+setColor(color: String): void Sets a new color.
+isFilled(): boolean Returns the filled property.
+setFilled(filled: boolean): void
+toString(): String
Sets a new filled property.
Returns a string representation of this object.
You can download and
run the java project
TestingInheritance.
Circle Rectangle
-radius: double -width: double
+Circle(radius: double) -height: double
+Circle(radius: double, color: String,
filled: boolean) +Rectangle(width: double, height: double)
+getRadius(): double +Rectangle(width: double, height: double
color: String, filled: boolean)
+setRadius(radius: double): void
+getWidth(): double
+area(): double
+setWidth(width: double): void
+perimeter(): double
+getHeight(): double
+ toString(): String
+setHeight(height: double): void
+area(): double
+perimeter(): double
+toString(): String

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 {

public static void main(String[] args) {

Circle c1 = new Circle("red", true, 2);


System.out.println(c1.getColor());
System.out.println(c1.getRadius());
System.out.println(c1);

GeometricObject g1 = new GeometricObject("black", false);


System.out.println(g1.getColor());
// getRadius() will give an error since class GeometricObject does not
// have a method called getRadius()
// System.out.println(g1.getRadius());
System.out.println(g1);

Rectangle r1 = new Rectangle("green", true, 3,4);


System.out.println(r1.getColor());
System.out.println(r1.getHeight());
System.out.println(r1);
}
}

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.

In the subclass you can also:


 Add new data fields
 Add new methods
 Override the methods of the superclass (e.g. The
toString( ) method in class Circle is a modified version
of the toString( ) method in class GeometricObject).
Details about overriding will be provided later.

13

You might also like