Download presentation
Presentation is loading. Please wait.
Published byRhoda Holmes Modified over 9 years ago
1
Chapter FifteenModern Programming Languages1 A Second Look At Java
2
Chapter FifteenModern Programming Languages2 Subtype Polymorphism Does this declare x to be a reference to an object of the Person class? Not exactly—the type Person may include references to objects of other classes n Java has subtype polymorphism Person x;
3
Chapter FifteenModern Programming Languages3 Interfaces n A method prototype just gives the method name and type—no method body n An interface in Java is a collection of method prototypes public interface Drawable { void show(int xPos, int yPos); void hide(); }
4
Chapter FifteenModern Programming Languages4 Implementing Interfaces n A class can declare that it implements a particular interface Then it must provide public method definitions that match those in the interface – But interface method declarations are always implicitly public (and abstract)
5
Chapter FifteenModern Programming Languages5 Examples “Interface Inheritance” public class Icon implements Drawable { public void show(int x, int y) { … method body … } public void hide() { … method body … } …more methods and fields… } public class Square implements Drawable, Scalable { … all required methods of all interfaces implemented … }
6
Chapter FifteenModern Programming Languages6 Why Use Interfaces? n An interface can be implemented by many (unrelated) classes: n Interface name can be used as a reference type: public class Window implements Drawable … public class MousePointer implements Drawable … public class Oval implements Drawable … Drawable d; d = new Icon("i1.gif"); d.show(0,0); d = new Oval(20,30); d.show(0,0);
7
Chapter FifteenModern Programming Languages7 Interfaces as Capabilities n Implement multiple interfaces n Interface names are often adjectives – They describe capabilities n Example: AbleTest.java
8
Chapter FifteenModern Programming Languages8 Interface Inheritance n A commitment to implement a contract n No implementation is inherited n Disadvantage: – No code sharing n Advantage: – No code commitment – Freedom to implement any way you want
9
Chapter FifteenModern Programming Languages9 Extending Interfaces n An interface can extend another n The result is the union of all the declared methods interface PersistentStack extends Stack, Persistent {} class DynamicStack implements PersistentStack {…}
10
Chapter FifteenModern Programming Languages10 Inheritance n When a subclass extends its superclass, it inherits all its methods and fields – “Implementation Inheritance” – A lesser degree of separation of interface from implementation n (Nothing like this happens with interfaces—when a class implements an interface, all it gets is an obligation) n In addition to inheritance, you also get polymorphism
11
Chapter FifteenModern Programming Languages11 Inheritance Chains n A derived class can have more classes derived from it n All classes but one are derived from some class If you do not give an extends clause, Java supplies one: extends Object Object is the ultimate base class in Java
12
Chapter FifteenModern Programming Languages12 public class Icon { private int x,y; private int width; private int height; private Gif image; public void move (int newX, int newY) { x = newX; y = newY; } public Gif getImage() { return image; } } public class Label { private int x,y; private int width; private int height; private String text; public void move (int newX, int newY) { x = newX; y = newY; } public String getText() { return text; } } Two classes with a lot in common—but neither is a simple extension of the other.
13
Chapter FifteenModern Programming Languages13 public class Icon extends Graphic { private Gif image; public Gif getImage() { return image; } } public class Label extends Graphic { private String text; public String getText() { return text; } } Common code and data have been factored out into a common base class. public class Graphic { protected int x,y; protected int width,height; public void move(int newX, int newY) { x = newX; y = newY; } }
14
Chapter FifteenModern Programming Languages14 The Liskov Substitution Principle n An is-a relationship means that the subtype must be in all cases substitutable for a supertype – All depends on the methods involved – Think of the methods and their preconditions and postconditions as a contract – Subclasses must adhere to the contract – Can weaken the preconditions and/or strengthen postconditions (“require no more, promise no less”) n Don’t go crazy with inheritance n Prefer composition over inheritance
15
Chapter FifteenModern Programming Languages15 Extending And Implementing Classes can use extends and implements together n For every class, the Java language system keeps track of several properties, including: A: the interfaces it implements B: the methods it is obliged to define C: the methods that are defined for it D: the fields that are defined for it
16
Chapter FifteenModern Programming Languages16 Simple Cases For A Class n A method definition affects C only n A field definition affects D only An implements part affects A and B – All the interfaces are added to A – All the methods in them are added to B A: the interfaces it implements B: the methods it is obliged to define C: the methods that are defined for it D: the fields that are defined for it
17
Chapter FifteenModern Programming Languages17 Tricky Case For A Class An extends part affects all four: – All interfaces of the base class are added to A – All methods the base class is obliged to define are added to B – All methods of the base class are added to C – All fields of the base class are added to D A: the interfaces it implements B: the methods it is obliged to define C: the methods that are defined for it D: the fields that are defined for it
18
Chapter FifteenModern Programming Languages18 Multiple Inheritance n In some languages (such as C++) a class can have more than one base class n Seems simple at first: just inherit fields and methods from all the base classes n For example: a multifunction printer
19
Chapter FifteenModern Programming Languages19 Collision Problem n The different base classes are unrelated, and may not have been designed to be combined Scanner and Fax might both have a method named transmit When MultiFunction.transmit is called, what should happen?
20
Chapter FifteenModern Programming Languages20 Diamond Problem n A class may inherit from the same base class through more than one path If A defines a field x, then B has one and so does C Does D get two of them?
21
Chapter FifteenModern Programming Languages21 Solvable, But… n A language that supports multiple inheritance must have mechanisms for handling these problems n Can be tricky n The question is, is the additional power worth the additional language complexity? n Java’s designers did not think so
22
Chapter FifteenModern Programming Languages22 Living Without Multiple Inheritance One benefit of multiple inheritance is that a class can have several unrelated types (like Copier and Fax ) n This can be done in Java by using interfaces: a class can implement any number of interfaces n Another benefit is inheriting implementation from multiple base classes n This is harder to accomplish with Java
23
Chapter FifteenModern Programming Languages23 public class MultiFunction { private Printer myPrinter; private Copier myCopier; private Scanner myScanner; private Fax myFax; public void copy() { myCopier.copy(); } public void transmitScanned() { myScanner.transmit(); } public void sendFax() { myFax.transmit(); } … } Forwarding
24
Chapter FifteenModern Programming Languages24 Python MI Solution n When resolving the binding of an identifier: – 1) Look first in the object itself – 2) Then in its class – 3) Then in all base classes in declaration order – 4) Repeat 3 recursively as needed n Order of declaration of base classes matters!
25
Chapter FifteenModern Programming Languages25 Generics n Parametric polymorphism – Automatic in dynamically typed languages – (Why?) n Type variables in ML, Haskell n Templates in C++, D – Code generation n Generics in Java, C#, Ada, Eiffel – varying degrees of flexibility
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.