Week 2

Post date: Jun 13, 2019 11:31:58 AM

Important details about Overriding

    • Changing the Return Type of an Overridden Method

    • Changing the Access Permission of an Overridden Method

Chapter 8: Polymorphism and Abstract Classes

8.1 Polymorphism

Recommended Self-Test exercises: ALL!

Lab Exercise:

Test your classes with the following TestShape class:

// A test driver program for Shape and its subclasses public class TestShape { public static void main(String[] args) { Shape s1 = new Rectangle("red", 4, 5); System.out.println(s1); System.out.println("Area is " + s1.getArea()); Shape s2 = new Triangle("blue", 4, 5); System.out.println(s2); System.out.println("Area is " + s2.getArea()); Shape s3 = new Shape("green"); System.out.println(s3); System.out.println("Area is " + s3.getArea()); } }

Source: Java Programming Tutorial

Chapter 8: Polymorphism and Abstract Classes

8.2 Abstract Classes

Other resources:

Lab Exercises:

Ex 1:

click to enlarge

In this exercise, Shape shall be defined as an abstract class, which contains:

    • Two protected instance variables color(String) and filled(boolean). The protected variables can be accessed by its subclasses and classes in the same package. They are denoted with a '#' sign in the class diagram.

    • Getter and setter for all the instance variables, and toString().

    • Two abstract methods getArea() and getPerimeter() (shown in italics in the class diagram).

The subclasses Circle and Rectangle shall override the abstract methods getArea() and getPerimeter() and provide the proper implementation. They also overridethe toString().

Write a test class to test these statements involving polymorphism and explain the outputs. Some statements may trigger compilation errors. Explain the errors, if any.

Shape s1 = new Circle(5.5, "RED", false); // Upcast Circle to Shape System.out.println(s1); // which version? System.out.println(s1.getArea()); // which version? System.out.println(s1.getPerimeter()); // which version? System.out.println(s1.getColor()); System.out.println(s1.isFilled()); System.out.println(s1.getRadius()); Circle c1 = (Circle)s1; // Downcast back to Circle System.out.println(c1); System.out.println(c1.getArea()); System.out.println(c1.getPerimeter()); System.out.println(c1.getColor()); System.out.println(c1.isFilled()); System.out.println(c1.getRadius()); Shape s2 = new Shape(); Shape s3 = new Rectangle(1.0, 2.0, "RED", false); // Upcast System.out.println(s3); System.out.println(s3.getArea()); System.out.println(s3.getPerimeter()); System.out.println(s3.getColor()); System.out.println(s3.getLength()); Rectangle r1 = (Rectangle)s3; // downcast System.out.println(r1); System.out.println(r1.getArea()); System.out.println(r1.getColor()); System.out.println(r1.getLength()); Shape s4 = new Square(6.6); // Upcast System.out.println(s4); System.out.println(s4.getArea()); System.out.println(s4.getColor()); System.out.println(s4.getSide()); // Take note that we downcast Shape s4 to Rectangle, // which is a superclass of Square, instead of Square Rectangle r2 = (Rectangle)s4; System.out.println(r2); System.out.println(r2.getArea()); System.out.println(r2.getColor()); System.out.println(r2.getSide()); System.out.println(r2.getLength()); // Downcast Rectangle r2 to Square Square sq1 = (Square)r2; System.out.println(sq1); System.out.println(sq1.getArea()); System.out.println(sq1.getColor()); System.out.println(sq1.getSide()); System.out.println(sq1.getLength());

What is the usage of the abstract method and abstract class?

Source: Java Programming Tutorial

Ex 2:

Write the following:

  • A Comparison super class that declares an abstract method called compare2Numbers. This method should have two integer parameters.

  • A sub class of Comparison called NumbersComparison that defines the abstract method compare2Numbers from the Comparison class. This method should take two integers and it should print the message “Numbers are equal” if they are, and prints “Numbers are not equal” otherwise. This class should also declare an abstract method called calculateArea that calculates the area of a circle.

  • A sub class of NumbersComparison called CircleArea that defines the method calculateArea from the NumbersComparison class.

  • An application called ComparingApp that uses the methods compare2Numbers and calculateArea.