CS212: Object Oriented Analysis and Design

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
7M822 UML Class Diagrams advanced concepts 15 September 2008.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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 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.
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.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
CS3773 Software Engineering Lecture 04 UML Class Diagram.
CS212: Object Oriented Analysis and Design Lecture 13: Relationship between Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
CS212: Object Oriented Analysis and Design Lecture 14: Reusing classes in C++
Object Oriented Software Development
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Object-Oriented Programming Chapter Chapter
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 12 Object-oriented design for more than one class.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Introduction to Object-Oriented Programming Lesson 2.
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
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.
Classes, Interfaces and Packages
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Introduction to Object-oriented Programming
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Object Oriented Programming
Inheritance ITI1121 Nour El Kadri.
Inheritance-Basics.
Final and Abstract Classes
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
Inheritance, Polymorphism, and Virtual Functions
CS212: Object Oriented Analysis and Design
Review: Two Programming Paradigms
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
An Introduction to Inheritance
ATS Application Programming: Java Programming
Object Oriented Analysis and Design
Comp 249 Programming Methodology
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Inheritance, Polymorphism, and Virtual Functions
COP 3330 Object-oriented Programming in C++
Fundaments of Game Design
Chapter 11: Inheritance and Composition
Overview of C++ Polymorphism
Final and Abstract Classes
Object-Oriented Programming
Lecture 10 Concepts of Programming Languages
C++ Object Oriented 1.
Presentation transcript:

CS212: Object Oriented Analysis and Design Relationship Between Classes

Introduction Classes and objects are building blocks The structure of a software system is defined by the way in which these building blocks relate with one another The behaviour of the system is defined by the manner in which the objects interact with one another We need mechanisms that create connections between these building blocks

Types of relationship Relationship Association Inheritance Genericity Objects of the two classes are related in some non-hierarchical way Classes have a hierarchical relationship based on generalisation Restricted form of inheritance based on types

Association Relation among two or more classes describing a group of links with common structure and semantics. An object of one class is making use of an object of another class Student objects may make use of Course objects when transcripts are generated, when tuition is computed or when a course is dropped.

Association between Classes An association does not imply that there is always a link between all objects of one class and all objects of the other. An association does imply that there is a persistent, identifiable connection between two classes Given an object of class A, it is possible to find an object of class B Otherwise, no B object has been assigned to the association yet. Conceptual relationships between classes as well as physical relationships between the objects of these classes

Implementation criteria Class A stores a key (or several keys) that uniquely identifies an object of class B. Class A stores a reference(s) to object(s) of class B. Class A has a reference to an object of class C, which, in turn is associated with a unique instance of class B. Direct association Indirect association Depend on the requirements that the system has to satisfy

Characteristics of associations Represent very general relationships Allow for variation in the nature of the connection The common variation is the arity of the connection Some form of containment involved in the relationship

Arity of relationships How many objects of class A can be linked to one object of class B and vice-versa Relationship could be one–one, one–many, or many–many One–one relationship: between a User-Interface class accepting user input and a Display-Window class displaying information One–many relationship: A multi-user system Many–many relationship

Containment relationships Aggregation: the object of class A is ‘made up of’ objects of class B. This suggests some kind of a whole–part relationship between A and B. Composite aggregation, also known as composition, has been recognised as being significant. Each instance of the part belongs to only one instance of the whole, and that the part cannot exist except as part of the whole.

Association classes Is used to model an association as a class It is used when the association itself has attributes An association class is the association. The idea of the role is that the same classes can play the same or different roles in other associations.

How to form association? What does an association represent? When can we call a relationship an association? It describes a relationship that will exist between instances at run time and has an example. Possesses, controls, is connected to, is related to, is a part of, has as parts, is a member of, or has as members some other class in the system (i) hierarchy, (ii) stems from a dependency alone, (iii) or relationships depending on particular operation.

In a nutshell Association Binary Aggregation Composition N-Ary

An example A company sells television sets and books. Different attributes to be tracked The company needs to keep track of sales, profits (or losses), etc., for all products.

Inheritance Mechanism for deriving new classes from existing classes. Base class/ Superclass Sub-class/ derived class Generalization Specialization

Advantages of inheritance Reuse of program or model parts Consistent definition of interfaces Use as a modelling aid through a natural categorization of the occurring elements Support for incremental development

Class structure TV Model Price Manufacture quanSold TV() Sales() SetPrice() Book Title Author Price Publisher quanSold Book() Sales() SetPrice() Development of a base class or superclass that reflects the commonalities of the two classes Then extends or sub classes this base class to arrive at the functionalities

Hierarchy Product Price Company quanSold Product() Sales() SetPrice() Part of the object represented by the superclass must be initialised before Book Title Author Book() TV Model TV()

Some Limitations and Caveats Sub-classing could result in deep hierarchies It may be necessary to hide selected features of the superclass Combining inheritance with genericity may result in complications Lack of multiple inheritance Liskov substitution principle

Genericity Mechanism for creating entities that vary only in the types of their parameters Can be associated with any entity (class or method) that requires parameters of some specific types. Method: we specify the types of arguments and the return type Class: the types of arguments to the constructor(s), the return types and argument types of the methods are all specified Generic parameters: The entity is therefore not fully specified

Code Reuse Fascinating feature of C++ is code reuse. To do a lot more than copy code and change it. Use existing classes that someone else has built Composition and Inheritance Source: Tech Comics

Composition A straightforward method Create objects of your existing class inside the new class. The new class is composed of objects of existing classes Composing classes primarily with built-in types Containment, Aggregation, Sub-object Demonstration

Inheritance Syntax (c++) There’s a new and different form State this in code by giving the name of the class Before the opening brace of the class body, put a colon Followed by the name of the base class(es) class derived-class-name : access base-class-name { // body of class }; “This new class is like that old class.”

Constructor & Member initializer list It is important to guarantee proper initialization Constructors for all of its sub-objects are to be called Derived class constructor doesn’t have permission to access the private data elements So it can’t initialize them directly Solution: Constructor initializer list Demonstration

Initializing base class members Initialize Base class data member when we create a Derived object Restrictions on how to initialize inherited member variables. The value of a variable can only be set in an initialization list of a constructor belonging to the same class as the variable Ensures that all variables are initialized only once.

Example class Derived: public Base { public: double m_dValue;       Derived(double dValue=0.0, int nValue=0)         : m_dValue(dValue), m_nValue = nValue     {     } }; Derived(double dValue=0.0, int nValue=0)         : m_dValue(dValue)     {         m_nValue = nValue;     }

Passing parameter to Base class Expanded form of the derived class's constructor declaration derived-constructor(arg-list) : base1(arg-list), base2(arg-list), // ... baseN(arg-list) { // body of derived constructor } Derived(double dValue=0.0, int nValue=0)         : Base(nValue), // Call Base(int) constructor           m_dValue(dValue)     {     } Demonstration

What about member objects of built-in types? No object (or part of an object) can get out of the starting gate without its constructor being called. “Pseudo-constructor calls” is to perform a simple assignment. class X { int i; float f; char c; char* s; public: X() : i(7), f(1.4), c('x'), s("howdy") {} };

Composition & Inheritance Like some class(es) and composed of some class(es) B C inherits from B and has a member object (“is composed of”) of type A. C A Demonstration

Destructor calls To make explicit constructor calls in the initializer list Never need to make explicit destructor calls Compiler still ensures that all destructors are called Starting with the most-derived destructor and working back to the root Order of constructor & destructor calls

Name hiding New definition to a base class’s function in derived class There are two possibilities Redefining, Overriding : exact signature and return type Change the member function argument list or return type Demonstration Anytime you redefine an overloaded function name from the base class, all the other versions are automatically hidden in the new class.

Name hiding Changing the interface of the base class Using the class in a different way than inheritance Using inheritance primarily for code reuse Not to maintain the common interface of the base class Taking a general-purpose class and specializing it

Function that don’t inherit Not all functions are automatically inherited Constructors and destructors don’t inherit Must be created specially for each derived class operator= doesn’t inherit: constructor-like activity With constructors, you can’t create any constructors Demonstration

Composition vs. Inheritance Both place sub-objects inside the new class (base class). Both use the constructor initializer list

Private Inheritance Can inherit a base class privately “implementing in terms of” Included in the language for completeness Publicizing privately inherited members (some of the functions of the base class is available to the derived class) Demonstration

Protected Want to make something hidden from the world at large and yet allow access for members of derived classes. “This is private as far as the class user is concerned, but available to anyone who inherits from this class.” To provide flexibility in the inheritance mechanism Base class' protected members become protected members of the derived class (Demonstration) Protected Base-Class Inheritance (Demonstration)

Forms of Inheritance Demonstration A B Single Inheritance A C Multiple Inheritance B A B Hierarchical Inheritance D C A B Multilevel Inheritance C B D Hybrid Inheritance C A

Inheritance and Visibility Modifier Class A Private Protected Public Class B: public A Class C: private A Private Protected Public Private Protected Public Class D: public B, protected C Private Protected Public

Access mechanism Private Protected Public Own member functions Friendly functions and classes Own member functions Friendly functions and classes, derived class member functions Own member functions Friendly functions and classes, derived class member functions, All users

Friendship and inheritance Friend functions are non-member function Can access private and protected member(s) Friend classes has access to private members of its friend Friendship is not inherited Demonstration

Upcasting Inheritance not only provides functions to the derived class It established the relationship between base and derived class “The new class is a type of the existing class” Instrument Wind void tune(Instrument& i){} int main() { Wind flute; tune(flute); } // Upcasting

Ambiguity in inheritance Grandparent Parent 1 Parent 2 Child Grandparent Parent 1 Parent 2 Child As virtual base class Demonstration Demonstration Declaring the base class as virtual when it is inherited

subtypes and subclasses Differences between subtypes and subclasses in supporting reuse Subclasses allow one to reuse the code inside classes Subtyping is useful in supporting reuse externally Types should only refer to the interface of an object, not its implementation