Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance and Polymorphism

Similar presentations


Presentation on theme: "Inheritance and Polymorphism"— Presentation transcript:

1 Inheritance and Polymorphism
CS 4450 – Chapter 15 Chapter Fifteen Modern Programming Languages

2 Subtype Polymorphism Person x;
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 Java, C#, C++, Python, D all support subtype polymorphism Chapter Fifteen Modern Programming Languages

3 Interfaces A method prototype just gives the method name and type—no method body An interface in Java or C# is a collection of method prototypes public interface Drawable { void show(int xPos, int yPos); void hide(); } Chapter Fifteen Modern Programming Languages

4 Why Use Interfaces? An interface can be implemented by many (unrelated) classes: 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); Chapter Fifteen Modern Programming Languages

5 Interface Semantics A commitment to implement a contract
No implementation is inherited Disadvantage: No code sharing Advantage: No code commitment Freedom to implement any way you want Chapter Fifteen Modern Programming Languages

6 Extending Interfaces An interface can extend another one
The result is the union of all the declared methods (in Java, not quite in C++) interface PersistentStack extends Stack, Persistent {} class DynamicStack implements PersistentStack {…} Chapter Fifteen Modern Programming Languages

7 Implementation Inheritance
When a subclass extends its superclass, it inherits all its methods and fields A lesser degree of separation of interface from implementation (Nothing like this happens with interfaces—when a class implements an interface, all it gets is an obligation) In addition to inheritance, you also get subtype polymorphism Chapter Fifteen Modern Programming Languages

8 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; } } 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; } } What should we do? Two classes with a lot in common—but neither is a simple extension of the other. Chapter Fifteen Modern Programming Languages

9 public abstract class Graphic { protected int x,y; protected int width,height; public void move(int newX, int newY) { x = newX; y = newY; } } public class Label extends Graphic { private String text; public String getText() { return text; } } public class Icon extends Graphic { private Gif image; public Gif getImage() { return image; } } Graphic is an abstract class. Explain why inheriting from concrete classes is a bad idea. Common code and data have been factored out into a common base class (which usually should be declared abstract). Chapter Fifteen Modern Programming Languages

10 Multiple Inheritance In some languages (such as C++, Python Eiffel) a class can have more than one base class Seems simple at first: just inherit fields and methods from all the base classes For example: a multifunction printer Chapter Fifteen Modern Programming Languages

11 Collision Problem 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? Chapter Fifteen Modern Programming Languages

12 Diamond Problem 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? Chapter Fifteen Modern Programming Languages

13 Solvable, But… A language that supports multiple inheritance must have mechanisms for handling these problems Is the additional power worth the additional language complexity? Designers of Java, C#, and D did not think so Python has a simpler approach Chapter Fifteen Modern Programming Languages

14 Explicit Forwarding Simulating MI
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(); } … } Chapter Fifteen Modern Programming Languages

15 Python MI Solution 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 Order of declaration of base classes matters! Chapter Fifteen Modern Programming Languages


Download ppt "Inheritance and Polymorphism"

Similar presentations


Ads by Google