Inheritance issues SE-2811 Dr. Mark L. Hornick 1.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces.
Software Engineering Class design. Class design – ADT Abstract Data Types:  Why use ADT? Hidden implementation details Changes do not affect whole program.
Strategy Pattern1 Design Patterns 1.Strategy Pattern How to design for flexibility?
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Chapter 25 More Design Patterns.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Inheritance using Java
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
The Adapter Pattern SE-2811 Dr. Mark L. Hornick 1.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Week 2, Day 2: The Factory Method Pattern Other good design principles Cohesion vs. Coupling Implementing the Strategy Pattern Changing strategies (behaviors)
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
Week 6, Class 1 & 2: Decorators Return Exam Questions about lab due tomorrow in class? Threads Locking on null object invokeLater & the squares example.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
SE-2811 Software Component Design Week 1, Day 2 (and 1-3 and 2-1) SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 1.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
CS 210 Introduction to Design Patterns August 29, 2006.
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
It started with a simple … A highly successful duck pond simulation game called SimUDuck The game can show a large variety of duck swimming and making.
Systems Requirements SE 3821 Design? Algorithms CS 2852.
SE-2811 Software Component Design Week 1, Day 2 Making teams Warm-up exercise Design pattern defined SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick.
Systems Requirements SE 3821 Design? Algorithms CS 2852.
Slide design: Dr. Mark L. Hornick
Testing in OO Environment The reasons for testing is not any different for any of the design and implementation methodologies, including OO methodology.
// create some behaviors SwimBehavior csb = new CircularSwimming(); QuackBehavior sqb = new StandardQuacking(); SwimBehavior rsb = new RandomFloating();
SE-2811 Software Component Design Week 1, Day 1 Design pattern defined Code that needs a design pattern… SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
SE 461 Software Patterns Welcome to Design Patterns.
Design Patterns: MORE Examples
Web Design & Development Lecture 9
Sections Inheritance and Abstract Classes
Strategy Pattern.
Week 2, Day 1: The Factory Method Pattern
Copyright © by Curt Hill
Interfaces I once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session,
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Strategy Design Pattern
SE-2811 Software Component Design
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
SE-2811 Software Component Design
Object-Oriented Programming
Interfaces.
Advanced Java Programming
Review: Design Pattern Structure
SE-2811 Software Component Design
Objects First with Java A Practical Introduction using BlueJ
Adapter Design Pattern
14. Factory Pattern SE2811 Software Component Design
14. Factory Pattern SE2811 Software Component Design
Objects First with Java A Practical Introduction using BlueJ
Week 6, Class 2: Observer Pattern
16. Visitors SE2811 Software Component Design
16. Visitors SE2811 Software Component Design
Presentation transcript:

Inheritance issues SE-2811 Dr. Mark L. Hornick 1

Consider a (badly written) generic Duck class //Duck daffy = new Duck(Duck.MALLARD, “daffy”); //Duck donald = new Duck(Duck.REDHEAD, “donald”); //Duck clyde = new Duck(Duck.DECOY, “clyde”);... public void swim() { if( type == REDHEAD || type == MALLARD ) // swim in circles else // do nothing; decoys just float } public void quack() { if( type == MALLARD || type == REDHEAD ) // real ducks quack else // do nothing; decoys don’t quack } SE-2811 Dr. Mark L. Hornick 2 SimUDuck v1

Duck is an example of a class that exhibits low cohesion Cohesion: A measure of how focused or strongly related the responsibilities of a class are Does a class do many unrelated things? If “yes”, then it has low cohesion (bad) Does a class represent only one thing? If “yes”, then it has high cohesion (good) SE-2811 Dr. Mark L. Hornick 3

Refactoring to improve cohesion via use of OO Inheritance Duck: an abstract class Abstract classes can define attributes and (default) behaviors common to all Duck-derived classes. Concrete classes implement type-specific adaptations (overrides) These may also include additional type-specific attributes (none in this case) SE-2811 Dr. Mark L. Hornick 4 SimUDuck v2

Is inheritance always a solution? What if some ducks (Mallards, Redheads) had similar behavior: Quacking sound Circular swimming pattern While others (Pintail) had different behaviors: Quacking sound Random swimming (ie floating) pattern And still others (Decoys) had: No quacking sound Random swimming (ie floating) pattern Lots of overriding (and maybe duplication of code)! Q1) Should we bother implementing any behaviors at all in the Duck abstract class if many will be overridden anyway (and possibly duplicated)? Q2) Can we modify the class hierarchy to group similar behaviors together? SE-2811 Dr. Mark L. Hornick 5 Code duplication?

What about multiple levels of abstraction? SE-2811 Dr. Mark L. Hornick 6 Here, the swim() and quack() behavior is defined, but not implemented, in Duck… …instead, swim() and quack() behaviors are implemented in a second level of abstract classes... …and finally inherited in concrete classes. SimUDuck v3 And what about a quiet, swimming Duck?

BUT: Multiple inheritance is not even allowed in Java! (FYI: it IS allowed in C++, but is EVIL) SE-2811 Dr. Mark L. Hornick 7 Here, the swim() and quack() behavior is defined, but not implemented, in Duck… …instead, swim() and quack() behaviors are implemented in a second level of abstract classes... …and finally inherited in concrete classes. SimUDuck v3

This can also lead to messy “class explosions” SE-2811 Dr. Mark L. Hornick 8 SimUDuck v3

Some reflections on inheritance… Allan Holub (a noted computer technology author) once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session, he asked him [Gosling]: Q: "If you could do Java over again, what would you change?" Gosling: "I'd leave out classes.” After the laughter died down, he explained that the real problem wasn't classes per se, but rather implementation inheritance (the extends relationship). Interface inheritance (the implements relationship), Gosling explained, is preferable. SE-2811 Dr. Mark L. Hornick 9

We can use interface inheritance to address these concerns… SE-2811 Dr. Mark L. Hornick 10 We eliminate implementation in abstract classes, and force concrete classes to supply it instead. …but now we’re back to the duplication of code problem that we saw in v2! SimUDuck v4

A different approach: Isolate behaviors that vary, and encapsulate them as attributes to eliminate implementation inheritance and class explosions: SE-2811 Dr. Mark L. Hornick 11 SimUDuck v5