Inheritance and Polymorphism CS 4450 Chapter 15 Chapter

  • Slides: 15
Download presentation
Inheritance and Polymorphism CS 4450 – Chapter 15 Chapter Fifteen Modern Programming Languages 1

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

Subtype Polymorphism Person x; �Does this declare x to be a reference to an

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 2

Interfaces �A method prototype just gives the method name and type—no method body �An

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 x. Pos, int y. Pos); void hide(); } Chapter Fifteen Modern Programming Languages 3

Why Use Interfaces? �An interface can be implemented by many (unrelated) classes: public class

Why Use Interfaces? �An interface can be implemented by many (unrelated) classes: public class Window implements Drawable … public class Mouse. Pointer implements Drawable … public class Oval implements Drawable … �Interface name can be used as a reference type: Drawable d; d = new Icon("i 1. gif"); d. show(0, 0); d = new Oval(20, 30); d. show(0, 0); Chapter Fifteen Modern Programming Languages 4

Interface Semantics �A commitment to implement a contract �No implementation is inherited �Disadvantage: ◦

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 5

Extending Interfaces �An interface can extend another one �The result is the union of

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 Persistent. Stack extends Stack, Persistent {} class Dynamic. Stack implements Persistent. Stack {…} Chapter Fifteen Modern Programming Languages 6

Implementation Inheritance �When a subclass extends its superclass, it inherits all its methods and

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 7

public class Icon { public class Label { private int x, y; private int

public class Icon { public class Label { private int x, y; private int width; private int height; private Gif image; private String text; public void move (int new. X, int new. Y) { { x = new. X; y = new. Y; } } public Gif get. Image() public String get. Text() { { return image; return text; } } Two classes with a lot in common—but neither is a simple extension of the other. Chapter Fifteen Modern Programming Languages 8

public abstract class Graphic { protected int x, y; protected int width, height; public

public abstract class Graphic { protected int x, y; protected int width, height; public void move(int new. X, int new. Y) { x = new. X; y = new. Y; } } public class Icon public class Label extends Graphic { private Gif image; private String text; public Gif get. Image() public String get. Text() { { return image; return text; } } Common code and data have been factored out into a common base class (which usually should be declared abstract). Chapter Fifteen Modern Programming Languages 9

Multiple Inheritance �In some languages (such as C++, Python Eiffel) a class can have

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 10

Collision Problem �The different base classes are unrelated, and may not have been designed

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 Multi. Function. transmit is called, what should happen? Chapter Fifteen Modern Programming Languages 11

Diamond Problem �A class may inherit from the same base class through more than

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 12

Solvable, But… �A language that supports multiple inheritance must have mechanisms for handling these

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 13

Explicit Forwarding Simulating MI public class Multi. Function { private Printer my. Printer; private

Explicit Forwarding Simulating MI public class Multi. Function { private Printer my. Printer; private Copier my. Copier; private Scanner my. Scanner; private Fax my. Fax; public void copy() { my. Copier. copy(); } public void transmit. Scanned() { my. Scanner. transmit(); } public void send. Fax() { my. Fax. transmit(); } … } Chapter Fifteen Modern Programming Languages 14

Python MI Solution �When resolving the binding of an identifier: ◦ 1) Look first

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 15