Chapter I OOP
Chapter I OOP
O
Polymorphism
O Abstraction
Inheritance
P
Orfel L. Bejarin,MIT Encapsulation
TOPICS
• OOP • Coupling
• Object • Cohesion
• Class • Association
• Inheritance • Aggregation
• Polymorphism • Composition
• Abstraction
• Encapsulation
OOP (Object-Oriented Programming)
A class should:
• Provide a well-defined interface - such as the remote control of the television.
• Represent a clear concept - such as the concept of a television.
• Be complete and well-documented - the television should have a plug and should have a
manual that documents all features.
• The code should be robust - it should not crash, like the television.
With a functional programming language (like C) we would have the
component parts of the television scattered everywhere and we would
be responsible for making them work correctly - there would be no case
surrounding the electronic components.
When a class is derived from a class which is also derived from another class, i.e. a class
having more than one parent class but at different levels, such type of inheritance is called
Multilevel Inheritance.
If we talk about the flowchart, class B inherits the properties and behavior of class A and
class C inherits the properties of class B. Here A is the parent class for B and class B is the
parent class for C. So, in this case class C implicitly inherits the properties and methods of
class A along with Class B. That’s what is multilevel inheritance.
3. HIERARCHICAL INHERITANCE:
When a class has more than one child classes (sub classes) or in other words,
more than one child classes have the same parent class, then such kind of
inheritance is known as hierarchical.
If we talk about the flowchart, Class B and C are the child classes which are
inheriting from the parent class i.e Class A.
4. HYBRID INHERITANCE:
To use an abstract class, you have to inherit it from another class where you have to provide
implementations for the abstract methods there itself, else it will also become an abstract
class.
Interface:
Interface in Java is a blueprint of a class or you can say it is a collection of abstract methods
and static constants. In an interface, each method is public and abstract but it does not
contain any constructor. Along with abstraction, interface also helps to achieve multiple
inheritance in Java.
ENCAPSULATION
• Binding (or wrapping) code and data together into a single unit are known as encapsulation. For
example, a capsule, it is wrapped with different medicines.
• It also means to hide your data in order to make it safe from any modification. What does this
mean? The best way to understand encapsulation is to look at the example of a medical capsule,
where the drug is always safe inside the capsule. Similarly, through encapsulation the methods and
variables of a class are well hidden and safe.
• A java class is the example of encapsulation. Java bean is the fully encapsulated class because all
the data members are private here.
There are some other terms which are used in Object-Oriented design:
• Coupling
• Cohesion
• Association
• Aggregation
• Composition
Coupling
Refers to the knowledge or information or dependency of another class. It arises
when classes are aware of each other. If a class has the details information of
another class, there is strong coupling. In Java, we use private, protected, and
public modifiers to display the visibility level of a class, method, and field. You
can use interfaces for the weaker coupling because there is no concrete
implementation.
Cohesion
Refers to the level of a component which performs a single well-defined task. A
single well-defined task is done by a highly cohesive method. The weakly
cohesive method will split the task into separate parts. The java.io package is a
highly cohesive package because it has I/O related classes and interface.
However, the java.util package is a weakly cohesive package because it has
unrelated classes and interfaces.
Association
Represents the relationship between the objects. Here, one object can be
associated with one object or many objects. There can be four types of
association between the objects:
• One to One
• One to Many
• Many to One, and
• Many to Many
Let's understand the relationship with real-time examples. For example, One
country can have one prime minister (one to one), and a prime minister can have
many ministers (one to many). Also, many MP's can have one prime minister (many
to one), and many ministers can have many departments (many to many).
Aggregation
is a way to achieve Association. Aggregation represents the relationship where
one object contains other objects as a part of its state. It represents the weak
relationship between objects. It is also termed as a has-a relationship in Java.
Like, inheritance represents the is-a relationship. It is another way to reuse objects.
Composition
is also a way to achieve Association. The composition represents the relationship
where one object contains other objects as a part of its state. There is a strong
relationship between the containing object and the dependent object. It is the
state where containing objects do not have an independent existence. If you
delete the parent object, all the child objects will be deleted automatically.