Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Design Patterns Examples of smart use of inheritance and polymorphism: Composite Pattern State Pattern FEN 2014UCN Teknologi/act2learn1.
Object-Oriented Design Patterns Composite Singleton State Observer … Autumn 2012UCN Technology: IT/Computer Science1.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
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,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Kari R. Schougaard, PhD Stud. Værktøjer og Teknikker, 2006 UNIVERSITY OF AARHUS Department of Computer Science Unified Modeling Language Visual language.
Chapter 8 Object Design Reuse and Patterns. Finding Objects The hardest problems in object-oriented system development are: –Identifying objects –Decomposing.
Chapter 10 Classes Continued
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Introduction to software design patterns For CSE 3902 By: Matt Boggus.
Programming Languages and Paradigms Object-Oriented Programming.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Object Oriented Design and Programming Alan Goude Room: Sheaf 9323.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Unified Modeling Language, Version 2.0
Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Programming in Java CSCI-2220 Object Oriented Programming.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Object Oriented Programming
Design Patterns Introduction
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Overview of C++ Polymorphism
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Class Relationships Lecture Oo07 Generalization Relationships.
1 Good Object-Oriented Design Dr. Radu Marinescu Lecture 4 Introduction to Design Patterns.
Polymorphism Lecture - 9.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Modern Programming Tools And Techniques-I
Design Patterns: MORE Examples
Unit II-Chapter No. : 5- design Patterns
Factory Method Pattern
Interface, Subclass, and Abstract Class Review
Software Design and Architecture
Polymorphism.
Factory Method Pattern
Types of Programming Languages
Object Oriented Analysis and Design
Software Engineering Lecture 7 - Design Patterns
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Overview of C++ Polymorphism
Chapter 14 Abstract Classes and Interfaces
Lecture 10 Concepts of Programming Languages
Presentation transcript:

Class Relationships Lecture Oo08 Polymorphism

References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML Distilled Applying the Standard Object Modeling Language, AWL, 1997, Chapt 4 n Booch, Object Oriented Analysis and Design, Chapt 3

Teaching Points n Abstract Classes n Polymorphism n Late-Binding Mechanisms n Interfaces n When/Why to use Polymorhism? n Patterns –Case/Enum –Factory Method

Review n What is the difference between type and class? n What is an operation signature?

Abstract Classes n Are objects normally instantiated from every class in a system?

Polymorphism n Overloading (ad hoc polymorphism) –Binding based on number and type of arguments –I.e. operation signature –A static binding mechanism (compile/link - time binding)

Polymorphism n polymorphism (parametric polymorphism) –depends on type of actual parameter of function call at run time –A dynamic binding mechanism (late- binding)(run-time binding)

Polymorphism n Allows us to treat the same kinds of objects in the same way n Objects with the same interface can be treated in the same way n Objects of different types can be treated in the same way provided they have a common base-type n A VERY POWERFUL MECHANISM!

Things Needed for a Polymorphic Function Call n Common methods in different classes such that the classes are substitutable for each other, and a late binding mechanism n In statically typed langagues (e.g. Java, C++) –Base-class Object Reference –Over ridden methods n REMEMBER THESE!

Operation Overriding class shape { private point centre; private int id; public int getId(){… } public point getCentre(){… } public void draw(){… }// overridden operation } class circle extends shape { private double radius; public void draw(){… }//overriding method (same signature) }

Example n List of shapes n Drawing the list

Abstract Classes n Provide a common interface n Abstract operations n A class with all abstract operations and no state, like a Java interface n Interface-inheritance

Abstract Operations abstract class shape { // Now an abstract class private point centre; private int id; public int getId(){… } public point getCentre(){… } public abstract void draw(); // Now an abstract operation } class circle extends shape { private double radius; public void draw(){… }//overriding method (same signature) }

Sub-typing without Sub-classing n Java Interfaces n Abstract classes with no field and no operation implementations n These ideas only come into play when working with the implementation perspective

Modeling an Interface

Alternate Interface Notation n Lollipop n Equivalent to last slide

Implementing an Interface interface shape { // Now an interface public int getId(); // All operations now abstract public point getCentre(); public void draw(); } class circle implements shape { private point centre; //State variables have to move to private int id; //the implementing class private double radius; public void draw(){… }//overriding method (same signature) }

Sub-classing without Sub-typing n Private and Protected Inheritance n Allows us to reuse a class without having to present its interface to the rest of the world n The base class is concrete n Can be similar to aggregation

When to Use Polymorphism n When we want to treat a set of things in the same way, even though they may not be objects of the same concrete class –The concept of an abstract interface is critical –Probably the prime reason for using generalization/inheritance

What is a Pattern? n “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice” –Christopher Alexander

What is a Pattern? n “A design pattern describes a commonly-recurring structure of communicating components that solve a general design problem in a particular context.” –Gamma, et al.

Why Use Patterns? n Advantages of design patterns are reusability and modularity, which are precisely the goals of object-oriented software design. n Because design patterns capture expert knowledge, the solutions it provides are tried and true. Therefore, they not only make it less time-consuming to design a large system, it also improves design quality.

Why Use Patterns? n Design patterns provide a common vocabulary for designers to communicate by expressing software design at higher level of abstraction than that of a design notation or programming language. n Being able to recognise design patterns helps one to understand existing systems.

Pattern Examples n Meta-pattern for case/enum n Factory

Case/Enum //Fertilization schedule switch(fruitType) { case APPLES: //apples fertilization code break; case ORANGES: //oranges fertilization code break; case PEARS: //pears fertilization code break; }

Case/Enum

Factory Method n Intent –Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. n Also Known As –Virtual Constructor

Factory Method n Use the Factory Method pattern when: –a class can't anticipate the class of objects it must create. –a class wants its subclasses to specify the objects it creates. –classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.

Factory Method

Teaching Points n Abstract Classes n Polymorphism n Late-Binding Mechanisms n Interfaces n When/Why to use Polymorhism? n Patterns –Case/Enum –Factory Method