OOP Lab4
OOP Lab4
1. Objective
• Understand polymorphism, abstraction in OOP
• Implement polymorphism, abstraction in OOP
2. Literature
2.1. Polymorphism
• Polymorphism is the ability of an object to take on many forms. The most common use
of polymorphism in OOP occurs when a parent class reference is used to refer to a child
class object.
• In this section, we will show you how the behavior of overridden methods in Java allows
you to take advantage of polymorphism when designing your classes.
• In the following example, we have two real objects, which are Rectangle and Triangle,
and a general object Shape. In reality, we don't need to create a Shape object, since it
does not have any behavior. The question is how can we prevent users from creating
Shape object.
• We have two approaches, the first one is taking advantage of interface and the second
is using abstract class
1
Thu Dau Mot University Object Oriented Programming
// Shape.java
public interface Shape {
public double getArea();
}
// Rectangle.java
public class Rectangle implements Shape {
private double length;
private double width;
public Rectangle() {
this.length = 0.0;
this.width = 0.0;
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getLength() {
return this.length;
}
public double getWidth() {
return this.width;
}
public void setLength(double length) {
this.length = length;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public double getArea() {
return this.length * this.width;
}
public double getPerimeter() {
return (this.length + this.width) * 2.0;
}
public boolean equals(Object obj) {
2
Thu Dau Mot University Object Oriented Programming
// Test.java
public class Test {
public static void main(String[] args) {
Shape s = new Rectangle(3, 4);
System.out.println(s.toString());
System.out.println("Area = " + s.getArea());
s = new Triangle(4, 5);
System.out.println(s.toString());
System.out.println("Area = " + s.getArea());
}
}
2.2. Abstraction
• Abstraction is process of hiding the implementation details from the user, only the
functionality will be provided to the user. For example, in email system, to send an
email, user need only to provide recipient email, the content, and click send. All
implementation of the system is hidden.
• Java provides a mechanism to allow a program to achieve abstraction, by using abstract
keyword. The abstract keyword can be used for class and method definition. For
example, in the following diagram, we will define an abstract class, Shape, which
contains color attribute and getArea() behavior. That means, every derived object from
Shape will have color information and getArea() method.
• You should notice that if you define an abstract class, the method must be either an
abstract method or an implemented method. An abstract class cannot be instantiated.
This usually
3
Thu Dau Mot University Object Oriented Programming
// Shape.java
public abstract class Shape {
protected String color;
public Shape() {
this.color = "";
}
// Rectangle.java
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle() {
super();
this.length = 0;
this.width = 0;
}
@Override
public double getArea() {
return this. length * this.width;
}
4
Thu Dau Mot University Object Oriented Programming
}
}
// Test.java
public class Test {
public static void main(String[] args) {
Shape s = new Rectangle(3, 4);
System.out.println(s.toString());
System.out.println("Area = " + s.getArea());
s = new Triangle(4, 5);
System.out.println(s.toString());
System.out.println("Area = " + s.getArea());
}
}
3. Hands-on
3.1. Lab 4.1: Continue the above examples, implement the Triangle class (1 pts):
Triangle.java
public class Triangle extends Shape{
private double base;
private double high;
public Triangle() {
super();
this.base = 0.0;
this.high = 0.0;
}
@Override
5
Thu Dau Mot University Object Oriented Programming
@Override
public String toString() {
return "Triangle{" + "base=" + this.base + ", high=" + this.high +
", color=" + color + '}';
}
}
3.2. Lab 4.2: Implement superclass Shape and its concrete subclasses based on
following diagram (4 pts):
6
Thu Dau Mot University Object Oriented Programming
// Shape.java
public abstract class Shape {
protected String color;
protected boolean filled;
public Shape() {}
// Circle.java
public class Circle extends Shape {
private double radius;
public Circle() {
}
7
Thu Dau Mot University Object Oriented Programming
@Override
public double getArea() {
return Math.PI * this.radius * this.radius;
}
@Override
public double getPerimeter() {
return this.radius * 2 * Math.PI;
}
@Override
public boolean equals(Shape s) {
if (s instanceof Circle) {
Circle cir = (Circle) s;
return (this.getRadius() == cir.getRadius()) &&
(this.getColor() == cir.getColor() && (this.filled == cir.filled));
}
else return false;
}
@Override
public String toString() {
return "Circle{" + "radius=" + this.radius + ", color=" + this.color + ",
filled=" + this.filled + '}';
}
}
// Rectangle.java
public class Rectangle extends Shape{
public Rectangle() {
}
public Rectangle( double width, double length, String color, boolean filled) {
super(color, filled);
this.width = width;
this.length = length;
}
8
Thu Dau Mot University Object Oriented Programming
@Override
public double getArea() {
return this.width * this.length;
}
@Override
public double getPerimeter() {
return (this.width + this.length) * 2.0;
}
@Override
public boolean equals(Shape s) {
if (s instanceof Rectangle) {
Rectangle rec = (Rectangle) s;
return (this.getLength() == rec.getLength() && this.getWidth()
== rec.getWidth() && this.getColor() == rec.getColor() && this.filled ==
rec.filled);
}
else return false;
}
@Override
public String toString() {
return "Rectangle{" + "length=" + length + ", width=" + width + " , " +
this.color + " , " + this.filled;
}
}
// Square.java
public class Square extends Rectangle {
public Square() {
}
9
Thu Dau Mot University Object Oriented Programming
}
@Override
public void setWidth(double side) {
setSide(side);
}
@Override
public void setLength(double side) {
setSide(side);
}
}
3.3. Lab 4.3: Implement Interface Movable and its implementations MovablePoint and
MovableCircle (2 pts):
// Moveable.java
public interface Movable {
void moveUp();
void moveDown();
void moveLeft();
void moveRight();
}
10
Thu Dau Mot University Object Oriented Programming
// MoveablePoint.java
public class MovablePoint implements Movable {
private int x;
private int y;
private int xSpeed;
private int ySpeed;
@Override
public void moveUp() {
this.x += this.xSpeed;
@Override
public void moveDown() {
this.x -= this.xSpeed;
}
@Override
public void moveLeft() {
this.y -= this.ySpeed;
}
@Override
public void moveRight() {
this.y += this.ySpeed;
}
11
Thu Dau Mot University Object Oriented Programming
@Override
public String toString() {
return "MoveablePoint{" +
"x=" + x +
", y=" + y +
", xSpeed=" + xSpeed +
", ySpeed=" + ySpeed +
'}';
}
}
// MovableCircle.java
public class MovableCircle implements Movable{
@Override
public void moveUp() {
this.center.setX(this.center.getX() + this.center.getxSpeed());
}
@Override
public void moveDown() {
this.center.setX(this.center.getX() - this.center.getxSpeed());
}
@Override
public void moveLeft() {
this.center.setY(this.center.getY() - this.center.getySpeed());
}
@Override
public void moveRight() {
this.center.setY(this.center.getY() + this.center.getySpeed());
}
12
Thu Dau Mot University Object Oriented Programming
@Override
public String toString() {
return "Center: " + "(" + this.center.getX() + ", "+ this.center.getY() +
")";
}
}
3.4. Lab 4.4: Implement Interface GeometricObject and Resizable based on following
diagram (3 pts):
// GeometricObject.java
public interface GeometricObject {
// Circle.java
public class Circle implements GeometricObject{
private double radius = 1.0f ;
13
Thu Dau Mot University Object Oriented Programming
@Override
public String toString() {
return "Circle " + this.radius;
}
@Override
public double getArea() {
return Math.PI * this.radius * this.radius;
}
@Override
public double getPerimeter() {
return this.radius * 2 * Math.PI;
}
}
// Resizable.java
public interface Resizable {
public void rezize(int percent) ;
}
// ResizableCircle.java
public class ResizableCircle extends Circle implements Resizable{
@Override
public String toString() {
return "ResizableCircle " + getRadius();
}
@Override
public void rezize(int percent) {
14
Thu Dau Mot University Object Oriented Programming
15