Lab 10: Inheritance

advertisement

Lab 10: Inheritance

Lab Objectives

Use a superclass and subclass for software reusability

 Understand the purpose of an abstract class

Override methods in a superclass

 Call superclass methods

 Understand polymorphism

Lab References

 Chapter 2: Inheritance

 Chapter 14: Inheritance and design of base classes

Problem Description

Inheritance is reusing an existing class to create a new class. The new class is called the subclass and the existing class is the superclass. The subclass inherits all of the attributes and methods of the superclass. Since the methods of the superclass are inherited, they do not need to be defined again. This promotes software reusability. If needed, however, the subclass may override the methods of the superclass.

Polymorphism enables different objects within the same class hierarchy to call methods with the same name. The actual method invoked depends upon which type of object called the method. In this lab a class called Shape is the superclass. The classes Line ,

Oval , and Rectangle are subclasses of class Shape . All these classes have a method called draw . The Shape class declares this method as abstract . The subclasses must implement the draw method. When an object of type Shape calls the draw the method, the correct method is invoked for the type of object.

Files Required

 DrawShapes.java

 Lab10Demo.jar

 Line.java

 Oval.java

Rectangle.java

Shape.java

Lab Exercise

1.

You can execute Lab10Demo.jar

to view the solution ( JAR execution instructions ).

2.

Add the following as private instance variables to the Shape class:

private int x;

private int y;

Update the default constructor to initialize these variables to zero. Update the other constructor to accept two integers as arguments to initialize these variables.

3.

Implement get and set methods for x and y in the Shape class.

4.

Add the following to create an abstract draw method:

public abstract void draw(Graphics g);

Add the keyword abstract to the class definition.

public abstract class Shape

An abstract class cannot be used to create an instance of an object of this type.

However it can be used as a superclass and instance of the subclasses can be created.

5.

Open the file Line.java

and add extends Shape to the class definition to make the Line class a subclass of the Shape class.

6.

Add two private instance variables x2 and y2 to the Line class. These will both be of type int . These variables are the coordinates end point of the line. The variables in the superclass are the coordinates of the start point. Create get and set methods for the x2 and y2 variables.

7.

Create two constructors for the Line class. The first constructor will be the default constructor. It first calls the constructor for the superclass and initializes x and y to zero.

The second constructor accepts four integer arguments and an argument of type

Color . The first two arguments are sent to the superclass constructor and the other two arguments are used to initialize x and y .

public Line(int x, int y, int x2, int y2, Color c)

{

super(x, y, c);

this.x2 = x2;

this.y2 = y2;

}

8.

Add the following draw method to the Line class:

public void draw(Graphics g)

{

g.setColor(getColor());

g.drawLine(getX(), getY(), x2, y2);

}

This method uses the graphics device context to change the color and then draw a line. The first two arguments sent to the drawLine method are obtained using methods inherited from the superclass. They access the x and y attributes inherited from the superclass.

9.

Open the file Oval.java

and add extends Shape to the class definition to make the Oval class a subclass of the Shape class.

10.

Add two private instance variables width and height to the Oval class. These will be of type int . These variables are the width and height of the bounding box for the oval. The variables in the superclass are the coordinates of the upper left corner of the bounding rectangle of the oval. Create get and set methods for the width and height variables.

11.

Create two constructors for the Oval class. The first constructor will be the default constructor. It first calls the constructor for the superclass and initializes width and height to zero.

The second constructor accepts four integer arguments and an argument of type

Color . The first two arguments are sent to the superclass constructor and the other two arguments are used to initialize width and height .

public Oval(int x, int y, int w, int h, Color c)

{

super(x, y, c);

width = w;

height = h;

}

12.

Add the following draw method to the Oval class:

public void draw(Graphics g)

{

g.setColor(getColor());

g.drawOval(getX(), getY(), width, height);

}

This method uses the graphics device context to change the color and draw an oval. As before, the first two arguments sent to the drawOval method are obtained using methods inherited from the superclass.

13.

Open the file Rectangle.java

and add extends Shape to the class definition to make the Rectangle class a subclass of the Shape class.

14.

Add two private instance variables width and height to the Rectangle class. These will be of type int . These variables are the width and height of the rectangle. The variables in the superclass are the coordinates of the upper left corner of the rectangle. Create get and set methods for the width and height variables.

15.

Create two constructors for the Rectangle class. The first constructor will be the default constructor. It first calls the constructor for the superclass and initializes x and y to zero.

The second constructor accepts four integer arguments and an argument of type

Color . The first two arguments are sent to the superclass constructor and the other two arguments are used to initialize width and height .

public Rectangle(int x, int y, int w, int h,

Color c)

{

super(x, y, c);

width = w;

height = h;

}

16.

Add the following draw method to the Rectangle class:

public void draw(Graphics g)

{

g.setColor(getColor());

g.drawRect(getX(), getY(), width, height);

}

This method uses the graphics device context to change the color and draw a rectangle. As before, the first two arguments sent to the drawRect method are obtained using methods inherited from the superclass.

17.

Open the program DrawShapes.java

. This program allows you to draw lines, ovals, and rectangles. The following vector is used to save the shapes drawn:

private Vector shapes = new Vector(20,5);

// initial capacity of 20 objects,

// increase capacity by 5 when necessary

A shape is created and displayed on the screen as it is being drawn. The following method creates the shapes:

private Shape createShape()

{

switch(currentElement)

{

case LINE:

return new

Line((int)startPoint.getX(),

(int)startPoint.getY(),

(int)endPoint.getX(),

(int)endPoint.getY(),

currentColor);

case RECTANGLE:

return new

Rectangle((int)startPoint.getX(),

(int)startPoint.getY(),

(int)endPoint.getX() -

(int)startPoint.getX(),

(int)endPoint.getY() -

(int)startPoint.getY(),

currentColor);

case OVAL:

return new

Oval((int)startPoint.getX(),

(int)startPoint.getY(),

(int)endPoint.getX() -

(int)startPoint.getX(),

(int)endPoint.getY() -

(int)startPoint.getY(),

currentColor);

}

return null;

}

The paint method uses polymorphism to call the correct draw method for the shape.

public void paint(Graphics g)

{

super.paint(g);

Enumeration e = shapes.elements();

while (e.hasMoreElements())

{

((Shape)(e.nextElement())).draw(g);

}

}

Run the DrawShapes program.

Download