0% found this document useful (0 votes)
50 views15 pages

OOP Lab4

The document discusses polymorphism and abstraction in object-oriented programming. It provides code examples of implementing polymorphism using an interface and abstract class to define common behaviors for shapes. It also demonstrates abstraction by defining an abstract Shape class with subclasses for specific shapes like Rectangle and Triangle that implement abstract methods defined in the superclass. Hands-on exercises include implementing a Triangle class and modifying the Shape class hierarchy based on a UML diagram.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views15 pages

OOP Lab4

The document discusses polymorphism and abstraction in object-oriented programming. It provides code examples of implementing polymorphism using an interface and abstract class to define common behaviors for shapes. It also demonstrates abstraction by defining an abstract Shape class with subclasses for specific shapes like Rectangle and Triangle that implement abstract methods defined in the superclass. Hands-on exercises include implementing a Triangle class and modifying the Shape class hierarchy based on a UML diagram.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Thu Dau Mot University Object Oriented Programming

HANDS-ON #4: POLYMORPHISM, ABSTRACTION

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

if (obj instanceof Rectangle) {


Rectangle rec = (Rectangle) obj;
return (this.getLength() == rec.getLength()) &&
(this.getWidth() == rec.getWidth());
}
else return false;
}
@Override
public String toString() {
return "Rectangle{" + "length=" + this.length + ", width=" +
this.width + '}';
}
}

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 = "";
}

public Shape(String color) {


this.color = color;
}

public abstract double getArea();

public String getColor() {


return this.color;
}

public void setColor(String color) {


this.color = color;
}
}

// Rectangle.java
public class Rectangle extends Shape {
private double length;
private double width;

public Rectangle() {
super();
this.length = 0;
this.width = 0;
}

public Rectangle(double length, double width, String color) {


super(color);
this.length = length;
this.width = width;
}

@Override
public double getArea() {
return this. length * this.width;
}

public double getPerimeter() {


return (this.length + this.width) * 2.0;
}

public String toString() {


return "Rectangle{" + "length=" + length + ", width=" + width + ",
color=" + color + '}';

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;
}

public Triangle(double base, double high, String color) {


super(color);
this.base = base;
this.high = high;
}

public double getBase() {


return this.base;
}

public double getHigh() {


return this.high;
}

public void setBase(double base) {


this.base = base;
}

public void setHigh(double high) {


this.high = high;
}

@Override

5
Thu Dau Mot University Object Oriented Programming

public double getArea() {


return this.base * this.high/2;
}

@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() {}

public Shape(String color, boolean filled) {


this.color = color;
this.filled = filled;
}

public String getColor() {


return color;
}

public void setColor(String color) {


this.color = color;
}

public boolean isFilled() {


return filled;
}

public void setFilled(boolean filled) {


this.filled = filled;
}

public abstract double getArea();


public abstract double getPerimeter();
public abstract boolean equals(Shape s);
public abstract String toString();

// Circle.java
public class Circle extends Shape {
private double radius;

public Circle() {
}

public Circle(double radius) {


this.radius = radius;
}

public Circle(double radius, String color, boolean filled) {


super(color, filled);
this.radius = radius;
}

public double getRadius() {


return radius;
}

7
Thu Dau Mot University Object Oriented Programming

public void setRadius(double radius) {


this.radius = radius;
}

@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{

protected double width;


protected double length;

public Rectangle() {
}

public Rectangle( double width, double length, String color, boolean filled) {
super(color, filled);
this.width = width;
this.length = length;
}

public double getWidth() {


return width;
}

public void setWidth(double width) {


this.width = width;
}

8
Thu Dau Mot University Object Oriented Programming

public double getLength() {


return length;
}

public void setLength(double length) {


this.length = length;
}

@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() {
}

public Square( double side, String color, boolean filled) {


super(side, side,color, filled);
}

public double getSide() {


return width;
}

public void setSide(double side) {


this.width = side;
this.length = side;

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;

public MovablePoint(int x, int y, int xSpeed, int ySpeed) {


this.x = x;
this.y = y;
this.xSpeed = xSpeed;
this.ySpeed = 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;
}

public int getX() {


return x;
}

public void setX(int x) {


this.x = x;
}

public int getY() {


return y;
}

public void setY(int y) {


this.y = y;
}

public int getxSpeed() {


return xSpeed;
}

11
Thu Dau Mot University Object Oriented Programming

public void setxSpeed(int xSpeed) {


this.xSpeed = xSpeed;
}

public int getySpeed() {


return ySpeed;
}

public void setySpeed(int ySpeed) {


this.ySpeed = ySpeed;
}

@Override
public String toString() {
return "MoveablePoint{" +
"x=" + x +
", y=" + y +
", xSpeed=" + xSpeed +
", ySpeed=" + ySpeed +
'}';
}
}

// MovableCircle.java
public class MovableCircle implements Movable{

private int radius;


private MovablePoint center;

public MovableCircle(MovablePoint center, int radius) {


this.center = center;
this.radius = radius;
}

@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 {

public double getPerimeter();


public double getArea();
}

// Circle.java
public class Circle implements GeometricObject{
private double radius = 1.0f ;

13
Thu Dau Mot University Object Oriented Programming

public Circle(double radius) {


this.radius = radius ;
}

public double getRadius() {


return radius;
}

public void setRadius(double radius) {


this.radius = radius;
}

@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{

public ResizableCircle(double radius) {


super(radius);
}

public ResizableCircle(int radius) {


super(radius);
}

@Override
public String toString() {
return "ResizableCircle " + getRadius();
}

@Override
public void rezize(int percent) {
14
Thu Dau Mot University Object Oriented Programming

setRadius(percent * getRadius() /100);


}
}

15

You might also like