Docsity

Object-Oriented Design: Understanding Classes, Encapsulation, Inheritance, and Metrics - P, Study notes of Software Engineering

Partial preview of the text

Download Object-Oriented Design: Understanding Classes, Encapsulation, Inheritance, and Metrics - P and more Study notes Software Engineering in PDF only on Docsity!

Object Oriented

Design

Kenneth M. Anderson

Lecture 20

CSCI 5828: Foundations of

Software Engineering

Object-Oriented Design

 Traditional procedural systems separate data and procedures, and model these separately  Object orientation combines data and methods together into a cohesive whole  data abstraction  The purpose of Object-Oriented (OO) design is to define the classes (and their relationships) that are needed to build a system that meets the requirements contained in the SRS

Relationship of OO A&D

OO Concepts

 Encapsulation

 grouping of related ideas into one unit which we can refer to by a single name  For example, methods, classes, packages

 Provides information hiding by restricting the

external visibility of a unit’s information

 In OO A&D, the object is the unit of

encapsulation

 An object’s data is hidden behind the public interface (methods) of the object

OO Concepts..

 Classes – a class is a stencil from which

objects are created; defines the structure and

services of a “class” of objects. A class has

 An interface which defines which parts of an object can be accessed from outside  A body that implements the operations  Instance variables to hold object state

 Objects and classes are different; a class is a

type, an object is an instance

 State and identity is associated with objects

OO Concepts – access

 Operations in a class can be

 Public: accessible from outside  Private: accessible only from within the class  Protected: accessible from within the class and from within subclasses

Inheritance..

 A subclass B generally has a derived part

(inherited from A) as well as new attributes

(new instance variables or methods)

 B’s specification only defines the new attributes

 This creates an “is-a” relationship

 objects of type B are also objects of type A

Inheritance…

Inheritance…

 There are several types of inheritance  Strict inheritance: a subclass uses all of the features of its parent class without modification  The subclass only adds new attributes or methods  Non-strict inheritance: a subclass may redefine features of the superclass or ignore features of the superclass  Strict inheritance supports “is-a” cleanly and has fewer side effects  If a subclass redefines a method of the parent, it can potentially break the contract that the superclass offers its users

Inheritance and Polymorphism

 Inheritance enables polymorphism, i.e. an

object can be of different types

 An object of type B is also an object of type A

 Hence an object has a static type and a

dynamic type

 Implications on type checking  Also brings dynamic binding of operations which allows writing of general code where operations do different things depending on the type

Module Level Concepts

 Basic modules are classes  During OO design, a key activity is to specify the classes in the system being built  In creating our design, we want it to be “correct” (i.e. cover its requirements)  But a design should also be “good” – efficient, modifiable, stable, …  We can evaluate an OO design using three concepts  coupling, cohesion, and open-closed principle

Coupling…

 Interaction coupling occurs when the

methods of a class invoke methods of

another class

 this can’t be avoided, obviously…  but we want to ensure that an object’s public interface is used  a method of class A should NOT directly manipulate the attributes of another class B  Why?

Coupling…

 Component coupling – when a class A has

variables of another class C

 A has instance variables of type C  A has a method with a parameter of type C  A has a method with a local variable of type C

 When A is coupled with C, it is coupled with

all subclasses of C as well

 Component coupling will generally imply the presence of interaction coupling also