CS100J, Fall 2001 Tues 11/20 Lecture 23 ------------------------------------------------------------------------------- Announcements: + Prelim 3 tonight + E10 due Tues 11/27 (deals with P6) + MATLAB next week ("coincidentally", more quizzes... :-) ------------------------------------------------------------------------------- Topics: + polymorphism + types of classes (concrete, abstract, interface) ------------------------------------------------------------------------------- Summary: + inheritance and encapsulation + Object class + OOP means inheritance, encapsulation, and polymorphism so what's polymorphism? ------------------------------------------------------------------------------- Polymorphism: + Greek for "many forms" + For programming, 2 definitions: Savitch: use overriden method associated with an extended class (pp 500-502) Java makers: object may have many types (I like the makers' version better, but both are right) ------------------------------------------------------------------------------- How Polymorphism works: ___ + A class "B" is a class "A". / A \ Say, "an object made from B can be an A". \___/ ex) A Horse is a Mammal. ex) A Ford is a Car. ex) A Penny is a Coin. ___ ex) A Peon is a Worker, who is a Person. / B \ \___/ So, a super reference reference may refer to a subclass object! ___ / C \ \___/ + Syntax: no inheritance: superclass var = new ______________ inheritance: superclass var = new ______________ ex) "An integer is also a double." (double) so, think "an integer can be a double" so, "integer extends double" so, $double x = 7$ is an OK assignment! (int) + Reverse syntax? subclass var = (subclass) superclass(...) ex) must use a cast to take "portion" of superclass object so, $int x = (int) 7.7$ is an OK assignment! With general objects though, Java will get upset if you try to run (generates an exception) because "is a" hard to model in reverse (Are all Cars also Fords? No!) ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- More Practice with Polymorphism: ex) Coins and Pennies/Doubles and Integers: Think Coin c = new Coin() or an integer Think Penny p = new Penny() or a double Can an "A" be a "B"? - are all coins also pennies? - are all doubles also integers? Hierarchy: Can a "B" be an "A"? - are all pennies also coins? - are all integers also doubles? Two forms for reference types of A: A a1 = new ___________ ; For Coins, Coin c1 = new ____________ ; A a2 = new ___________ ; For Coins, Coin c2 = new ____________ ; see also poly_basics.txt and inherit_detailed0 ------------------------------------------------------------------------------- Aliases: + example: A a = new A(); // nothing new B b = new B(); // nothing new + can $a$ get $b$? or $b$ get $a$? Reminder: a ref to a superclass object may also refer to any subclass object So, ______________________ + graphical: a-var a-object A-class b-var b-object B-class + code: class A {} class B extends A {} public class poly_aliases { public static void main(String[] args) { A a1 = new A(); A a2 = new A(); B b1 = new B(); B b2 = new B(); // Since a B can be an A, could have said $A a1 = new B()$. So: a1 = b1; // But, the reverse is "difficult" for Java: // b2 = (B) a2; // compiles, but won't run } } ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- Effect on variables and methods: + (RULE 2) Java accesses an instance variable based on reference type + (RULE 3) Java invokes a method based on class of actual object class Employee { public int salary; public Employee(int s) { salary = s; } public int getSalary() { return salary; } } class Peon extends Employee { public int salary; public Peon(int s) { super(s); salary = s/10; } public int getSalary() { return salary; } } public class poly_varsmeths { public static void main(String[] args) { Employee E = new Peon(100); Peon P = new Peon(100); System.out.println(E.salary); // _____ System.out.println(E.getSalary()); // _____ System.out.println(P.salary); // _____ System.out.println(P.getSalary()); // _____ } } ------------------------------------------------------------------------------- Arrays + advantage: dynamic method lookup (rule for methods) - can collect many different subclass objects - can then call same method name on each - the "rules" handle which object to access! aren't they great? see poly_arrays0,1,2 ------------------------------------------------------------------------------- Abstract classes: + classes so far have been CONCRETE - all members defined + ABSTRACT class - class with abstract ideas (methods with headers but no bodies) - helps build class hierarchies by serving as placeholders - software engineering also: high-level teams make abstract classes and peons make concrete versions + class becomes abstract when at least (maybe even all) 1 method is abstract: - syntax: $abstract modifier returntype name(parameters);$ - note: no body and use of keyword $abstract$ + rules: - must use $abstract$ as class modifier - at least 1 method must be abstract - no $final$ or $static$ abstract methods allowed! (subclasses need to override or inherit the abstract methods) - cannot instantiate object from abstract class - can use the abstract class as type, though! - concrete class must override all abstract members, otherwise the subclass is abstract, too see abstract0,1 and next page ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- abstract class Worker { public final int BASE=1000; protected int salary; abstract public int getSalary(); } abstract class Peon extends Worker {} class Boss extends Worker { public int getSalary() { return BASE*5; } } class Owner extends Boss { public int getSalary() { return BASE*10; } } class StudentWorker extends Peon { public int getSalary() { return BASE*2; } } class FullTimeWorker extends Peon { public int getSalary() { return BASE*3; } } public class abstract0 { public static void main(String[] args) { Worker[] p = { new StudentWorker(), new FullTimeWorker(), new Boss(), new Owner() }; for (int i = 0; i < p.length; i++) { System.out.print(p[i].getClass()); System.out.println(": "+p[i].getSalary()); } } } /* Output: * class StudentWorker: ______ * class FullTimeWorker: ______ * class Boss: ______ * class Owner: ______ */ ------------------------------------------------------------------------------- Interfaces: + completely abstract classes + may only have constants and abstract methods + like defining a generic type + subclasses IMPLEMENT 1 or more interfaces syntax: class name implements interface1,interface2,.... { overriden methods } + a subclass that implements an interface must override the abstract methods for each interface + multiple inheritance: - obtain features from multiple superclasses - Java does not provide with $extends$ - can use interfaces, though see interface_basics.java -------------------------------------------------------------------------------