Algorithms and OOD (CSC 207 2014F) : EBoards

# CSC 207 2014F: Review Session, Week 1

Overview

• You ask questions
• I attempt to give answers

## Could You Throw More Light on Polymorphism, Inheritance, and Encapsulation?

• For clarity: I have not yet done the deep discussion of these topics.
• Each of these serves one of two useful purposes in OOP
• Save (repeated) code!
• Protect programmers from each other

Polymorphism

• Consider `(define square (lambda (x) (* x x)))`
• `square` works for integers
• `square` works for doubles
• `square` works for complex numbers
• In fact, square will work for anything that supports multiplication
• Basic idea of polymorphism: You write the operation once, and it works for lots and lots of types of values.
• Why is it useful?
• No repeated code!
• Is C polymorphic?
• Most of C expects you to provide a type, so it's harder to write polymorphic functions.
• Two strategies for polymorphic functions:
• Just pass pointers
• Macros `#define square(x) ((x)*(x))`
• To some, overloading is a form of polymorphism
• There's more (it's not just about polymorphic function), but that's enough for now

Inheritance

• Very abstract: I write code to do one thing. I want to write code to do something similar. Inheritance is a way to programmatically base one set of code on another set of code, without retyping.
• Done in terms of objects.
• Simple object: GrinnellStudent
• Fields: Surname, Forename, StudentID, Transcript, Email Address, ...
• Operations: getFullName(), sendMessageTo(String message), writeRecommendationLetter()
• New object: GrinnellStudentEmployee
• Need all the same fields
• Plus other fields: (e.g., pay grade)
• Need all the same operations
• Plus other operations: pay()
• May need to change some operations, such as writeRecommendationLetter
• Term: override
• I don't want to have to copy and paste all of the common things
• Strategy: Have GrinnellStudentEmployee "inherit" all of the common things from GrinnellStudent
• Note: Although multiple inheritance is really useful conceptually, it's a mess in terms of semantics and implementation, so Java disallows it.
• In Java

``````public class GrinnellStudentEmployee
extends GrinnellStudent
``````

Encapsulation

• Two main issues
• Group related things together (like a struct in C, except that we also include functions in the things we group)
• Protect some parts of the things from the outside world
• Suppose we are representing points on the plane

``````public class Point
{
// +--------+------------------------------------------------------
// | Fields |
// +--------+

double x;
double y;

// +---------+-----------------------------------------------------
// | Methods |
// +---------+

public double distanceFromOrigin()
{
return Math.sqrt(x*x + y*y);
} // distanceFromOrigin
} // class Point
``````
• Two approaches for the client

``````public class GeoLocator
{
public static void main(String[] args)
throws Exception;
{
Point pt1 = GoogleMaps.makePoint("Grinnell, IA");
Point pt2 = GoogleMaps.makePoint("Minneapolis, Minisoda");

// I need to figure out which of point 1 and point 2 is closer
/  to the origin
if (pt1.distanceFromOrigin() &lt; pt2.distanceFromOrigin())
giveDirectionsTo(pt1);
else
giveDirectionsTo(pt2);

}
}

// I need to figure out which of point 1 and point 2 is closer
/  to the origin (Note: For non-negative x and y,
// x &lt; y iff sqrt(x) &lt; sqrt(y).)
if (pt1.x*pt1.x + pt1.y*pt1.y &lt; pt2.x*ptx + pt2.y*pt2.y)
giveDirectionsTo(pt1);
else
giveDirectionsTo(pt2);

``````
• Which is better?

• The first is more readable.
• The first is more convenient.
• The second is a bit faster.
• The first is more amenable to changes in implementation.

What happens if we decide that we should represent points with radius and angle?

``````    public class Point
{
// +--------+------------------------------------------------------
// | Fields |
// +--------+

double radius;
double theta;

// +---------+-----------------------------------------------------
// | Methods |
// +---------+

public double distanceFromOrigin()
{
return radius;
} // distanceFromOrigin
} // class Point
``````
• Encapsulation lets you set policies for what can and cannot be accessed and enforces thoses policies (usually through the compiler)

## Basic Java Program Structure

• Done in Eclipse. I'll try to remember to move the code here.