Stéphane Ducasse 1 Design Points - Subclassing vs Subtyping Stéphane Ducasse --- 2010.

Slides:



Advertisements
Similar presentations
Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Advertisements

Stéphane Ducasse«ChapterNr».1 The Design in Question The Basic Idea behind Frameworks Subclassing vs SubTyping Design Heuristics Design Symptoms.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Software Testing and Quality Assurance Lecture 28 – Testing Class Hierarchies.
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP Week 4 1 Object Oriented Programming in Java Monday, Week 4 Last week’s asst. –rating…. This week’s asst. OOP Concepts Inheritance & Substitutability.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CSSE501 Object-Oriented Development
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Abstraction, Inheritance, and Polymorphism in Java.
Design Patterns.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
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.
3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Objected Oriented Programming & Design JAVA Shishir Gupta (704) (704)
S.Ducasse Stéphane Ducasse 1 Design Heuristics.
Stéphane Ducasse 1 Elements of Design - Simple Smells.
1 Computer Science 340 Software Design & Testing Inheritance.
Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Stéphane Ducasse 1 Visitor.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Inheritance Semantics and Method Lookup.
Lecture 12 March 16, The Scope of a Variable What if there are two variables with the same name? –A local or block-local variable can have the same.
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
Stéphane Ducasse 1 Elements of Design - Inheritance/Compositio n.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Stéphane Ducasse 1 Singleton.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Object Oriented Programming
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
S.Ducasse Stéphane Ducasse 1 Essential OO Concepts Stéphane Ducasse.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Stéphane Ducasse 1 Strategy.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Software Construction and Evolution - CSSE 375 Dealing with Generalization Steve and Shawn Left – In the 1990 movie “The Freshman,” Matthew Broderick,
Stéphane Ducasse 1 Elements of Design - Unit of Reuse.
Module 9. Dealing with Generalization Course: Refactoring.
COP 4331 – OOD&P Lecture 7 Object Concepts. What is an Object Programming language definition: An instance of a class Design perspective is different.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
CSCE 240 – Intro to Software Engineering Lecture 3.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Web Design & Development Lecture 9
Sections Inheritance and Abstract Classes
Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Interface, Subclass, and Abstract Class Review
Week 4 Object-Oriented Programming (1): Inheritance
Generics, Lambdas, Reflections
Chapter 10 Thinking in Objects
Modeling with Inheritance
MSIS 670 Object-Oriented Software Engineering
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Java Programming
Presentation transcript:

Stéphane Ducasse 1 Design Points - Subclassing vs Subtyping Stéphane Ducasse

S.Ducasse 2 A little problem How to implement a Stack?

S.Ducasse 3 Stack subclass of OrderedCollection Stack>>pop ^ self removeFirst Stack>>push: anObject self addFirst: anObject Stack>>top ^ self first Stack>>size, Stack>>includes: are free, inherited from

S.Ducasse 4 Wait! but What do we do with all the rest of the interface of OrderedCollection? a Stack IS NOT an OrderedCollection! We cannot substitute an OrderedCollection by a Stack Some messages do not make sense on Stack Stack new addLast: anObject Stack new last So we have to block a lot of methods...

S.Ducasse 5 Consequences... Stack>>removeFirst self shouldNotImplement Stack>>pop ^ super removeFirst

S.Ducasse 6 The Problem There is not a simple relationship between Stack and OrderedCollection Stack interface is not an extension or subset of OrderedCollection interface Compare with CountingStack a subclass of Stack CountingStack interface is an extension of Stack interface

S.Ducasse 7 Compare the two uses

S.Ducasse 8 Compare the two replacements

S.Ducasse 9 A Better Approach By defining the class Stack that uses OrderedCollection Object subclass: Stack instVarNames: ‘elements’ Stack>>push: anElement elements addFirst: anElement Stack>>pop element isEmpty ifFalse: [^ element removeFirst]

S.Ducasse 10 Inheritance and Polymorphism Polymorphism works best with conforming/substituable interfaces Inheritance creates families of classes with similar interfaces An abstract class describes an interface fulfilled by its subclasses Inheritance helps software reuse by making polymorphism easier

S.Ducasse 11 About subtyping and subclassing You have only extends or subclass: in programming language. But they can be used to define a subclassing or subtyping relationship.

S.Ducasse 12 Subtyping Inheritance Reuse of specifications A subclass refines superclass specifications A program that works with Numbers will work with Fractions. A program that works with Collections will work with Arrays.

S.Ducasse 13 Subclassing Inheritance for code reuse Dictionary is a subclass of Set Semaphore is a subclass of LinkedList No relationship between the interfaces of the classes Subclass reuses code from superclass, but has a different specification. It cannot be used everywhere its superclass is used. Usually overrides a lot of code. ShouldNotImplement use is a bad smell…

S.Ducasse 14 Inheritance for Code Reuse Inheritance for code reuse is good for rapid prototyping getting application done quickly. Bad for: easy to understand systems reusable software application with long life-time.

S.Ducasse 15 Subtyping Essence You reuse specification You should be able to substitute an instance by one of its subclasses (more or less) There is a relationship between the interfaces of the class and its superclass

S.Ducasse 16 How to Choose? Favor subtyping When you are in a hurry, do what seems easiest. Clean up later, make sure classes use “is-a-subtype” relationship, not just “is-implemented-like”. Is-a-subtype is a design decision, the compiler only enforces is-implemented-like!!!

S.Ducasse 17 Quizz – Circle subclass of Point? – Poem subclass of OrderedCollection?