0% found this document useful (0 votes)
34 views27 pages

Chapter I OOP

This document discusses key concepts in object-oriented programming (OOP) including objects, classes, inheritance, polymorphism, abstraction, and encapsulation. It provides examples and definitions of each concept. OOP is a programming paradigm that uses classes and objects to design software. Some key benefits of OOP include code reusability, abstraction, and information hiding.

Uploaded by

Geraldine
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)
34 views27 pages

Chapter I OOP

This document discusses key concepts in object-oriented programming (OOP) including objects, classes, inheritance, polymorphism, abstraction, and encapsulation. It provides examples and definitions of each concept. OOP is a programming paradigm that uses classes and objects to design software. Some key benefits of OOP include code reusability, abstraction, and information hiding.

Uploaded by

Geraldine
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/ 27

Object Class

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 methodology or paradigm to design a program using classes and objects.


• Programming style which is associated with the concepts like class, object,
Inheritance, Encapsulation, Abstraction, Polymorphism. Most popular
programming languages like Java, C++, C#, Python, Ruby, etc. follow an
object-oriented programming paradigm.
OBJECT
• Any entity that has state and behavior is known as an
object. For example, a chair, pen, table, keyboard, bike,
etc. It can be physical or logical.
• An Object can be defined as an instance of a class. An
object contains an address and takes up some space in
memory. Objects can communicate without knowing the
details of each other's data or code. The only necessary
thing is the type of message accepted and the type of
response returned by the objects.
Example:
A dog is an object because it has states like color, name, breed, etc. as well as
behaviors like wagging the tail, barking, eating, etc.
CLASS
• Collection of objects is called class. It is a logical entity.
• A class can also be defined as a blueprint/template from which you
can create an individual object. Class doesn't consume any space.
• A class is a description for a set of objects.
note the three computers on a conveyer belt in a manufacturing plant:
• The three computers represent objects, and the specifications document
represents a class. The specifications document is a blueprint that describes the
computers: it lists the computers' components and describes the computers'
features.
If we think of a real-world object, such as a television, it will have several features
and properties:
• We do not have to open the case to use it.
• We have some controls to use it (buttons on the box, or a remote control).
• We can still understand the concept of a television, even if it is connected to a DVD
player.
• It is complete when we purchase it, with any external requirements well
documented.
• The TV will not crash!
The concept of a class - television example.

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.

Classes allow us a way to represent complex structures within a


programming language. They have two components:
• States - (or data) are the values that the object has.
• Methods - (or behavior) are the ways in which the object can interact
with its data, the actions.
INHERITANCE
• When one object acquires all the properties and behaviors of a parent
object, it is known as inheritance. It provides code reusability. It is used
to achieve runtime polymorphism.
• In OOP, computer programs are designed in such a way where
everything is an object that interact with one another. Inheritance is one
such concept where the properties of one class can be inherited by the
other. It helps to reuse the code and establish a relationship between
different classes.
As we can see in the image, a child inherits the properties from his father. Similarly, in
Java, there are two classes:
• 1. Parent class (Super or Base class)
• 2. Child class (Subclass or Derived class)
A class which inherits the properties is known as Child Class whereas a class whose
properties are inherited is known as Parent class.
INHERITANCE IS FURTHER CLASSIFIED INTO 4 TYPES:
1. SINGLE INHERITANCE:

In single inheritance, one class inherits the properties of another.


It enables a derived class to inherit the properties and behavior from a
single parent class. This will in turn enable code reusability as well as
add new features to the existing code.
2. MULTILEVEL INHERITANCE:

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:

Hybrid inheritance is a combination of multiple inheritance and multilevelinheritance.


Since multiple inheritance is not supported in Java as it leads to ambiguity, so this
type of inheritance can only be achieved through the use of the interfaces.
If we talk about the flowchart, class A is a parent class for class B and C, whereas
Class B and C are the parent class of D which is the only child class of B and C.
POLYMORPHISM
• If one task is performed in different ways, it is
known as polymorphism. For example: to convince
the customer differently, to draw something, for
example, shape, triangle, rectangle, etc.
• In Java, we use method overloading and method
overriding to achieve polymorphism.
• Another example can be to speak something; for
example, a cat speaks meow, dog barks woof,
etc.
• Polymorphism means taking many forms, where ‘poly’ means many and ‘morph’
means forms. It is the ability of a variable, function or object to take on multiple
forms. In other words, polymorphism allows you define one interface or method and
have multiple implementations.
• Suppose, we need to color a shape, there are multiple shape option (rectangle,
square, circle). However, we could use same method to color any shape. This
concept is called Polymorphism.
ABSTRACTION
• Hiding internal details and showing functionality is known as abstraction. For
example, phone call, we don't know the internal processing.
• In Java, we use abstract class and interface to achieve abstraction.
• Abstraction refers to the quality of dealing with ideas rather
than events. It basically deals with hiding the details and showing
the essential things to the user. If you look at the image here,
whenever we get a call, we get an option to either pick it up or
just reject it. But in reality, there is a lot of code that runs in the
background. So, you don’t know the internal processing of how a
call is generated, that’s the beauty of abstraction. Therefore,
abstraction helps to reduce complexity.
You can achieve abstraction in two ways:
• a) Abstract Class
• b) Interface
Abstract class:
Abstract class in Java contains the ‘abstract’ keyword. Now what does the abstract keyword
mean? If a class is declared abstract, it cannot be instantiated, which means you cannot
create an object of an abstract class. Also, an abstract class can contain abstract as well as
concrete methods.

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.

You might also like