CS110- Lecture 20 May 4, 2005 Agenda Wrapper Classes Autoboxing and Unboxing Binding Polymorphism Polymorphism via inheritance Polymorphism via interfaces 6/30/2016 CS110-Spring 2005, Lecture 20 1 Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class 6/30/2016 byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean void Void CS110-Spring 2005, Lecture 20 2 Wrapper Classes The following declaration creates an Integer object which represents the integer 40 as an object Integer age = new Integer(40); An object of a wrapper class can be used in any situation where a primitive value will not suffice 6/30/2016 CS110-Spring 2005, Lecture 20 3 Wrapper Classes For example, some objects serve as containers of other objects Primitive values could not be stored in such containers, but wrapper objects could be Wrapper classes also contain static methods that help manage the associated type 6/30/2016 CS110-Spring 2005, Lecture 20 4 Wrapper Classes For example, the Integer class contains a method to convert an integer stored in a String to an int value: int num = Integer.parseInt(str); The wrapper classes often contain useful constants as well For example, the Integer class contains MIN_VALUE and MAX_VALUE which hold the smallest and largest int values 6/30/2016 CS110-Spring 2005, Lecture 20 5 Autoboxing Autoboxing is the automatic conversion of a primitive value to a corresponding wrapper object: Integer obj; int num = 42; obj = int; The assignment creates the appropriate Integer object 6/30/2016 CS110-Spring 2005, Lecture 20 6 Unboxing The reverse conversion (called unboxing) also occurs automatically as needed 6/30/2016 CS110-Spring 2005, Lecture 20 7 Binding Consider the following method invocation: obj.doIt(); At some point, this invocation is bound to the definition of the method that it invokes If this binding occurred at compile time, then that line of code would call the same method every time However, Java defers method binding until run time -this is called dynamic binding or late binding Late binding provides flexibility in program design 6/30/2016 CS110-Spring 2005, Lecture 20 8 Polymorphism Polymorphism is an object-oriented concept that allows us to create versatile software designs. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects at different points in time 6/30/2016 CS110-Spring 2005, Lecture 20 9 Polymorphism The method invoked through a polymorphic reference can change from one invocation to the next All object references in Java are potentially polymorphic 6/30/2016 CS110-Spring 2005, Lecture 20 10 Polymorphism Suppose we create the following reference variable: Occupation job; Java allows this reference to point to an Occupation object, or to any object of any compatible type This compatibility can be established using inheritance or using interfaces 6/30/2016 CS110-Spring 2005, Lecture 20 11 Polymorphism Careful use of polymorphic references can lead to elegant, robust software designs 6/30/2016 CS110-Spring 2005, Lecture 20 12 References and Inheritance An object reference can refer to an object of its class, or to an object of any class related to it by inheritance Holiday Holiday day; day = new Christmas(); Christmas 13 References and Inheritance Assigning a child object to a parent reference is considered to be a widening conversion, and can be performed by simple assignment Assigning an parent object to a child reference can be done also, but it is considered a narrowing conversion and must be done with a cast 14 Polymorphism via Inheritance It is the type of the object being referenced, not the reference type, that determines which method is invoked Suppose the Holiday class has a method called celebrate, and the Christmas class overrides it. 6/30/2016 CS110-Spring 2005, Lecture 20 15 Polymorphism via Inheritance Now consider the following invocation: day.celebrate(); If day refers to a Holiday object, it invokes the Holiday version of celebrate; if it refers to a Christmas object, it invokes the Christmas version 6/30/2016 CS110-Spring 2005, Lecture 20 16 Polymorphism via Interfaces An interface name can be used as the type of an object reference variable Speaker current; The current reference can be used to point to any object of any class that implements the Speaker interface 6/30/2016 CS110-Spring 2005, Lecture 20 17 Polymorphism via Interfaces The version of speak that the following line invokes depends on the type of object that current is referencing current.speak(); Suppose two classes, Philosopher and Dog, both implement the Speaker interface, providing distinct versions of the speak method 6/30/2016 CS110-Spring 2005, Lecture 20 18 Polymorphism via Interfaces In the following code, the first call to speak invokes one version and the second invokes another: Speaker guest = new Philospher(); guest.speak(); guest = new Dog(); guest.speak(); 6/30/2016 CS110-Spring 2005, Lecture 20 19