Inheritance and Polymorphism

Slides:



Advertisements
Similar presentations
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Advertisements

DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1 Spring 2012.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1.
A Second Look At Java Chapter FifteenModern Programming Languages, 2nd ed.1.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
1 The Design of Class Mechanism for MOBY. 2 Earlier Software designers have to choose between the following schemes for the development Earlier Software.
Chapter 10 Classes Continued
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Component Software Beyond Object-Oriented Programming Clements Szyperski Chapter 7 – Object versus class composition or how to avoid inheritance Alexandre.
Inheritance and Polymorphism CS351 – Programming Paradigms.
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Abstract classes and Interfaces. Abstract classes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
OO as a language for acm l OO phrase l Mental model of key concepts.
Chapter FifteenModern Programming Languages1 A Second Look At Java.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
CSSE501 Object-Oriented Development. Chapter 13: Multiple Inheritance  In this chapter we will investigate some of the problems that can arise when a.
Session 22 Chapter 11: Implications of Inheritance.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Abstract Classes and Interfaces Week 17.  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Object Oriented Programming Lecture 3: Things OO.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Advanced Programming in Java
Advanced Programming in Java
Modern Programming Languages
University of Central Florida COP 3330 Object Oriented Programming
Multi-Methods in Cecil
Inheritance and Polymorphism
CS 326 Programming Languages, Concepts and Implementation
Inheritance & Polymorphism
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Object orientation concepts
Interfaces and Inheritance
Object Oriented Analysis and Design
Interfaces.
Object oriented vs procedural programming
Advanced Programming in Java
Advanced Programming in Java
Object-Oriented Programming
Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Further abstraction techniques
Lecture 10 Concepts of Programming Languages
COP 3330 Object-oriented Programming in C++
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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