CS4Programming18May2015

From GIS CS4
Jump to: navigation, search

OOP Terms and Concepts

Some things to remember:

  • An object class defines a type, prototype or template to be used to create objects.
  • Class names begin with a capital letter e.g. RectArea
  • When an object is created using the new keyword, the object is called an instance of the class.
  • A special function known as the Constructor creates and initialises a new instance of a class.
  • Instance names begin with a lowercase letter (same as variable and function names) e.g. rectArea1;
  • A new, more specific class can be defined based on an existing more general class, e.g. the class TextArea can be derived from RectArea, or the classes Tulip and Sunflower can be derived from the Flower class.
  • A hierarchy of more and more specific classes can be defined and this can be called the class hierarchy or inheritance tree.
  • The more general class is known as the base, parent or super class.
  • The more specific class is known as the derived, child or sub class.

Flower Grower Challenge

The final will be based on concepts of object oriented programming.

In programming languages and type theory, polymorphism (from Greek πολύς, polys, "many, much" and μορφή, morphē, "form, shape") is the provision of a single interface to entities of different types.[1]

Use the flower grower program and review the code and the comments -

Polymorphism using "Flower Grower Challenge":

https://www.khanacademy.org/cs/polymorphism-challenge-flower-grower/6304205634535424

Multiple Choice

1. Which of the following IS NOT a property of the Flower class?

c) growBy is a method, x, y and height are properties

2. What type of class is the Flower class NOT?

b) Flower is not a Virtual class, it is a base or parent class. It is also an abstract class.

3. Which of the following statements IS NOT true?

a) The Flower class does not inherit the growBy method. Flower is a base class therefore it defines properties and methods that may be inherited by derived classes.

4. Which of the following IS NOT true about the Flower class growBy method?

a) The Flower growBy method is not a virtual method. Vitual methods should not be called. They just provide a prototype for a method that should be overriden by each derived class.

5. Which of the following IS NOT true about a virtual method?

b) A virtual method is not inherited from the bass class. A virtual method should be overriden by the derived class.

6. Which of the following IS NOT an example of inheritance in OOP?

c) If a derived class defines a new method in addition to those defined in the base class then this is not an example of inheritance.

7. What does the word polymorphism mean?

b) many shapes.

8. Which of the following is an example of polymorphism in OOP?

c) The Flower draw method is an example of polymorphism. It is a virtual method which provides a prototype for a method that should be overriden by each derived class.

9. Which of the following is a feature of polymorphism?

a) Allows a single interface to be used to handle objects of different types.

Free Response

10. In your own words, describe how polymorphism is used in our version of Flower Grower challenge.

The Flower base class defines a virtual draw() method. This provides a common interface to call the display method for all derived classes. Each derived class needs to override the base class virtual draw() method in order to implement the specific display functionality for each derived flower.

Fill-in-the-Blanks

  • Our version of the Flower Grower Challenge demonstrates some features of OOP.
  • The Tulip and Sunflower classes are derived from the Flower base class.
  • All Flowers have an x,y position and a height.
  • These properties will be inherited by each object of a derived class.
  • Flower is an example of an abstract class.
  • Other classes can be derived from the Flower class.
  • There should never be an instance of the Flower class.
  • The growBy method is implemented on the base Flower class.
  • This method will enable derived classes to inherit behaviour from the base class without the need for this method to be implemented again for each derived class.
  • This demonstrates the power of inheritance - allowing code to be reused.
  • The Flower draw method is an example of a virtual method.
  • It should never be called because the Flower base class is an abstract class and doesn't know how to draw a specific Flower.
  • The Flower.draw() method provides a prototype or template for calling the draw() method in a common way for all derived Flowers.
  • Every derived class needs to provide its own specific draw() method.
  • The specific draw() methods on each derived class will override the virtual method on the base class.
  • Sunflowers are unusual in that they grow super fast.
  • Although the Flower base class provides a default growBy method that is inherited by most flowers, Sunflower can override the Flower base class growBy method so that it can grow twice as fast.
  • However the Sunflower growBy method does make use of base class default method by calling the method but doubling the amount parameter.