Polymorphism: Inheritance Interfaces 256
Recap Finish Deck class methods Questions about assignments? Review Player class for HW9 (starter zip posted) Lessons Learned: Arrays of objects require multiple object creation: new for the array memory, new for each object that goes into it (see Deck.java example). Warm-up: Piazza poll: @352 Class Relationships 257
Polymorphism literally: many forms key feature of object oriented programming method overloading: same name, different parameters (number and/or type) Inheritance creates a new [sub, derived] class by extending existing [super, base] one method overriding: redefine method in subclass that exists in superclass Interface defines common set of operations implementations differ from class to class 258
Class Relationships USES: ClassA uses ClassB when ClassA has static data members or methods with local variables of type ClassB. For example, many of our main classes use the Scanner class and the String class. HAS: ClassA has one or more instance data members of type ClassB. This could be class composition (single members) or aggregation (array of ClassB) IS: ClassA is a subclass (inheritance based derivation) of ClassB. For example, Poodle is a sub-class of Dog. 259
Relationship Examples Let's revisit the poker tournament example... Tournament has Tables (aggregation) Table has Dealer, Players, Deck Person has String, Table or just table number? Dealer is Person, has Deck Player is Person, has Hand CardSet has Cards Deck is CardSet, uses Random Hand is CardSet Card has String 260
UML Class Diagram (unified modeling language) Person IS-A Each class is represented by a rectangle Player Dealer An arrow goes from each sub-class to its base (IS) Hand HAS-A Deck HAS-A A diamond connects each class to those it contains (HAS) IS-A CardSet HAS-A Card 261
Recall: Object class in JAVA all java classes are derived from Object directly or indirectly contains these methods (plus others): String tostring() returns class name + memory address boolean equals(object o) compares memory addresses (same as ==) Object clone() returns copy of object we override these methods (tostring & equals especially) in our classes to get more appropriate behaviors than the provided default 262
Inheritance We can build new classes by extending existing ones The subclasses inherit all the members of the superclass Protected members of the superclass can be accessed directly in the subclass, but private members cannot Subclasses inherit the private members (ie, their objects contain the private data), but they can't operate on them directly. We use super to call superclass constructors to initialize subclass objects, and also to refer to other inherited members in some cases. 263
Inheritance Terminology These are all different words and phrases for the same things in an inheritance relationship: ClassA -> IS a -> ClassB Subclass -> is a subset of -> Superclass Derived class -> is derived from -> Base class Child class -> inherits from -> Parent class 264
Inheritance Pic public class Parent { private char init; protected double money; public Parent(char c, double d) { this.init = c; this.money = d; } } public class Child extends Parent { private int val; public Child(char c, double d, int n) { this.super(c, d); this.money = 2 * this.money; this.val = n; } } Parent p = new Parent( P, 3500.00); Child c = new Child( S, 20.20, 6); init P c val 6 init S p money 3500.0 money 40.4 265
Interfaces They are collections of operations, specified through method headers, and comments All methods are public by default Interfaces may contain some data it is always public and static by default We write classes to implement an interface When implementing an interface, a class must have definitions for every method in the interface An "abstract" method is one without a definition (as in an interface) A class can implement multiple interfaces, and multiple classes usually implement each interface We cannot use new to create an object of an interface type they don't have constructors! 266
Shapes Polymorphism Example Let's implement these together... implements Interface: Shape.java implements Circle.java Parallelogram.java (base class) Has ShapeTest.java main program Is-a Rectangle.java (derived & base class) Is-a Square.java (derived class) 267
Polymorphism related reserved words extends: to say subclass extends base class super: to call base class constructor or base class version of an over-ridden method in subclass instanceof: to see if an object belongs to a particular class protected: to give access to derived classes, access to all other classes in the same package interface: to define an interface implements: to say a class will have method definitions to support an interface 268
Shapes Exercise Download ShapeTest.java from the schedule, along with the starter classes We'll write a Shape interface together, and then you'll update classes Circle, Parallelogram and Rectangle to implement it Use as little code as possible, making the most of inheritance instead 269
Poly References & Objects MyFace face = new MyBase(4); implements (MyFace <---------- MyBase) MyFace MyBase value: 4 MyBase base = new MyBase(5); MyBase MyBase value: 5 MySub sub = new MySub(6, 7); MySub MySub data: 7 MyBase value: 6 MyBase bsub = new MySub(8, 9); (MyBase extends MySub) MyBase MySub data: 9 MyBase value: 8 270
Comparable interface in JAVA API Contains one method: int compareto (Object param) Should return a negative int if this < param, 0 if same, positive int if this > param String class implements Comparable for example We implement this interface in lots of classes, particularly if we might want to sort a collection of those objects Example: let's update Card to implement Comparable 271