138x Filetype PDF File size 0.22 MB Source: www.cs.cornell.edu
Object-oriented Programming - II Overview Reference: • Single implementation inheritance, multiple interface inheritance A Programmer's Guide to Java Certification: A Comprehensive Primer and supertypes Chapter 4 & Chapter 6 • Assigning, casting and passing references •The instanceof operator • Polymorphism and dynamic method lookup • Choosing between inheritance and aggregation •Encapsulation • Packages: usage and definition • Accessibility modifiers for classes and interfaces: default and private • Member accessibility modifiers: , , default and public protected private • Abstract classes and methods • Final classes, members and parameters CS211, Spring 2000. KAM OOP-II.FM 1/96 CS211, Spring 2000. KAM Object-oriented Programming 2/96 Interfaces Interfaces: Programming by Contract • Java provides interfaces which allow new type names to be introduced and used polymorphically, and also permit multiple interface inheritance. • Interfaces support programming by contract. CS211, Spring 2000. KAM Object-oriented Programming 3/96 CS211, Spring 2000. KAM Object-oriented Programming 4/96 Defining Interfaces Design Inheritance: Implementing Interfaces • An interface defines a contract by specifying prototypes of methods, and not • A class specifies the interfaces it implements as a comma-separated list using their implementation. the clause in the class header. implements{ } interface ISwitch { // method prototypes «interface» • An interface is abstract by definition and therefore cannot be instantiated. It void switchOn(); ISwitch Object void switchOff(); should also not be declared abstract. boolean isOn(); • Reference variables of the interface type can be declared. } class Light implements ISwitch { • : Java allows to be defined by using . // Method implemenations Light Pure Design contracts interfaces public void switchOn() {...} «interface» public void switchOff() {...} interface ISwitch { // method prototypes ISwitch public boolean isOn() {...} void switchOn(); //... void switchOff(); } () TubeLight boolean isOn(); switchOn class TubeLight extends Light {...} () } switchOff () isOn LightBulb TubeLight ISwitch Note that subclasses and also implement the interface. CS211, Spring 2000. KAM Object-oriented Programming 5/96 CS211, Spring 2000. KAM Object-oriented Programming 6/96 A Word on Typing Supertypes and Subtypes Type rules determine which type of values (objects) can be manipulated by which type of • Interfaces define new types. variables (names). • Although interfaces cannot be instantiated, variables of an interface type can be • A variable of type T can denote any object of type T or a subtype of T. declared. Light spotlight = new Light(); • If a class implements an interface, then references to objects of this class and its TubeLight ceilingLight = new TubeLight(); subclasses can be assigned to a variable of this interface type. ISwitch toggle = ceilingLight; • The interfaces that a class implements and the classes it extends, directly or indirectly, are called its supertypes. name: spotlight name: ceilingLight name: toggle • The class is then a subtype of its supertypes. type: Light type: TubeLight type: ISwitch • A supertype is thus a reference type. • Interfaces with empty bodies are often used as markers to “tag” classes as having a certain property or behavior (java.io.Serializable). :Light :TubeLight • Note that a class inherits only one implementation of a method, regardless of how many supertypes it has. • All interfaces are also subtypes of Object class. A supertype reference can denote objects of its type and of its subtypes. CS211, Spring 2000. KAM Object-oriented Programming 7/96 CS211, Spring 2000. KAM Object-oriented Programming 8/96 Example: Interface Types Remarks on Interfaces interface ISwitch { // method prototypes • Classes which interfaces, guarantee that they will honor the contract. «interface» implement public void switchOn(); ISwitch Object public void switchOff(); • A class can choose to implement only some of the methods of its interfaces, i.e. public boolean isOn(); give a partial implementation of its interfaces. } • The class must then be declared as abstract. class Light implements ISwitch { // Method implementations Light • Note that interface methods cannot be declared static, because they comprise public void switchOn() {...} the contract fulfilled by the objects of the class implementing the interface and public void switchOff() {...} public boolean isOn() {...} are therefore instance methods. //... • The interface methods will all have public accessibility when implemented in } TubeLight class TubeLight extends Light {...} the class (or its subclasses). // Some client ISwitch starLight = new TubeLight(); starLight.getTubeLight(); // Compile error starLight.switchOn(); starLight.equals(someOtherLight); • Type TubeLight is a of Light, ISwitch and Object. subtype – Light, ISwitch and Object are supertypes of type TubeLight. • Objects of a type can be denoted by reference variables of its supertypes. CS211, Spring 2000. KAM Object-oriented Programming 9/96 CS211, Spring 2000. KAM Object-oriented Programming 10/96 Extending Interfaces Kinds of Inheritance • An interface can extend other interfaces, using the extends clause. «interface» IStack Object • Unlike extending classes, an interface can extend several interfaces. • Multiple inheritance of interfaces can result in an inheritance hierarchy which push() pop() has multiple roots designated by different interfaces. • Note that there are three different inheritance relations at work when defining inheritance between classes and interfaces: «interface» ISafeStack StackImpl • Linear implementation inheritance hierarchy between classes: a class extends isFull() another class. isEmpty() push() pop() • Multiple inheritance hierarchy between interfaces: an interface extends other ... interfaces. • Multiple interface inheritance hierarchy between interfaces and classes: a class SafeStackImpl Linear implementation inheritance hierarchy between classes. implements interfaces. Multiple interface inheritance hierarchy between interfaces • There is only one single implementation inheritance into a class, which avoids isFull() and classes. many problems associated with general multiple inheritance. isEmpty() ... Multiple inheritance hierarchy between interfaces. Figure 1 Inheritance Relations CS211, Spring 2000. KAM Object-oriented Programming 11/96 CS211, Spring 2000. KAM Object-oriented Programming 12/96 Example 1 Interfaces interface ISafeStack extends IStack { // (5) boolean isEmpty(); interface IStack { // (1) boolean isFull(); void push(Object item); } Object pop(); class SafeStackImpl extends StackImpl implements ISafeStack { // (6) } class StackImpl implements IStack { // (2) public SafeStackImpl(int capacity) { super(capacity); } protected Object[] stackArray; public boolean isEmpty() { return tos < 0; } // (7) protected int tos; public boolean isFull() { return tos == stackArray.length-1; } // (8) } public StackImpl(int capacity) { public class StackUser { stackArray = new Object[capacity]; tos = -1; public static void main(String args[]) { // (9) } SafeStackImpl safeStackRef = new SafeStackImpl(10); public void push(Object item) // (3) StackImpl stackRef = safeStackRef; { stackArray[++tos] = item; } ISafeStack isafeStackRef = safeStackRef; IStack istackRef = safeStackRef; public Object pop() { // (4) Object objRef = safeStackRef; Object objRef = stackArray[tos]; safeStackRef.push("Dollars"); // (10) stackArray[tos] = null; stackRef.push("Kroner"); tos--; System.out.println(isafeStackRef.pop()); return objRef; System.out.println(istackRef.pop()); } System.out.println(objRef.getClass()); public Object peek() { return stackArray[tos]; } } } } CS211, Spring 2000. KAM Object-oriented Programming 13/96 CS211, Spring 2000. KAM Object-oriented Programming 14/96 Output from the program: Example: Interfaces Kroner Dollars SafeStackImpl safeStackRef :SafeStackImpl class SafeStackImpl ISafeStack isafeStackRef isEmpty(int) SafeStackImpl isFull() StackImpl stackRef ... IStack istackRef StackImpl push(Object) pop() Object objRef Object ... SafeStackImpl safeStackRef = new SafeStackImpl(10); StackImpl stackRef = safeStackRef; ISafeStack isafeStackRef = safeStackRef; IStack istackRef = safeStackRef; safeStackRef.push("Dollars"); Object objRef = safeStackRef; stackRef.push("Kroner"); System.out.println(isafeStackRef.pop()); istackRef.isEmpty(); // Compile error System.out.println(istackRef.pop()); stackRef.isFull(); // Compile error System.out.println(objRef.getClass()); CS211, Spring 2000. KAM Object-oriented Programming 15/96 CS211, Spring 2000. KAM Object-oriented Programming 16/96
no reviews yet
Please Login to review.