0% found this document useful (0 votes)
2 views

Inheritance Polymorphisom

The document discusses the design of classes for geometric shapes like circles, rectangles, and triangles using inheritance to avoid redundancy. It introduces a superclass called GeometricObject that contains common properties such as color, filled status, and creation date. The subclasses Circle and Rectangle inherit from GeometricObject and add specific features related to their shapes.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Inheritance Polymorphisom

The document discusses the design of classes for geometric shapes like circles, rectangles, and triangles using inheritance to avoid redundancy. It introduces a superclass called GeometricObject that contains common properties such as color, filled status, and creation date. The subclasses Circle and Rectangle inherit from GeometricObject and add specific features related to their shapes.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Inheritance and Polymorphism

Suppose you will 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.

1
Superclasses 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).
-dateCreated: java.util.Date The date when the object was created.
+GeometricObject() Creates a GeometricObject.
+GeometricObject(color: String, Creates a GeometricObject with the specified color and filled
filled: boolean) values.
+getColor(): String Returns the color.
+setColor(color: String): void Sets a new color. GeometricObject1
+isFilled(): boolean Returns the filled property.
+setFilled(filled: boolean): void Sets a new filled property.
+getDateCreated(): java.util.Date Returns the dateCreated.
+toString(): String Returns a string representation of this object. Circle4

Circle Rectangle
-radius: double -width: double
+Circle() -height: double Rectangle1
+Circle(radius: double) +Rectangle()
+Circle(radius: double, color: String, +Rectangle(width: double, height: double)
filled: boolean) +Rectangle(width: double, height: double
+getRadius(): double color: String, filled: boolean)
+setRadius(radius: double): void +getWidth(): double
TestCircleRectangle
+getArea(): double +setWidth(width: double): void
+getPerimeter(): double +getHeight(): double

Run
+getDiameter(): double +setHeight(height: double): void
+printCircle(): void +getArea(): double
+getPerimeter(): double

2
Motivations
Suppose you will define classes to model circles,
rectangles, and triangles.

These classes have many common features (i.e., color, date created,
filled or not filled, etc.). What is the best way to design these classes
so to avoid redundancy?
*Note that, these classes have many uncommon features too (i.e.,
radius, length, height, etc.).

3
Motivations

The answer is to use inheritance.


The technique is known as code reuse
(to avoid redundancy).

4
More examples

5
More examples

6
Superclasses 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).
-dateCreated: java.util.Date The date when the object was created.
+GeometricObject() Creates a GeometricObject.
+GeometricObject(color: String, Creates a GeometricObject with the specified color and filled
filled: boolean) values.
+getColor(): String Returns the color.
+setColor(color: String): void Sets a new color.
+isFilled(): boolean Returns the filled property.
+setFilled(filled: boolean): void Sets a new filled property.
+getDateCreated(): java.util.Date Returns the dateCreated.
+toString(): String Returns a string representation of this object.

Circle Rectangle
-radius: double -width: double
+Circle() -height: double
+Circle(radius: double) +Rectangle()
+Circle(radius: double, color: String, +Rectangle(width: double, height: double)
filled: boolean) +Rectangle(width: double, height: double
+getRadius(): double color: String, filled: boolean)
+setRadius(radius: double): void +getWidth(): double
+getArea(): double +setWidth(width: double): void
+getPerimeter(): double +getHeight(): double
+getDiameter(): double +setHeight(height: double): void
+printCircle(): void +getArea(): double
+getPerimeter(): double

You might also like