[go: up one dir, main page]

0% found this document useful (0 votes)
80 views35 pages

Chapter 4 Basics of Object-Oriented Concepts

This document discusses several basic object-oriented concepts including classes and objects, inheritance, encapsulation, polymorphism, abstraction, and relationships between objects. Classes define objects and consist of attributes that represent an object's state and methods that define its behavior. Inheritance allows subclasses to inherit attributes and behaviors from superclasses. Encapsulation hides an object's internal representation and only exposes its public methods and properties. Polymorphism allows objects of different classes to respond to the same method name. Relationships like association, aggregation, and composition define connections between objects. Cohesion and coupling measure how tightly connected components in a system are.

Uploaded by

Abdu Aminu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views35 pages

Chapter 4 Basics of Object-Oriented Concepts

This document discusses several basic object-oriented concepts including classes and objects, inheritance, encapsulation, polymorphism, abstraction, and relationships between objects. Classes define objects and consist of attributes that represent an object's state and methods that define its behavior. Inheritance allows subclasses to inherit attributes and behaviors from superclasses. Encapsulation hides an object's internal representation and only exposes its public methods and properties. Polymorphism allows objects of different classes to respond to the same method name. Relationships like association, aggregation, and composition define connections between objects. Cohesion and coupling measure how tightly connected components in a system are.

Uploaded by

Abdu Aminu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

1

Basic Object Oriented Concepts


Classes and Object
Inheritance, Encapsulation
Polymorphism,Abstraction
Relations:
Coupling ,Cohesion
Aggregation, Composition
Association, Collaboration
Interfaces,
Components, Patterns
2
What are Classes and Object?
oAn object is an instance of a class.
oAn object is an entity with a well-defined boundary and identity that encapsulates state and
behavior.
oState is usually called attribute , and shows the characteristic properties that the object holds.
represented by data type.
– In Car example the car’s attributes are: color, manufacturer, cost, owner, model, etc.
oBehavior is represented by operations & methods, and it represents the externally visible
activities performed by the object.

3
What are Classes and Object?
oMethods: define objects behavior and specify the way in which an Object’s data are
manipulated.

 In the Car example the car’s methods are:


 drive(), lock(), carrypassenger().

4
Inheritance
Inheritance: allows one class of objects to be defined as a special case of a more general class.

Special cases are subclasses and more general cases are super classes.

Subclass inherits all properties of its super classes and can define its own unique properties.

Inheritance=Super class + sub class

Use of inheritance: makes our code easy to understand and reduces the lines of code.

5
Inheritance
Sometimes called Generalization/specialization concept.
Sub class_super class Generalization

Super class_sub class Specialization

Inheritance is one way we can achieve polymorphism, which is the ability to treat a set of
objects of similar types the as same.

6
Example: an Inheritance Hierarchy
Vehicle

Automobile Motorcycle Bus

Sedan Sports Car School Bus Luxury Bus

What properties does each vehicle inherit from the types of vehicles above it in
the diagram?
7
Encapsulation
oEncapsulation: refers to an object hiding its attributes behind its operations (it seals the
attributes in a capsule, with operations on the edge).

oEncapsulation means to design, produce, and describe software so that it can be easily used
without knowing the details of how it works.

oProvides information hiding by restricting the external visibility of a unit’s information. Also known
as information hiding.

oHidden attributes are said to be private.

8
Cont’d…
 Users of a class should not be concerned with internal representation of data members
or implementation details of services.

If any of these have changed, users of the class should not be effected.

An analogy:

When you drive a car, you don’t have know the details of how many cylinders the engine has or how
the gasoline and air are mixed and ignited. Instead you only have to know how to use the controls.

9
Cont’d…
Use of encapsulation:

o It enables the users and programmers to see the system components according to the
privilege they are given.

oSeparates the behavior from the implementation which makes the program modular and
simple.

oHelps to make easily understandable program.

10
Polymorphism
o Polymorphism: is the ability of an object to take many forms. Objects in different classes
may respond differently to messages with the same name.

o Polymorphism =poly +morphism

o poly -> many, morphism -> forms

 Major Components:
 Overriding
 Overloading

11
Cont’d…
oOverriding: when a class or method have the same signature(method name +attributes)
with other class or method, and when that class or method makes a special implementation of
the overridden class or method.
Eg. class Animals {
public static void main(String args[]) {
Animal animal = new Animal();
Cat cat= new Cat();
animal.print();
cat.print(); } public class Cat extends Animals {
void print() { void print() {
System.out.println("Superclass Animals"); System.out.println("Subclass Cat");}
}}} Out put: Superclass Animals
Subclass Cat

12
Cont’d…
oOverloading: when a class or method have the same method name but different set of
parameters or attributes(signature).
Eg.

class oosad{
public static void main(String args[]) {
int output(10);
double output(10.0);}
static void output(int i) {
Overloading is about same method have
System.out.println("int i = " + i);}
different signature. Overriding is
static void output(double d) { // same name, different parameters redefining body of a method of superclass in
System.out.println("double j = " + j);} a subclass to change behavior of a method.
}
Out put will be int i=10
double d=10.0

13
Interfaces
oInterfaces: is a collection of methods of a class or component.

oSpecifies the set of services that may be provided by the class or component.

oOperations in an interface may adorned with


 visibility properties,
 concurrency properties,
 stereotypes,
 tagged values and
 constraints.

14
Abstraction
oAbstraction: Is a method of creating abstract classes.
o is the identification of the essential characteristics of an item. It means ignoring irrelevant
features, properties, or methods and emphasizing the relevant ones.
oAllows us to create interfaces.
o It allows us to represent a complex reality in terms of a simplified model.
o Its methods are partially implemented.
oUse of using interfaces and abstract classes in system development:
o makes our system easy to maintain and debug.

15
Relations
o Relations: No object is an independent. All objects are connected to other objects:
 directly or indirectly

 strongly or loosely

oMajor types of object relations:


 Association, Aggregation, Composition, Coupling ,Cohesion, Collaboration,
Generalization….

16
What is the need of making relations or connections?
oBy connecting objects, we make them more powerful.
Association
oweak form of connection between objects or entities.
o the objects may be part of a group, or family, of objects but they’re not completely
dependent on each other.
oAssociation: Symbolized by ( ).
oComposition: symbolized by filled diamond.
oAggregation: Symbolized by hollow diamond.

17
What is the need of making relations or connections?
Aggregation

 strong form of connection.

 it is the basic form of connection to build up a large system.

 It represents a close dependency.

18
19
20
Aggregation Composition

Faculty SalesOrder

1..* 1
(team-teaching is
possible)
0..* 1..*

CourseTeaching SalesOrderLineItem

(another: assembly --> part) (another: hand --> finger)


21
Cohesion and Coupling
Cohesion and Coupling are degrees of module dependency.
They are under decomposition concept in OO software engineering
Cohesion Cohesion

Coupling

22
Coupling
oCoupling:The degree of interdependence between two or more components in a system.
oCoupling is a measure of the extent of information interchange between modules.
Reduced coupling results Reduced Complexity
Coupling assesses the interactions between modules
It is important to distinguish kind and strength
kind: A calls B, C inherits from D, etc., directionality
strength: the number of interactions
oCoupling is related to cohesion.

23
Coupling types
Highly coupled
Loosely coupled
Uncoupled

24
Highly coupled
oTight coupling implies large dependence on the structure of one module by another

25
Loosely coupled
oLoose coupling is the opposite of tight coupling. Modules with loose coupling are more
independent and easier to maintain

26
Uncoupled
Uncoupled components exist.
No Dependencies

27
Good vs Bad Coupling
Modules that are loosely coupled (or uncoupled) are better than those that are tightly
coupled.Why? Because of the objective of modules to help with human limitations.

The more tightly coupled are two modules, the harder it is to think about them
separately, and thus the benefits become more limited.

28
Cohesion
oIt indicates how closely the elements or the statements of a module are associated with each
other in the system inside a specific module/subsystem.
oCohesion is a measure of the internal strength of a module.
oThe more closely the elements of a module are associated with each other, the higher the
cohesion of the module.
oCohesion refers to the number and diversity of tasks that a single unit is responsible for.
oIf each unit is responsible for one single logical task, we say it has high cohesion.
oCohesion applies to classes and methods,We aim for high cohesion.

29
Cohesion
oTypes:
Coincidental

Logical

Temporal

Sequential

oNote: Coupling vs Cohesion: Cohesion is a measure of the internal strength of a module, while coupling
is a measure of the extent of information interchange between modules.

30
Cohesion
oCoincidental: Occurs when elements are collected into a module simply because they
happen to fall together/concident.

oLogical : several logically related functions or data elements are placed in same component.

o occurs when the element of a module are grouped together according to certain class of
activity

oThe element falls into some general category because they all do the same kind of thing

31
Cont’d…
oTemporal: Sometime a component is used to initialize a system or a set variables.
oSuch a component performs several functions in sequence ,but the functions are related by
the timing involved, so its cohesion is temporal.
oOccurs when the elements of a module are grouped together because they are related by time
oSequential: If the output from one part of a component is input to the next part the
component has sequential cohesion.
oOccurs when a module contains elements that depend on the processing of previous elements

32
Generalization
orelationship denoting the inclusion of the behavior described by another entity.

oWhen we recognize commonality in characteristics and behavior we abstract these


Emplo yee

commonalties out – we generalize


Mana ger Prog ramm er

oprocess of forming a superclass. budgetsControlled project


progLanguages
dateAppointed

overy week form of association


Proj ect Dept. Strateg ic
Mana ger Mana ger Mana ger

projects dept responsibilities

33
Cont’d…
o Relation Strength: Composition >Aggregation>Association>Generalization
o Note: The differences between different types of relations is the underlying semantic
strength.
o We will see other types of relations in UML in chapter four in class diagrams.
 One-to –one
 One-to –many
 Many-to -many

34
35

You might also like