Abstract Classes Abstract Classes r Java allows abstract
Abstract Classes
Abstract Classes r Java allows abstract classes m use the modifier abstract on a class header to declare an abstract class Vehicle { … } r An abstract class is a placeholder in a class hierarchy that represents a generic concept Vehicle Car Boat Plane 2
Abstract Class: Example r An abstract class often contains abstract methods, though it doesn’t have to m Abstract methods consist of only methods declarations, without any method body public abstract class Vehicle { String name; public String get. Name() { return name; } \ method body abstract public void move(); \ no body! } 3
Abstract Classes r An abstract class often contains abstract methods, though it doesn’t have to m Abstract methods consist of only methods declarations, without any method body r The non-abstract child of an abstract class must override the abstract methods of the parent r An abstract class cannot be instantiated (why? ) r The use of abstract classes is a design decision; it helps us establish common elements in a class that is too general to instantiate 4
Referencing Objects
Recap: Object References r All interaction with an object occurs through object reference variables r An object reference variable holds the reference (address, the location) of an object Chess. Piece bishop 1 = new Chess. Piece(); bishop 1 6
Recap: Primitive Assignment r The act of assignment takes a copy of a value and stores it in a variable r For primitive types: num 2 = num 1; Before After num 1 num 2 5 12 5 5 7
Recap: Reference Assignment r For object references, the reference is copied: bishop 2 = bishop 1; Before bishop 1 bishop 2 After bishop 1 bishop 2 8
Recap: Relationship Between Objects and Object References r Two or more references can refer to the same object; these references are called aliases of each other r One object (and its data) can be accessed using different references 9
References and Inheritance r An object reference can refer to an object of its class, or to an object of any class derived from it by inheritance r For example, if the Holiday class is used to derive a child class called Christmas, then a Holiday reference could actually be used to point to a Christmas object Holiday Christmas Holiday day; day = new Holiday(); … day = new Christmas(); 10
References and Inheritance r Assigning an object to an ancestor reference is considered to be a widening conversion, and can be performed by simple assignment Holiday = new Christmas(); r Assigning an ancestor object to a reference can also be done, but it is considered to be a narrowing conversion and must be done with a cast Christmas c 1 = new Christmas(); Holiday = c 1; Christmas c 2 = (Christmas) day; r The widening conversion is the most useful m for implementing polymorphism 11
Referencing and Inheritance
Recap: References and Inheritance r An object reference variable can refer to any object instantiated from m its own class, or m any class derived from it by inheritance r For example, Holiday Christmas Holiday day; day = new Holiday(); … day = new Christmas(); The assignment of an object of a derived class to a reference variable of the base class can be considered as a widening conversion 13
References and Inheritance r Through a given type of reference variable, we can invoke only the methods defined in that type class Holiday { Holiday day; day = new Christmas(); public void celebrate() {…} } class Christmas extends Holiday { public void celebrate() {…} public void listen. To. Christmas. Songs() {…} } Can we do the following statements: day. celebrate(); day. listen. To. Christmas. Songs(); 14
References and Inheritance r We can “promote” an object back to its original type through an explicit narrowing cast: Holiday = new Christmas(); day. celebrate(); … Christmas c = (Christmas) day; c. listen. To. Christmas. Songs(); Question: which celebrate() will be invoked by the line: day. celebrate(); 15
Polymorphism
What is Polymorphism? r A polymorphic reference can refer to different types of objects at different times m In java every reference can be polymorphic except of references to base types and final classes. r It is the type of the object being referenced, not the reference type, that determines which method is invoked m Polymorphic references are therefore resolved at runtime, not during compilation; this is called dynamic binding r Careful use of polymorphic references can lead to elegant, robust software designs 17
Polymorphism r Polymorphism: A polymorphic reference v is declared as class C, but unless C is final or base type, v can refer to an object of class C or to an object of any class derived from C. r A method call v. <method_name>(<args>) invokes a method of the class of an object referred to by v (not necessarily C): Ex 1: Holiday = new Christmas(); day. celebrate(); … Ex 2: void process(Holiday day) { … day. celebrate(); … } Christmas day =. . . ; process(day) r A very common usage of polymorphism: If classes C 1, C 2, . . , Cn are all derived from C, define an array A of elements of C. The entries A[i] can then refer to objects of classes C 1, . . , Cn. 18
The pay-roll of a firm Staff staff. List : staff. Memeber[] Staff. Memeber[] --staff. List: + payday() : void Method payday() iterates over elements s of staff. List and calls s. pay() on each s. Staff. Member # name : String # address : String # phone : String + to. String() : String + pay() : double Employee Volunteer # social. Security. Number : String # pay. Rate : double Method payday() also calls println(s) on each s. This works because println is defined as: void println(Object o) {String s =o. to. String()); Out. Stream. out(s); } + to. String() : String + pay() : double Executive - bonus : double + award. Bonus(exec. Bonus : double) : void + pay() : double Hourly - hours. Worked : int + add. Hours(more. Hours : int) : void + to. String() : String + pay() : double 19
Single vs. Multiple Inheritance r Some object-oriented languages allow multiple inheritance, which allows a class to be derived from two or more classes, inheriting the members of all parents r The price: collisions, such as the same variable name, same method name in two parents, have to be resolved r Java decision: single inheritance, meaning that a derived class can have only one parent class 20
Interfaces
Java Interface r A Java interface is a collection of constants and abstract methods m abstract method: a method header without a method body; we declare an abstract method using the modifier abstract m since all methods in an interface are abstract, the abstract modifier is usually left off r Methods in an interface have public visibility by default 22
Interface: Syntax interface is a reserved word public interface Doable { public static final String NAME; public void do. This(); int do. That(); void do. This 2 (float value, char ch); boolean do. The. Other (int num); } A semicolon immediately follows each method header No method in an interface has a definition (body) 23
Implementing an Interface r A class formally implements an interface by m stating so in the class header in the implements clause m a class can implement multiple interfaces: the interfaces are listed in the implements clause, separated by commas r If a class asserts that it implements an interface, it must define all methods in the interface or the compiler will produce errors 24
Implementing Interfaces public class Something implements Doable { public void do. This () implements is a { reserved word // whatever } public void do. That () { // whatever } Each method listed in Doable is given a definition // etc. } public class Many. Things implements Doable, Another. Doable 25
Interfaces: An Example r A class that implements an interface can implement other methods as well 26
UML Diagram <<interface>> Complexity Mini. Quiz + get. Complexity () : int + set. Complexity (int) : void 1 + main(args : String[]) : void 2 Question + get. Question () : String + get. Answer () : String + answer. Correct (String) : boolean + to. String() : String 27
Interfaces: Examples from Java Standard Class Library r The Java Standard Class library defines many interfaces: m the Iterator interface contains methods that allow the user to move through a collection of objects easily • has. Next(), next(), remove() m the Comparable interface contains an abstract method called compare. To, which is used to compare two objects if (obj 1. compare. To(obj 2) < 0) System. out. println(“obj 1 is less than obj 2”); 28
Polymorphism via Interfaces r Define a polymorphism reference through interface m declare a reference variable of an interface type Doable obj; m m the obj reference can be used to point to any object of any class that implements the Doable interface the version of do. This depends on the type of object that obj is referring to: obj. do. This(); 29
Example: Polymorphism via Interface r The payroll program revisited: we want to sort the employees by name 30
More Examples Speaker guest; guest = new Philosopher(); guest. speak(); guest = Dog(); guest. speak(); Speaker special; special = new Philosopher(); special. pontificate(); // compiler error Speaker special; special = new Philosopher(); ((Philosopher)special). pontificate(); public interface Speaker { public void speak(); } class Philosopher extends Human implements Speaker { // public void speak() {…} public void pontificate() {…} } class Dog extends Animal implements Speaker { // public void speak() { … } } 31
Interface Hierarchies r Inheritance can be applied to interfaces as well as r r classes One interface can be used as the parent of another The child interface inherits all abstract methods of the parent A class implementing the child interface must define all methods from both the parent and child interfaces Note that class hierarchies and interface hierarchies are distinct (they do not overlap) 32
- Slides: 32