Sie sind auf Seite 1von 28

Classes (abstraction and encapsulation) Inheritance Polymorphism With these principles, OOD allows us to design complex systems by constructing

ng them out of simple, smaller pieces that interact with each other.

Abstraction in the object oriented world generalizes the characteristics and behaviors of an item. Example: Automobile. Encapsulation conceals the functional details of an object from other objects, i.e., the object operates as a black box. Example: The functionality of brake is hidden.

Inheritance is a way to form new classes using classes that have already been defined. Inheritance is employed to help reuse of existing code with little or no modification. Example: Animal, Tiger. Inheritance Hierarchies: Inheritance can span multiple levels. A super class to a subclass can also be a subclass to another super class.

Multiple
Classes

Inheritance

can be derived from more than one super class. Example : Tank - can be a weapon and a vehicle.

Polymorphism

refers to the ability of executing different operations in response to the same message.

A Class diagramgives an overview of a system by showing its classes and the relationships among them. Class diagrams are static -- they display what interacts but not what happens when they do interact. UML class notation is a rectangle divided into three parts: class name, attributes, and operations. Names of abstract classes, such asPayment,are in italics. Relationships between classes are the connecting links.

Account

Account
- balance : double - limit : double

[visibility] [ / ] attribute_name [multiplicity] [: type [ default_value] ]

Account
- balance : double - limit : double + deposit (amount : double) : void + withdraw (amount : double) : void

[visibility] method_name ([[In |Out] parameter : type [ , more params ]]) [: return_type ]

Account
- balance : double + limit : double # id : int ~ databaseId : int + deposit (amount : double) : void - withdraw (amount : double) : void # getAvailableFunds() : double ~ getDatabaseId() : int

+ public -Private # protected ~ package

Person
- noOfPeople : int - name: String - Person(name : String) - createPerson(name : String) : Person + getName() : String + getNumberOfPeople() : int

Class & Instance Scope

Classes can related to each other through different relationships:


Association (delegation) Aggregation & Composition Generalization (inheritance) Realization (interfaces) Dependency

Association names and Role Defaults

Person

Lives at

Address

Default role name = address Default multiplicity = 1

Role

Person

employee Works for

employer

Company

Association name

Classes can related to each other through different relationships:


Association (delegation) Aggregation & Composition Generalization (inheritance) Realization (interfaces) Dependency

ASSOCIATION - MULTIPLICITY

A Student can take up to five Courses. Student has to be enrolled in at least one course. Up to 300 students can enroll in a course. A class should have at least 10 Student Course Takes 1..5 10 . . 300 students.

A teacher teaches 1 to 3 courses (subjects) Each course is taught by only one teacher. A student can take between 1 to 5 courses. A course can have 10 to 300 students.

Teacher

Teache s

1..3

Course

Student

00 0..3 1

s Take

1..5

Company can have many employees. An employee can only work for one company employee employer
Person

Works for

Company

John:Perso n James:Pers on

Works for

Microsoft : Company
or rks f Wo

One class can be related to another in a


One-to-one One-to-many One-to-one or more One-to-zero or one One-to-a bounded interval (one-to-two through twenty) One-to-exactly n One-to-a set of choices (one-to-five or eight)

Multiplicity can be expressed as,


Exactly one 1 Zero or one - 0..1 Many - 0..* or * One or more - 1..* Exact Number - e.g. 3..4 or 6 Or a complex relationship e.g. 0..1, 3..4,6..* would mean any number of objects other than 2 or 5

An association that connects a class to itself is called a self association.


A Company has Employees. A single manager is responsible for up to 10 workers.

manag er

Employee
0.. 10

worker

Responsible for

Aggregation and Composition


0..1 1..

Computer

Hardware Device

Aggregation is made up of objects that can be shared or exchanged

ShoppingBaske t

0..1

1..

OrderItem

Composition is composed of objects that cannot be shared or exchanged and live only as long as the composite object

Person

Employee

<<interface> > Person

Person

OR

Employee

Employee

Shape Shape draw ( )

Rectan gle

Circle

Rectan gle draw ()

Circle draw ()

Change in specification of one class can change the other class. This can happen when one class is using another class.
Circle move (p : Point ) Point

Dependency

relationship can be used to show relationships between classes and objects.


n st a <in < >> O ce f

CircleA : Circle CircleB : Circle

Circle

<<i n >> stanceO f

Das könnte Ihnen auch gefallen