Lab 7
Lab 7
OBJECT-ORIENTED PROGRAMMING
LAB 7: POLYMORPHISM, ABSTRACTION
I. Objective
After completing this first lab tutorial, you can:
• Understand polymorphism and abstraction in OOP.
II. Polymorphism
Polymorphism is the behavior of functionality changes according to the actual type of data. We have
two mechanisms are Overloading and Overriding to approach the polymorphism in Java. 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 through the example of the interface in Java.
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 objects.
// 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;
}
@Override
public double getArea() {
return this.length * this.width;
}
@Override
public String toString() {
return "Rectangle{" + "length=" + this.length + ", width=" +
this.width + '}';
}
}
// Test.java
public class Test {
public static void main(String[] args) {
Shape s = new Rectangle(4, 3);
System.out.println(s.toString());
System.out.println("Area = " + s.getArea()); // 12.0
III. Abstraction
Abstraction is the process of hiding the implementation details from the user, only the functionality will
be provided to the user. For example, in the email system, to send an email, the user needs only to
provide the recipient’s email, and the email’s content and click send. All implementation of the system
is hidden. We have two approaches, the first one is taking advantage of the interface and the second is
using abstract class.
Java provides a mechanism to allow a program to achieve abstraction using abstract keywords. The
abstract keyword can be used for class and method definitions. For example, the following diagram 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 the getArea() method.
You should notice that if you define an abstract class, the method must be either abstract or implemented.
An abstract class cannot be instantiated. This usually happens because it has one or more abstract
methods. It must be implemented in concrete (i.e., non-abstract) subclasses.
// 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;
}
// Test.java
public class Test {
public static void main(String[] args) {
Shape s = new Rectangle(4, 3, "white");
System.out.println(s.toString());
System.out.println("Area = " + s.getArea());
@Override
public boolean equals(Object obj) {
if(obj instanceof Rectangle) {
Rectangle temp = (Rectangle) obj;
if(this.getArea() == temp.getArea()) {
return true;
}
}
return false;
}
}
System.out.println(rec.equals(rec1));
System.out.println(rec.equals(rec2));
}
}
b) You have two Fraction objects and these two fractions equal when they have the numerators and
the denominators equal in reduced form. We can define the Fraction class:
public class Fraction {
private int numerator;
private int denominator;
System.out.println(f.equals(f1));
System.out.println(f.equals(f2));
}
}
V. Exercises
1. Continue the above examples in Section III, and implement the Triangle class.
2. Given the Abstract superclass Shape and its concrete subclasses.
All Shapes will compare based on the area, if two Shapes have the same area, these Shapes will
be equal.
Write a class with a main method to test your work.
Assume that in the main method of the test class, you have a list containing any shapes. Your
work is to find the shape that has the largest area in this list.
Example: Find the shape that has the largest area in the list below.
Shape[] shapes = new Shape[5];
shapes[0] = new Circle(4, "Red", true);
shapes[1] = new Rectangle(8, 4, "Blue", true);
shapes[2] = new Square(10, "Black", true);
shapes[3] = new Circle(9);
shapes[4] = new Rectangle(12, 8, "Blue", true);
3. Implement the below interface and classes to manage points in Descartes’s coordinate system.
Given that, the move methods will change the coordinate x or y based on the xSpeed or ySpeed.