CPSC 124, Fall 1998

Quiz Number 5


This is the fifth quiz given in CPSC 124: Introductory Programming, Fall 1998. See the information page for that course for more information.

The answers given here are sample answers that would receive full credit. However, they are not necessarily the only correct answers. In some cases, I give more detailed answers here than would be necessary to get full credit.


Question 1: What is a constructor? How are constructors used? What is their purpose?

Answer: A constructor is special kind of subroutine in a class whose purpose is to ``construct'' new objects. The name of a constructor is the same as the name of the class that contains it, and it has no return type, not even void. It can only be called by using the new operator. After memory has been obtained for the new object on the heap, the system executes the constructor. It can fill in values for the instance variables of the object, but it can also do any other task that the programmer wants to perform when a new object is created.


Question 2: Suppose that class Tiger is a subclass of class Cat. Explain why the statement ``Cat c = new Tiger();'' is legal, while the statement ``Tiger t = new Cat();'' is illegal.

Answer: Since Tiger is a subclass of Cat, every object of type Tiger is also an object of type Cat. So it is legal to assign an object of type Tiger to a variable of type Cat. On the other hand, not all Cats are Tigers, so it is illegal to assign a Cat object to a Tiger variable.


Question 3: What is ``this'', in the context of Java programming?

Answer: ``this'' is a special variable that can be used in an instance method to refer to the object for which the method was called. If we think of calling a method as sending a message to an object, then this refers to the object to which the message was sent. For example, if a class Shape includes a method

             void select() {  // select this shape
                selectedShape = this;
             }

Then when the statement bigBlueRect.select() is executed, the this in the select method refers to bigBlueRect, and when smallGreenTriangle.select() is executed, the same this refers to smallGreenTriangle.


Question 4: Define the term polymorphism and give an example.

Answer: Polymorphism refers to the fact that the same method, when called for objects of different types, might perform different actions. In terms of messages, it means that different types of objects can respond to the same message in different ways. For example, if Shape is a class and RectShape and OvalShape are subclasses of Shape, then these classes might include a draw() method. This method will do different things for ovals and for rects, so it is a polymorphic method. If shp is a variable of type Shape, then shp can refer to both RectShape objects and OvalShape objects. This means that the statement shp.draw() can do different things, depending on what type of object shp actually refers to.


Question 5: One of the big problems in software development is how to reuse old work. Explain how object-oriented programming can help to solve this reuse problem.

Answer: First of all, a class is a module -- that is, a relatively independent software component -- that can be copied as a whole from one programming project to another. This is an example of reuse of old work, but it doesn't really have anything to do with object-oriented programming in particular. More important, in OOP, a programmer can build on old work by making a subclass of an existing class. Then the programmer can make just those changes and adaptations that are necessary to adapt the old class to a new programming project.


David Eck, 23 October 1998