KEMBAR78
Concepts | PDF | Inheritance (Object Oriented Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
6 views8 pages

Concepts

The document outlines key concepts of object-oriented programming (OOP), including classes, objects, inheritance, encapsulation, abstraction, and polymorphism. It explains how these concepts differentiate OOP from procedure-oriented programming using a student example, highlighting the relationships and interactions between classes and objects. The document also discusses the mechanisms of achieving these concepts in programming, such as method overloading and overriding.

Uploaded by

boomika
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)
6 views8 pages

Concepts

The document outlines key concepts of object-oriented programming (OOP), including classes, objects, inheritance, encapsulation, abstraction, and polymorphism. It explains how these concepts differentiate OOP from procedure-oriented programming using a student example, highlighting the relationships and interactions between classes and objects. The document also discusses the mechanisms of achieving these concepts in programming, such as method overloading and overriding.

Uploaded by

boomika
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/ 8

Created by Turbolearn AI

OOP Concepts
In object-oriented programming (OOP), several key concepts are fundamental. These
concepts distinguish OOP from procedure-oriented programming. Let's explore these
concepts with a real-life example using students.

‍Class and Object


Consider a class of students containing both males and females.

Each student shares common properties such as:

Two hands
Two legs
Two eyes
Nose
Height
Weight
Color

They also share common functions (or behaviors) like:

Sleeping
Eating
Studying
Speaking
Sitting

In this context:

A class is a blueprint or a user-defined data type that defines the core


properties (attributes) and functions (behaviors/methods) of an entity.

For example, Student is a class with attributes like legs, hands, height, weight, color,
and functions like eat(), sleep(), sit(), and speak().

An object is a real-world entity or an instance of a class. It has a physical


existence and occupies memory.

Page 1
Created by Turbolearn AI

For instance, students like Ram, Jenny, Rahul, and Nisha are objects. Rahul is an
object of the Male class, while Nisha is an object of the Female class.

Here is a table summarizing the differences between classes and objects:

Feature Class Object

Definition Blueprint or template Instance of a class


Physicality Logical, not physical Physical, real-world entity
Memory No memory allocation when Memory is allocated when an object
Allocation the class is defined is created
Ram, Nisha, Rahul, Jenny (specific
Example Student, Male, Female
instances of these classes)

Inheritance
Inheritance allows a class to inherit properties and functions from another class.

In our student example:

Both Male and Female classes can inherit common properties and functions from
the Student class.
Instead of redefining common attributes and behaviors, these classes can reuse
the code defined in the Student class.
Male and Female can also have their own unique attributes and functions specific
to their class.

In technical terms, the Male and Female classes are known as child classes
or derived classes, while the Student class is known as the base class or
parent class.

Inheritance promotes code reusability and increases productivity.

Object Communication
Objects can interact with each other through message passing without needing to
know the internal details of each other’s code or data.

For example, if Rahul wants to communicate with Ram, Rahul only needs to know:

Page 2
Created by Turbolearn AI

The format of the message that Ram will accept.


The language Ram understands (e.g., Hindi or English).
The language in which Ram will respond.

Similarly, in programming, one object interacts with another by knowing the type of
message to send and the type of response to expect, without needing to know the
internal code or data of the other object.

Data and Code


Each object has its own data and code (functions) to manipulate that data.

For instance, Ram has his own:

Height
Weight
Color

And functions that can modify these attributes. If Ram eats more, his weight
increases, but this change does not affect the weight of other students like Rahul,
Jenny, or Nisha. The data and code are specific to each object.

Abstraction
Abstraction involves hiding the implementation details from the user and only
showing the relevant information.

Abstraction is hiding implementation detail from the outside world, only


showcasing the relevant details which are relevant to the world/users.

Page 3
Created by Turbolearn AI

Example 1: Coffee Machine

A coffee machine with one button labeled "make coffee" abstracts the
complex process of coffee making.
Users don't need to know the details of boiling water, adding coffee, etc.

Example 2: Switching on a Fan

When you switch on a fan, you don't need to know the underlying
mechanics.
You just press the button, and the fan starts.

How to Achieve Abstraction in Java:

Using interfaces and abstract classes.

Benefits:

In large-scale applications with extensive codebases, abstraction


simplifies usage.
Developers can use functions without understanding their inner workings.

Other examples:

Animals or aliens know that a student or male/female can walk and eat,
but they don't know the implementation detail.

Encapsulation
Encapsulation involves wrapping data and functions into a single unit.

Encapsulation is binding data and the functions into a single unit.

Page 4
Created by Turbolearn AI

Sometimes referred to as data hiding.

Like a capsule medicine, it hides the inner contents with a wrapper.

Class Example:

In a class, data (attributes) and methods (functions/behavior) are bound


together.
This is encapsulation.

C++ Access Specifiers:

private: Data is hidden from the outside world.


public: Methods that can be accessed from the outside world.
protected

Mechanism:

Data is kept private.


Only public methods can access and manipulate the data.
Other objects cannot directly access the data.

Real-World Example: College Departments

Departments like CSC and Mechanical are separate units.


If a faculty from CSC needs data from the Mechanical department, they
can't directly access it.
They must request the data through a person in the Mechanical
department.
This increases data privacy and security.

Benefits:

Enhances data security and privacy.


Prevents unauthorized access to data.

How to Achieve Encapsulation:

Keep data private.


Use public methods to access and manipulate the data.

Polymorphism

Page 5
Created by Turbolearn AI

Polymorphism means "many forms."

Polymorphism is having many forms.

Achieved through overloading and overriding.

Real-Life Example: A person having multiple forms.

One person can behave differently in different situations (e.g., as a faculty


member, class teacher, friend, or colleague).

Overloading
Function overloading involves having multiple functions with the same name but
different parameters.

Example:

sum(int a, int b)
sum(int a, float b)
sum(float a, float b)
sum(int a, int b, int c)

The sum function can take different types and numbers of arguments.

The function behaves differently based on the input parameters.

Overriding
Function overriding allows a child class to redefine a function already defined in its
parent class.

Page 6
Created by Turbolearn AI

Function name, number of arguments, type of arguments, and return type are
the same.

Only the implementation changes.

Example:

In a parent class:

$sum = x + y$

In a child class:

$sum = x - y$

The child class overrides the implementation of the sum function.

Real-Life Example:

Parent class (e.g., walk function).


Male and female classes inherit this function.

Overriding vs. Overloading


Overriding and overloading are two different ways to achieve polymorphism in
object-oriented programming.

Page 7
Created by Turbolearn AI

Overriding:

Occurs when a subclass provides a specific implementation of a method


that is already defined in its parent class.
The method in the subclass has the same name, same arguments, and
same return type as the method in the parent class.
Allows a subclass to change the behavior of a method inherited from its
parent class.

Overriding is when a subclass provides a different implementation


for a method that is already defined in its superclass. The method
signature (name, parameters, return type) remains the same.

Example:
A Student (parent) class has a method walk() that represents normal
walking.
A Female (child) class inherits from the Student class but overrides
the walk() method to implement walking in reverse.

Overloading:

Occurs when a class has multiple methods with the same name but
different parameters (number, type, or order).
Allows a class to provide different ways to call the same method with
different inputs.

Overloading is when a class has multiple methods with the same


name but different parameter lists.

Example:
A class has two walk() methods: one that takes legs as an argument
and another that takes hands as an argument.

Polymorphism Types
Achieving polymorphism through overloading is known as compile-time
polymorphism.
Achieving polymorphism through overriding is known as runtime
polymorphism.

Page 8

You might also like