2016-11-09 HW

posted Nov 9, 2016, 1:14 PM by Samuel Konstantinovich   [ updated Nov 9, 2016, 1:14 PM ]
Goal: Abstract classes and methods. Just as confusing as art, but more useful.

HW: Complete the class hierarchy (all 4 classes) and have a digital copy tomorrow in class!

An abstract class is a class that cannot be instantiated, but can be subclassed.
   It is declared abstract, and it it may or may not include abstract methods.

The opposite of the abstract designation is concrete.
There is no keyword needed for concrete.

An abstract method is a method that is declared without an implementation.
like this:
     public abstract void moveTo(double deltaX, double deltaY);
     //(without braces, and followed by a semicolon)


Any class that includes abstract methods, must be declared abstract, as in:

public abstract class GraphicObject {
   // declare fields
   // declare nonabstract methods
   public abstract void draw();
}

When an abstract class is subclassed, either:
-Implement ALL abstract methods and it is now a concrete class.
 //We usually do this

-Implement some but not all (can be none) of the abstract methods,
then the subclass must also be declared abstract.
//This would be used to make a more specific abstract subclass but isn't used in this course.


Abstract classes are similar to interfaces. We will talk about them later.

Consider using abstract classes if these statements apply to
your situation:
-You want to share code among several closely related classes.
-You do not have a way to implement the code yet, at least until you
 add more fields/methods in the subclasses


Abstract class Shape.
a) It has the followin ABSTRACT METHODS: 
    public double getArea()
    public double getPerimeter()
b) It has the following public methods:
    public double getSemiPerimeter()//This is NOT abstract. How can this be?!?!?
    public String toString()  -> returns "Shape"

There are two sub classes of Shape: Circle and Rectangle
THEY MUST IMPLEMENT (write the complete method for) All abstract methods.

d) Circle
    double radius
    set/get methods for radius
    Circle(double)
    Circle() //sets the radius to 1.0
    toString()
        //returns: "A Circle with radius=xxx, which is a subclass of yyy", 
        //where yyy is the output of the toString() method from the superclass.

e) Rectangle
    double width
    double length
    set and get methods for width/length
    Rectangle() //sets the width/length to 1.0
    Rectangle(double,double) //width and length
    toString() 
       //returns: "A Rectangle with width=xxx and length=zzz, which is a subclass of yyy"
       //where yyy is the output of the toString()method from the superclass.

Rectangle has one subclass Square:
f) Square
    Square() //same as superclass constructor
    Square(double)
    Override the setLength() and setWidth() to change both the width and length,
    so as to maintain the square geometry.
    toString()
       //returns: "A Square with side=xxx, which is a subclass of yyy"
       //where yyy is the output of the toString() method from the superclass.

Comments