You are on page 1of 34

Chapter Four

Understanding the Basics: Object-oriented concepts

07:46 AM • SAD
 OO concepts from a structured point  Persistence
of view  Coupling
 Abstraction, Encapsulation, and
 Cohesion
information hiding
 Polymorphism
 Inheritance
 Interfaces
 Association
 Components
 Aggregation
 Patterns
 Collaboration
07:46 AM • SAD
OO concepts from a structured point of view

• Focuses on capturing the structure and behavior of information systems into small modules that
combine both data and process.
• To improve the quality and productivity of system analysis and design so as to make it more
usable.
• It performs well in situations where systems are undergoing continuous design, adaption, and
maintenance.
• It identifies the objects in the problem domain, classifying them in terms of data and behavior.

07:46 AM • SAD
OO concepts from a structured point of view

The OO model is beneficial in the following ways −


It facilitates changes in the system at a low cost.
It promotes the reuse of components.
It simplifies the problem of integrating components to configure large systems.
It simplifies the design of distributed systems.

07:46 AM • SAD
OO concepts from a structured point of view

Elements of Object-Oriented System:


Objects:- is something that exists within the problem domain and can be identified by data (attribute)
or behavior.
 An object is an instance of a class.
 Object is a real-world entity such as a pen, laptop, mobile, bed, keyboard, mouse, chair, etc.
 Object allocates memory when it is created.
 It is a physical entity.
 Eg: Man, Woman, Banana, etc.

07:46 AM • SAD
OO concepts from a structured point of view

Elements of Object-Oriented System:


Class:- is a blueprint or template from which objects are created.
 It is a group of similar objects.
 It is a logical entity.
 It doesn’t allocate memory when it is created.
 Eg: Class: Fruit Object: Apple, Banana, Mango, Guava, etc.

07:46 AM • SAD
OO concepts from a structured point of view

Elements of Object-Oriented System:


• Attributes:- They describe information about the object.
• Behavior:- It specifies what the object can do. It defines the operation performed on objects.
• Methods:- Methods determine the behavior of a class. They are nothing more than an action that
an object can perform.
• Message:- A message is a function or procedure call from one object to another. They are
information sent to objects to trigger methods.
• Essentially, a message is a function or procedure call from one object to another.

07:46 AM • SAD
OO concepts from a structured point of view

Elements of Object-Oriented System:

07:46 AM • SAD
Abstraction, Encapsulation, and information hiding

• Abstraction:- is a process of hiding the implementation details and showing only functionality to
the user.
• It is a feature of OOPs.
• It is used to hide unnecessary information or data from the user but shows the essential data that is
useful for the user.
• It can be achieved by using the interface and the abstract class.
• In interfaces, only the methods are exposed to the end user.
• The best example of abstraction is a TV remote. The user only interacts with the outer
interface which is nothing but keys.
• The user only knows which key to press for what function.

07:46 AM • SAD
Abstraction, Encapsulation, and information hiding

Abstract Method in Java


• A method that is declared as abstract and does not have an implementation.

Eg: abstract void printStatus(); //no method body and abstract


• Example:

07:46 AM • SAD
Abstraction, Encapsulation, and information hiding

• Abstraction:- is a process of hiding the implementation details and showing only functionality to
the user.

07:46 AM • SAD
Abstraction, Encapsulation, and information hiding

• Encapsulation:- binding data and corresponding methods (behavior) together into a single unit.
» is a programming technique that binds the class members (variables and methods)
together and prevents them from being accessed by other classes.
» Thereby, we can keep variables and methods safes from outside interference and
misuse.
» In the encapsulation technique, we declare fields as private in the class to prevent
other classes from accessing them directly.
» The required encapsulated data can be accessed by using the public Java getter
and setter method.

07:46 AM • SAD
Abstraction, Encapsulation, and information hiding

• Encapsulation:-

07:46 AM • SAD
Abstraction, Encapsulation, and information hiding
• Encapsulation:-

Realtime Example of Encapsulation

Realtime Example 1:
» School bag is one of the most real examples of Encapsulation. School bags can keep our
books, pens, etc.

Realtime Example 2:
» When you log into your email accounts such as Gmail, Yahoo Mail, or Rediff Mail, there
are a lot of internal processes taking place in the backend and you have no control over it.
» When you enter the password for logging, they are retrieved in an encrypted form and
verified, and then you are given access to your account.
» You do not have control over it that how the password has been verified. Thus, it keeps our
account safe from being misused.
07:46 AM • SAD
Abstraction, Encapsulation, and information hiding

• Encapsulation:-

How to achieve or implement Encapsulation in Java


1. Declaring the instance variable of the class as private so that it cannot be
accessed directly by anyone from outside the class.
2. Provide the public setter and getter methods in the class to set/modify the
values of the variable/fields.
 Eg:

07:46 AM • SAD
Abstraction, Encapsulation, and information hiding

• Data/Information Hiding:- ensure the security of members of a class from illegal access.
» protects the data members from being changed or hacked.

• Eg: Suppose you declared a CheckAccount class and you have a data member balance
inside that class. Here, the balance of an account is sensitive information. You may allow
an outside application to check the balance inside of an account but, you won’t allow an
outside application to alter the balance attribute. Thus declaring the balance attribute
private would restrict access to balance from an outside application.

07:46 AM • SAD
Abstraction, Encapsulation, and information hiding

• Key Difference Between Data/Information Hiding and Encapsulation:-

1) Encapsulation deals with hiding the complexity of a program.

Data hiding deals with the security of data in a program.

2) Encapsulation focuses on wrapping (encapsulating) the complex data in order to present a


simpler view for the user.

Data hiding focuses on restricting the use of data, intending to assure data security.

3) In encapsulation data can be public or private.

In data hiding, data must be private only.

4) Data hiding is a process as well as a technique.

Encapsulation is a subprocess in data hiding.

07:46 AM • SAD
Inheritance

 Similarities often exist between different classes.


 Two or more classes often share the same attributes and/or the same methods.
 Because you do not want to have to write the same code repeatedly, you want a mechanism that
takes advantage of these similarities.
 Inheritance models ―” is a”, ―” is kind of”, and ―” is like” relationships, enabling you to reuse
existing data and code easily.
 For example, students have names, addresses, and telephone numbers, and they drive
vehicles.
 At the same time, professors also have names, addresses, and telephone numbers, and they
drive vehicles.

07:46 AM • SAD
Inheritance

07:46 AM • SAD
Association

• Association relationship

Example:
1) A single teacher has multiple students.

2) A single student can associate with many teachers.

07:46 AM • SAD
Association

• Association relationship
Common items to model for an association:
1. Directionality: The open arrowheads indicate the directionality of the association. When there is one
arrowhead the association is unidirectional: it can be traversed in one direction only (in the direction of the
arrow). When there are either zero or two arrowheads the association is bi-directional: it can be traversed in
both directions. Although you should indicate both arrowheads it is common practice to drop the
arrowheads from bi-directional associations.
2. Label: The label, which is optional, is typically one or two words describing the association. Reading one
class, the label, and then the other class should produce a sentence fragment that describes the relationship,
e.g., Professor teaches Seminars. Avoid generic labels like ―has‖ or ―communicates with‖ as much as
possible.

07:46 AM • SAD
Association

• Association relationship

Common items to model for an association:

3. Multiplicity: The multiplicity of the association is labeled on either end of the line, with one
multiplicity indicator for each direction

07:46 AM • SAD
Aggregation

• Aggregation: is a subset of association, which is a collection of different things.


» It represents a relationship and is more specific than an association.
» It describes a part-whole or part-of relationship. It is a binary association, i.e., it
only involves two classes.
» It is a kind of relationship in which the child is independent of its parent.

 Eg: Here we are considering a car and a wheel example. A car cannot move without a wheel.
But the wheel can be independently used with the bike, scooter, cycle, or any other vehicle.
The wheel object can exist without the car object, which proves to be an aggregation
relationship.

07:46 AM • SAD
Aggregation

• Aggregation:

07:46 AM • SAD
Composition

• The composition: depicts dependency between a composite (parent) and its parts (children),
which means that if the composite is discarded, so will its parts get deleted. It exists between
similar objects.
• As you can see from the example given below, the composition association relationship connects
the Person class with the Brain class, Heart class, and Legs class. If the person is destroyed, the
brain, heart, and legs will also get discarded.

07:46 AM • SAD
Collaboration
• Classes often need to work together to fulfill their responsibilities.
• Collaboration occurs between objects when one object asks another for information or to do something.
 For example, an airplane collaborates with its engines to fly. For the plane to go faster, the engines must
go faster. When the plane needs to slow down, the engines must slow down.
 If the airplane did not collaborate with its engines, it would be unable to fly.

 Objects collaborate with one another by sending each other messages. A message is either a request to do
something or a request for information. Messages are modeled in sequence and collaboration diagrams.

07:46 AM • SAD
Collaboration

07:46 AM • SAD
Persistence

• Persistence focuses on the issue of how to make objects available for future use of your software.
• In other words, how to save objects for permanent storage. To make an object persistent, you must
save the values of its attributes to permanent storage (such as a relational database or a file) as
well as any information needed to maintain the relationships (aggregation, inheritance, and
association) with which it is involved.
• In other words you need to save the appropriate properties to permanent storage. In addition to
saving objects, persistence is also concerned with their retrieval and deletion.

07:46 AM • SAD
Coupling

• Coupling is a measure of how two items, such as classes or methods, are interrelated. When one
class depends on another class, we say they are coupled.
• When one class interacts with another class, but does not know any of the implementation details
of the other class, we say they are loosely coupled. When one class relies on the implementation
(that is, it directly accesses the data attributes of the other), we say they are tightly coupled.

07:46 AM • SAD
Cohesion

• Cohesion defines how effectively elements within a Module or object work together to fulfill a
single well-defined purpose. A high level of cohesion means that elements within a module are
tightly integrated and working together to achieve the desired functionality.

07:46 AM • SAD
Polymorphism

• Polymorphism:- is the ability of an object to take on many forms.


» it occurs when we have many classes that are related to each other by inheritance.
» For example, think of a superclass called Animal that has a method called
animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, and Birds - And
they also have their own implementation of an animal sound (the pig oinks, and
the cat meows, etc.):
» Eg:

07:46 AM • SAD
Interfaces

• Another way to achieve abstraction in Java, is with interfaces.


• An interface is a completely "abstract class" that is used to group related methods with empty
bodies:
• Example:

// interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void run(); // interface method (does not have a body)
}
Eg:

07:46 AM • SAD
Patterns

• A pattern is a solution to a common problem taking relevant forces into account, effectively
supporting the reuse of proven techniques and approaches of other developers. Several flavors of
patterns exist, including analysis patterns, design patterns, and process patterns.
• Analysis patterns describe a solution to common problems found in the analysis/business domain of
an application,
• design patterns describe a solution to common problems found in the design of systems, and process
patterns address software process related issues.
• Design Patterns are reusable solutions to commonly occurring problems(in the context of software
design).
• Design patterns were started as best practices that were applied again and again to similar problems
encountered in different contexts.

07:46 AM • SAD
07:46 AM • SAD

You might also like