1 You can get stuck at the object-based level because you can quickly get there and you get a lot of benefit without much mental effort. It’s also easy to feel like you’re creating data types – you make classes and objects, you send messages to those objects, and everything is nice and neat. But don’t be fooled. If you stop here, you’re missing out on the greatest part of the language, which is the jump to true object-oriented programming. You can do this only with virtual functions. - Bruce Eckel, Thinking in C++, © Calvin College, 2009 2 Inheritance and Polymorphism ● ● Example The three elements of OOP: – Encapsulation; – Inheritance; – Polymorphism. ● A Final Word © Calvin College, 2009 3 Example: Analysis ● ● ● We’d like to build a painttype program that draws figures on demand. Figure types include rectangles, ellipses, lines, doodles, etc. A sketch of a solution achieving this goal is shown here. Mode buttons... Refresh The user draws the figures on this drawing panel using the mouse… © Calvin College, 2009 4 Example: Design ● The design includes the following classes: JFrame PApplet SimpledrawPanel SimpledrawController +setFigureMode() +setColorMode() +setFilledMode() +refresh() Figure?? 1 0..* © Calvin College, 2009 5 Iteration 0 ● Analysis ● Design ● Implementation ● Test Rectangle +myStart: Point +myWidth: int +myHeight: int +myColor: int +myFilled: boolean +render(PApplet) © Calvin College, 2009 6 Adding More Figure Types ● ● Our initial iteration’s design requires that we build a separate class for each figure type: rectangles, ellipses, lines, etc. This approach requires a considerable amount of duplication, so it doesn’t scale well to multiple figure types. image from http://www.linux.org/info/penguin.html © Calvin College, 2009 7 Modeling Objects ● ● Object-Oriented programming models objects and the relationships between them. Examples: – figures, squares, rectangles, polygons, doodles; – people, students, teachers, staff members, you; – animals, birds, penguins, wings, ; – naval vessels, submarines, carriers, fighter jets. image from http://www.linux.org/info/penguin.html © Calvin College, 2009 8 Modeling Relationships ● ● Inter-object relationships can usually be characterized by one of these paraphrases: – “is a” – “is a kind of” – “has a” Examples © Calvin College, 2009 9 Implementing Relationships ● ● Object-oriented languages implement these relationships as follows: – “is a” – “is a kind of” – “has a” We’ve implemented all of these things before in one form or another. © Calvin College, 2009 10 Class Aggregation ● ● Aggregation specifies containercontained relationships between classes. The container references the contained. Container Contained +referenceToContained © Calvin College, 2009 11 Class Inheritance ● ● Inheritance specifies one-directional, parent-child relationships. Parent The child inherits the parent’s: +parent's attributes – data – methods ● Each child can polymorphically “specialize” itself by overriding or adding data or methods. +parent's operations() Child +parent's attributes +child's attributes +parent's operations() +child's operations() © Calvin College, 2009 12 Carl Linneaus (1707-1778) Taxonomy ● ● Systema Naturae, 1758 7 levels: – – – – – – – Kingdom Phylum Class Order Family Genus Species images from www.linnean.org & www.kheper.auz.com © Calvin College, 2009 13 Example: Java’s Classes ● ● All Java classes fit into one hierarchy. All classes inherit from the root class: java.lang.Object ● You can find the full Java hierarchy here: http://java.sun.com/javase/6/docs/api/overview-tree.html © Calvin College, 2009 14 Iteration 1 ● Analysis ● Design ● Implementation ● Test © Calvin College, 2009 15 Example: Simpledraw SimpledrawPanel SimpledrawController +setFigureMode() +setColorMode() +setFilledMode() +refresh() Figure 1 0..* #myStart: Point #myColor: int +render(PApplet) ClosedFigure Line -myEnd: Point +render(PApplet) #myFilled : Boolean #myWidth : int #myHeight : int Rectangle +render(PApplet) Ellipse +render(PApplet) © Calvin College, 2009 16 Inheritance and Access ● Java provides three modifiers specifying access for class variables and methods: private – protected – public – ● It can be safer to declare attributes as private and provide protected accessor and mutator methods. © Calvin College, 2009 17 Abstract Classes ● Classes can be abstract or concrete. visibility abstract class className { classDefinition } ● Like concrete classes, abstract classes: – Can have sub-classes; – Can implement data and methods. ● Unlike concrete classes, abstract classes: – Cannot be instantiated. © Calvin College, 2009 18 Abstract Methods ● As with classes, methods can also be declared as abstract or concrete. visibility abstract type methodName(params); ● ● Abstract classes do not provide definitions for their abstract methods. Classes that contain abstract methods must be declared as abstract. © Calvin College, 2009 19 public abstract class Figure { protected Point myStart; protected int myColor; public Figure(Point start, int color) { myStart = start; myColor = color; } public int getColor() { return myColor; } public void setColor(int color) { myColor = color; } public abstract void render(PApplet p); } © Calvin College, 2009 20 Implementing Inheritance ● ● A child class specifies inheritance from a parent class using the extends clause. Concrete sub-classes must define the abstract methods that they inherit. Parent +aMethod() Child1 +aMethod() abstract class Parent { public abstract void aMethod(); } class Child1 extends Parent { public void aMethod() { // define method here... } } © Calvin College, 2009 21 Super-Class Constructors ● super is a reference to the parent. ● A child can invoke its parent’s constructor. super(parentConstructorArguments) ● A call to the parent’s constructor: must be the first statement in the constructor; – is added automatically if you don’t add it. – ● A child can invoke an overridden method: super.parentMethod(arguments) © Calvin College, 2009 22 public abstract class ClosedFigure extends Figure { protected int myWidth, myHeight; protected boolean myFilled; public ClosedFigure(Point start, int color, int width, int height, boolean filled) { super(start, color); myWidth = width; myHeight = height; myFilled = filled; } } © Calvin College, 2009 23 Overriding Methods ● ● ● When an object to asked to execute a method, Java searches up the inheritance hierarchy for a matching method definition. Thus, a sub-class that defines its own version of a method overrides any definitions of the methods that it inherits. A concrete sub-class must implement the abstract methods it inherits using a method with an identical signature. © Calvin College, 2009 24 public class Rectangle extends ClosedFigure { public Rectangle(Point start, Point end, int color, boolean filled) { super(start, color, end.x - start.x, end.y - start.y, filled); } @Override public void render(PApplet p) { p.stroke(myColor); if (myFilled) { p.fill(myColor); } else { p.noFill(); } p.rect(myStart.x, myStart.y, myWidth, myHeight); } } © Calvin College, 2009 25 public class Line extends Figure { private Point myEnd; public Line(Point start, Point end, int color) { super(start, color); myEnd = end; } @Override public void render(PApplet p) { p.stroke(myColor); p.line(myStart.x, myStart.y, myEnd.x, myEnd.y); } } © Calvin College, 2009 26 Rectangle myRectangle = new Rectangle(new Point(10, 10), new Point(280, 280), color(0), false); Ellipse myEllipse = new Ellipse(new Point(11, 11), new Point(278, 278), color(255, 55, 55), true); Line myLine1 = new Line(new Point(10, 10), new Point(280, 280), color(55, 255, 55)); Line myLine2 = new Line(new Point(280, 10), new Point(10, 280), color(55, 55, 255)); myRectangle.render(this); myEllipse.render(this); myLine1.render(this); myLine2.render(this); © Calvin College, 2009 27 Polymorphism Parent ● ● Polymorphism allows different +aMethod() concrete sub-classes to provide potentially different definitions for a given method inherited from their shared parent. Java chooses the appropriate method definition based upon which child the object instantiates at either: Child1 Child2 +aMethod() +aMethod() – Compile time (static binding); – Run time (dynamic binding). © Calvin College, 2009 28 Declarations & Initializations Parent ● We can declare an object of an abstract parent class but must initialize it as an object of a concrete child class. +aMethod() Child1 Child2 +aMethod() +aMethod() Parent myObject = new Child1(args); ● Java decides which method implementation to use based on the concrete type: myObject.aMethod(args); © Calvin College, 2009 29 Example Parent +aMethod() List<Parent> myObjects = new ArrayList<Parent>(); Child1 Child2 +aMethod() +aMethod() myObjects.add(new Child1(arguments)); myObjects.add(new Child2(arguments)); // add more children of either type... for (int i = 0; i < myObjects.size(); i++) { myObjects.get(i).aMethod(arguments); } © Calvin College, 2009 30 Iteration 2 ● Analysis Mode buttons... ● Design ● Implementation ● Test Refresh © Calvin College, 2009 33 Fredrick P. Brooks (1931- ) The Mythical Man-Month What’s the Big Idea Joys of programming Woes of programming We enjoy designing things because we are created in the image of God. The “mindless” details can be excessively tedious. The computer is a powerful and rewarding tool to use. Products become obsolete too quickly. As the child delights in his mud pie, so the adult enjoys building things, especially things of his own design. I think this delight must be an image of God's delight in making things, a delight shown in the distinctness and newness of each leaf and each snowflake. - F. P. Brooks, Jr. The Mythical Man-Month, 1975 images from: http://www.amazon.com/ © Calvin College, 2009