Review: Diagrams for Inheritance - String makemodel - int mileage + (String, int) Class #3: Inheritance & Polymorphism Software Design II (CS 220): M. Allen, 25 Jan. 18 + (String, int) <<update>> + void setmakemodel(string s) + void setmileage(int m) <<query>> + String getmakemodel() + int getmileage() int capacity + (String, int, int) <<update>> + void setcapacity(int c) <<query>> + int getcapacity() 2 Inheritance Structures s can be part of an inheritance hierarchy, with multiple levels of ancestors and descendants Every class in Java is descended from the class Type Conformance Every object conforms to the types of all its ancestors Every class in Java conforms to the type is a descendant of conforms to is an ancestor of and and conform to both and 3 4 1
Conformance Rules Any sub-class that conforms to another class can be used anywhere something of the original type can be used For example, if we have this method declaration: private void methodname( type1 var1, type2 var2 ) And then use it: object1 must conform to type1 methodname( object1, object2 ); object2 must conform to type2 Conformance Rules Often, we use strict conformance in our code For example, if we have this method declaration: public void setbounds( int x, int y, int w, int h ) We must use it with exactly the same types of inputs, since these are primitive types, with no conformance: setbounds( 20, 20, 100, 100 ); Conformance allows us to relax this requirement, using descendant classes instead 5 6 Using Conformance Once we have a set of sub-classes of a main class, we can do things like create an array of different objects Using Conformance Now that we have multiple types of in the array, we can write a method that works with multiple types at once v0, v1, v2, v3, v4; v0 = new ("Generic ", 0); v1 = new ("Toyota Prius", 100); v2 = new ("Honda Insight", 5000); v3 = new ("Mack ", 300, 20); v4 = new ("Ford F250", 1300, 2); [] lot = new [4]; lot[0] = v1; lot[1] = v2; lot[2] = v3; lot[3] = v4; variables get different types dynamically when created at run-time array now (appears to) have references to 3 different types of objects in it lot = new [5];... private void prints() System.out.println( lot[i].getmakemodel() ); Since any element of array conforms to the class, it will have access to the inherited method getmakemodel() 7 8 2
Conformance and Type-Checking Since we have declared the array of type, anything we try to do with objects in that array must be possible for any type that conforms to the class lot = new [5];... ERROR! private void prints() System.out.println( lot[i].getcapacity() ); Determining Specific Types Sometimes, we want to know something more specific about an object s type than we can know from the type of a variable or containing data-structure Java has instanceof operator to check conformance A boolean (binary, infix) operator Takes an object identifier and a type identifier as arguments Returns true if and only if the object conforms to the type Since only objects, specifically, have access to the method getcapacity(), this code will not compile 9 10 The instanceof Operator instanceof : true instanceof : true instanceof : false instanceof : true instanceof : false instanceof : false 11 Safe Casting of Conformant Types Once we know that an object conforms to some more specific type, we can use techniques like casting to use methods involving that type private void printcapacities() if ( lot[i] instanceof ) t = () lot[i]; System.out.println( t.getcapacity() ); else System.out.println( "Not a truck." ); Since we have used instanceof to check for the type, we can safely cast. Doing so will allow the object to call methods that do not belong to the more generic type. If we don t do the check first, the code will still compile, but the cast may fail at run-time! 12 3
Overriding Methods If we want to replace a method in a parent class we can create a new one to override it Must have same method signature: exact same access, name and list of parameters for input or output For example if the parent contains a method: public void dox( int y ) The child can override it by declaring own method with: 1. Same name: dox() 2. Same parameter type: int 3. Same output type: void 4. Same access: public Overriding Methods For instance, we might decide to override the parent method for setting the mileage, which allows any integer value we want: public void setmileage( int m ) mileage = m; We put a new version in the class, ensuring that the value is always between 0 and 500,000 Since the mileage public void setmileage( int m ) variable is private in if ( m < 0 ) the parent class, the m = 0; child version must else if ( m > 500_000 ) call the public method m = 500_000; that allows changing super.setmileage( m ); that variable, after setting the bounds on the value as desired 13 14 Types and Polymorphism Polymorphism: fr. Greek πολύμορῄοσ (polumorphos), meaning many shapes When you declare a variable, you assign its type As code runs, a polymorphic variable can appear to change type, based on objects assigned to it In each case, the compiler will check if the type of the assigned object conforms to that of the variable At runtime, the current type of the variable decides what methods and properties it will have 15 Polymorphism in Action A variable can be made to change type, since it is a reference that can be directed towards object of any conformant sub-type When we run a method on such a variable, it uses the version of any method provided by the actual type of the object to which it refers v0 = new ( "Generic, 0 ); v0.setmileage( -100 ); System.out.println( v0.getmakemodel() + ": " + v0.getmileage() ); v0 = new ( "Ford Focus", 100 ); v0.setmileage( -100 ); System.out.println( v0.getmakemodel() + ": " + v0.getmileage() ); Output: Generic : -100 Ford Focus: 0 On the first call to setmileage(), the actual type is, and we get the negative value (-100) On the second call to setmileage(), the actual type is now, and we get the minimum-bound value (0) 16 4
This Week Topic: Inheritance and polymorphism Class meetings: Monday Thursday: regular classroom Friday: in the CS Lab (16 Wing) Optional Reading: Chapter 5 & 6 in text Office Hours: Wing 210 Monday/Wednesday/Friday, 10:00 AM 11:00 AM Tuesday/Thursday, 1:30 PM 3:00 PM 17 5