CS100J, Spring 2001 Tues 4/17 Lecture 23 ------------------------------------------------------------------------------- Announcements: + Prelim 3 tonight + Makeup 5/3 (room TBD) + E11 due Thurs + Syllabus updated (drop deadline, moving Java I/O to Thurs I think) + P6 posted on Thurs ------------------------------------------------------------------------------- Topics: + polymorphism + types of classes (concrete, abstract, interface) ------------------------------------------------------------------------------- Summary from Lecture 22: + 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. _|_ / B \ \___/ + language of Java: - a ref to an object can have class of object or any of the object's superclasses + syntax: superclass var = new subclass(...) ex) think "an integer can be a double" so, "integer extends double" so, $double x = 7$ is an OK assignment! + 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 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!) see poly_basics and inherit0_detailed (a couple lectures ago) ------------------------------------------------------------------------------- Aliases: + ex) A a = new A(); B b = new B(); Since a B can be an A, we can say $A a1 = new B()$, so: a = b; + graphical a <- a-object <- A | \ | | b <- b-object <- B see poly_aliases ------------------------------------------------------------------------------- Effect on variables and methods: + Java accesses an instance variable based on reference type + Java invokes a method based on class of actual object see poly_varsmeths ------------------------------------------------------------------------------- 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 pions 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 - concrete class must override all abstract members, otherwise the subclass is abstract, too see abstract0,1 ------------------------------------------------------------------------------- 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 -------------------------------------------------------------------------------