1 / 15

The object contract

The object contract. Abstract classes Abstract Vs. Concrete Reference type Vs. Object type Multiple Inheritance Interfaces. The object contract. The superclass defines how the objects are to be used (the contract).

nasya
Download Presentation

The object contract

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The object contract Abstract classes Abstract Vs. Concrete Reference type Vs. Object type Multiple Inheritance Interfaces

  2. The object contract • The superclass defines how the objects are to be used (the contract). • Functionality can be overridden, but the method name/purpose defines the contract. • Example: Say we wish to keep track of a bunch of 2D shapes to bounce around a Jpanel • All shapes need: constructor, draw(), move() • constructor and move() is the same all shapes • draw() is different code for each shape • Polymorphism allows us to treat all objects similarly • object.move() • object.draw() • This is the “contract” – how to use the object • The contract is the same for all subclasses Shape Shape (x, y, dx, dy, color) move() draw() Triangle draw () Circle draw () Square draw ()

  3. Polymorphism as a contract:Using the Shape Class … ArrayList<Shape> shapeList; … public void makeShapes () { shapeList = new ArrayList<Shape>(); shapeList.add(new Circle(0,0,5,3,Color.Red)); shapeList.add(new Square(10,10,-1,-5,Color.Green)); } // end makeShapes … public void paintComponent (Graphics g) { for (Shape shape : shapeList) { shape.move(); shape.draw(); } } // end method paintComponent …

  4. How do I define Shape? • What should the default code be for draw()? • What should happen if I try to make a new Shape()? • What would it look like? Shape Shape (x, y, dx, dy, color) move() draw() … ArrayList<Shape> shapeList; … public void makeShapes () { shapeList = new ArrayList<Shapes>(); shapeList.add( new Circle(0,0,5,3,Color.Red)); shapeList.add( new Shape(10,10,1,-5,Color.Green)); } // end makeShapes … Triangle draw () Circle draw () Square draw ()

  5. Abstract classes • Some classes should never be instantiated • Some superclasses exists only to define a contract • You may never want to allow anyone to create one! • Marking a class abstract tells the compiler to create an instance of the class. Its only use is to be extended! Shape Shape (x, y, dx, dy, color) move() draw() Triangle draw () Circle draw () Square draw () abstract class Shape { Shape (int x, int y, int dx, int dy, Color color) { … // my code to inherit here } // end constructor Shape void move () { … // my code to inherit here } // end method move void draw () { … // what should go here? } // end method draw } // end class Shape

  6. Abstract methods • Some methods in an abstract class may have no reasonable default • How do I draw a “Shape”? As a circle? A triangle? • Marking a method abstract forces subclasses to implement/override it • An abstract methods can only exist in an abstract class • Defines contract, not implementation Shape Shape (x, y, dx, dy, color) move() draw() Triangle draw () Circle draw () Square draw () abstract class Shape { Shape (int x, int y, int dx, int dy, Color color) { … // my code to inherit here } // end constructor Shape void move () { … // my code to inherit here } // end method move abstract void draw (); // no body! // Subclass MUST define function } // end class Shape

  7. Abstract Vs. Concrete classes • Abstract class: cannot be instantiated • It can have static members • It can have non-static members • These can only be called by instances of a concrete subclass • It can have abstract method headers • Thus, it defines a contract • Concrete class: A class that is not abstract • The objects created and doing the work at runtime are concrete • These objects may be instances of a subclass of an abstract class • Cannot have anyabstractmethods • Must implement inherited abstract methods UML Note: Use italics to indicate abstract ABSTRACT Shape Shape (x, y, dx, dy, color) move() draw() Triangle draw () Circle draw () Square draw () CONCRETE CONCRETE CONCRETE

  8. Polymorphism means “Many Forms” • How does the compiler know what members are/are not available? Object Object() equals (object ) toString () clone(object) getClass() … … Triangle t = new Triangle (); Shape s = (Shape) t; Object o = (Object) t; … Shape Shape (x, y, dx, dy, color) move() draw() Triangle draw () rotate() Circle draw () Square draw () rotate() Shape Triangle Shape() move() draw() t draw() rotate() s o

  9. Instance Type IS-A Reference Type • The compiler cares about the type of the reference variable, not the class of the actual object at the other end of the reference (unknown till runtime!) • OK – resolved at runtime t.rotate() (Triangle) o.rotate() t.draw() s.draw() Object Object() equals (object ) toString () clone(object) getClass() … • Compiler Error • o.move() • s.rotate() … Triangle t = new Triangle (); Shape s = (Shape) t; Object o = (Object) t; … Shape Triangle Shape() move() draw() t draw() rotate() s • You can call a method on an object only if the class of the reference variable has that method! o

  10. The object contract • For an object of type Triangle • Everything accessible in class Triangle defines part of your contract • Everything accessible in class Shape defines part of your contract • Everything accessible in class Object defines part of your contract • Accessible: public or protected Object Object() equals (object ) toString () clone(object) getClass() … Shape Shape (x, y, dx, dy, color) move() draw() Triangle draw () rotate() Circle draw () Square draw () rotate() Shape Triangle Shape() move() draw() t draw() rotate()

  11. Multiple inheritance I • What if I want to create an object that fulfills the contracts of two separate (unrelated) existing classes? • This would allow us to use all the existing code that works for either method • Example: GUI_Card • It’s a card, and can be used with existing Cardgame code • It also needs to be displayed on the GUI, and is essentially just a Square • We want the following to be true: • GUI_Card IS-A Card • GUI_Card IS-A Square Square draw () rotate() Card show() GUI_Card draw () rotate()

  12. Multiple inheritance: Here be dragons! • What if both superclasses have a member with the same name? • How do we avoid ambiguity? • Extra syntax? • How do we help check for errors? • Basically, this is hard • Some languages allow this (C++, for one) • C++ is all about handling hard • Some languages don’t (Java, for one) • Java is all about the simple • Java does provide limited multiple inheritance Square draw () rotate() Card show() draw() GUI_Card draw () rotate() • Which draw() superclass method do I inherit?!?

  13. Interfaces • Java allows multiple inheritance to interface classes • Interface classes are: • 100% pure abstract class • They contain publicabstract method headers • Technically can also contain final static fields (Danger of ambiguity!) • The interface defines the contract but forces the subclass object to define the one and only one implementation. Shape draw () move() Card show() draw() GUI_Card draw () rotate() move() public interface Shape { public abstract void move (); public abstract void draw (); } // end interface Shape public class GUI_Card extends Card implements Shape { // Implementation must override move/draw } // end class GUI_Card

  14. Resolving ambiguity • Java protects you from most sources of ambiguity, but not all! public interface One { public final static int number = 1; } // end interface One public interface Two{ public final static int number = 2; } // end interface One Class MyClass implements One, Two { void do () { System.out.println(number); } // end method do } // end interface One Reference to number is ambiguous Class MyClass implements One, Two { public final static int number = One.number; … } // end interface One Resolve ambiguity manually!

  15. Contract Style • When do you make a class, a subclass, an abstract class, or an interface? • Class • Appropriate for objects that don’t extend anything • Fails the IS-A test for all other types (except Object) • Subclass (extend a class) • Only when you need to make a more specific version of a class • Override or add new behaviors • Abstract class • Define a template/contract for a group of subclasses • Has some implementation code that all subclasses can use • Guarantee that nobody can make an object of that type • Interface • Define a role that other classes can play, regardless of their type

More Related