Chapter 4 Basics of Object-Oriented Concepts
Chapter 4 Basics of Object-Oriented Concepts
3
What are Classes and Object?
oMethods: define objects behavior and specify the way in which an Object’s data are
manipulated.
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.
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
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
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.
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.
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.
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.
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
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
18
19
20
Aggregation Composition
Faculty SalesOrder
1..* 1
(team-teaching is
possible)
0..* 1..*
CourseTeaching SalesOrderLineItem
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.
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