Variations on Inheritance Object-Oriented Programming 236703 Spring 2008 1.

Slides:



Advertisements
Similar presentations
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Designing a Java Extension with Mixins Presented by: Yoav Gur
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 10 Classes Continued
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
1 Advanced Abstraction Mechanisms  Mixins  Traits  Delegation.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Inheritance using Java
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Chapter 8 More Object Concepts
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
ISBN Object-Oriented Programming Chapter Chapter
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
Dynamic Binding Implementation Object-Oriented Programming Spring
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Overview of C++ Polymorphism
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Inheritance and Polymorphism
Review: Two Programming Paradigms
Java Programming Language
Polymorphism Polymorphism
Java – Inheritance.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Overview of C++ Polymorphism
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Variations on Inheritance Object-Oriented Programming Spring

Circle and Ellipse Problem Is a Circle a kind-of an Ellipse? Problem: Ellipse has a scale(x, y) method, which is meaningless for Circle. Poor Options – Circle :: scale(x, y) is an error (throw an exception ) Pitfalls: surprising clients. – Circle::scale(x, y) is – a no-op / scales both dimensions by average Pitfalls: very difficult to document Is an Ellipse a Kind of Circle? problem: Circle should have centre and radius methods. How should these be defined in Ellipse ? We cannot delete methods in inheritance! 2

Solutions using Abstract Classes – Ellipse derived from Oval – Circle derived from Oval – scale(x, y) method of Ellipse – scale(factor) method of Circle Abstract (Deferred) Class: A class that can not be used for instantiation. Abstract Superclass Rule: All superclasses must be abstract – Inheritance represents a taxonomy. – It is rare to find a set A of real objects which has a sub-set B of real objects, such that the features of elements of B are a super-set of those of the elements of A. 3

Deferred Methods A concept complementing overriding: – Overriding: Reimplement a method in an inheriting class. – Deferring: Postponing implementation of a method to an inheriting class. Terminology: – Deferred method: A method which must be implemented in an inheriting class. – Effecting a method: Implementing a deferred method. – Deferred class: A class with deferred methods. – Effected class: A class in which all deferred methods have been effected. Usage: – Separate interface from implementation. Different derived classes will implement the same open contract differently. – Make classes that: can be used for inheritance cannot be used for instantiation are abstract... 4

Pure Virtual Functions in C++ Pure Virtual Functions (PVF): A C++ terminology --- virtual member functions which require an implementation in a derived class. Syntax: PVFs vs. Deferred Methods: – PVFs may, but usually don’t, have an implementation. – Deferred methods may not have an implementation. Abstract Class in C++: A class with PVFs. Concrete Class in C++: A class which is not abstract. – Only concrete classes may have instances. class Shape { public: //... virtual void rotate(int angle)= 0; virtual void hide() const = 0; }; class Shape { public: //... virtual void rotate(int angle)= 0; virtual void hide() const = 0; }; 5

Quiz: Abstract Classes in C++ struct Vehicle { virtual String media() const = 0; virtual unsigned speed() const = 0; } V, *PV; struct LandVehicle: public Vehicle { String media() const { return "Land";} } L, *PL; struct Train: public LandVehicle { virtual unsigned speed() const { return 130; } } T, *PT; PV = &V; PV = &L; PV = &T; PL = &V; PL = &L; PL = &T; PT = &V; PT = &L; PT = &T; Vehicle &RV1, &RV2 = L, &RV3 = T; LandVehicle &RL1, &RL2 = L, &RL3 = T; Train &RT1, &RT2 = L, &RT3 = T; Mark all errors in the following code: 6

Pure Virtual Destructor class Abstract { public: virtual ~Abstract() = 0; //... }; Abstract::~Abstract() { //... } class Abstract { public: virtual ~Abstract() = 0; //... }; Abstract::~Abstract() { //... } Pure virtual destructor must be defined – Destructors cannot be overridden It must be defined outside the class declarations – Just as all defined pure virtual functions A class defining such a destructor is abstract, but this definition does not make any derived class abstract. – No need to define a destructor in any derived class to make it concrete 7

Java: Interfaces vs. Abstract Classes Both specify a type Interface – Pure specification – Contains only method specifications (abstract methods) and named constant definitions. All data fields are public final static and all methods are public abstract Abstract class – Method specification plus, optionally: – May contain partial or full default method implementation, instance variables, constructors – Any class with abstract methods must be an abstract class – Abstract classes do not necessarily need to have abstract methods 8

Final Classes & Methods Final Method: cannot be overridden – Call can be inline Final Class: cannot be subclassed further – Final class implies that all methods are finalized – No dynamic binding mechanism is required – Can be used for heavy optimizations. Categorization principle: – Inherit only from abstract classes – Instantiate only final classes 9

Mixins Mixin: a subclass parametric in the superclass The problem: class C1 extends P1 { decs } class C2 extends P2 { decs } //same declarations as in C1 Drawbacks: – code duplication – inability to use H1 and H2 in a uniform way (decs is not a type) 10

A Mixin in C++ struct Number { int n; virtual void setValue(int v) { n = v; } virtual int getValue() { return n; } }; template // Define the mixin struct Undo: S { int old; void setValue(int v) {old = getValue(); S::setValue(v); } void undo() { S::setValue(old);} }; struct UndoableNumber : Undo // Instantiate the mixin { }; int main(int, char**) { UndoableNumber u; u.setValue(1); u.setValue(2); u.undo(); cout << u.getValue(); // output: 1 } 11

Jam: Java with Mixins Drawback of the C++’s mixin idiom: – The mixin class is not a type Undo* p = new UndoableNumber(); // Compiler error! Jam – An extension of Java with mixins mixin Undo { inherited void setValue(int v) ; inherited int getValue(); int old; void setValue(int v) {old = getValue(); super.setValue(v); } void undo() {setText(lastText); } } 12

Jam –Mixin Instantiation class Number { int n; void setValue(int v) { n = v; } int getValue() { return n; } } class UndoableNumber = Undo extends Number{}; mixins are types: void g(Undo u){ u.setValue(1) ; u.setValue(2) ; System.out.println(u.old) ; System.out.println(u.getValue()) ; } 13

Mixins : Drawbacks Imposes total ordering class A { public int f() { return 0; } public int g() { return 1; } } mixin B { public int f() { return 2; } public int g() { return 3; } } class C = B extends A { } –Can C offer B.f() and A.g() ?! Fragile inheritance: –Adding a method to a mixin may silently override an inherited method 14

MultiMethods Most languages allow a method (identified by a name) to have multiple definitions (bodies). Usually, the most appropriate method body is chosen at runtime depending on the dynamic type of the receiver (1st argument in the call). Multiple Dispatch The executed method is selected by the dynamic type of one (or more) argument(s) A Multimethod is a method that provides multiple dispatch. 15

Shape s = new Shape(); s.intersect(new Shape()); // no problem: Shape.intersect() is invoked s = new Rectangle(); s.intersect(new Rectangle()); // two rectangles but Shape.intersect() is invoked again! Example: Binary Methods (in Java) public class Shape { public void intersect(Shape s) { // generic code for two shapes } public class Shape { public void intersect(Shape s) { // generic code for two shapes } public class Rectangle extends Shape { public void intersect(Rectangle s) { // more efficient code for two rectangles } public class Rectangle extends Shape { public void intersect(Rectangle s) { // more efficient code for two rectangles } 16

Shape s = new Shape(); s.intersect(new Shape()); // no problem: Shape.intersect() is invoked s = new Rectangle(); s.intersect(new Rectangle()); // no problem: Rectangle.intersect() is invoked MultiJava: Java with MultiMethods public class Shape { public void intersect(Shape s) { // generic code for two shapes } public class Shape { public void intersect(Shape s) { // generic code for two shapes } public class Rectangle extends Shape { public void s) { // more efficient code for two rectangles } public class Rectangle extends Shape { public void s) { // more efficient code for two rectangles } 17