0% found this document useful (0 votes)
7 views

Untitled Document

Uploaded by

parux82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Untitled Document

Uploaded by

parux82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Object oriented programming

Object-oriented programming (OOP) refers to a programming methodology based on


objects, instead of just functions and procedures. These objects are organized into classes,
which allow individual objects to be grouped together.

Basic terminologies of OOP

❖ Object

● An entity that has state and behavior is known as an object e.g.. chair, bike, marker,
pen, table, car, etc. It can be physical or logical (tangible and intangible). The
example of an intangible object is the banking system.

● An object has three characteristics:

1. State: represents the data (value) of an object.

2. Behaviour: represents the behavior (functionality) of an object such as deposit,


withdraw, etc.

3. Identity: An object identity is typically implemented via a unique ID. The value of the
ID is not visible to the external user. However, it is used internally by the JVM to
identify each object uniquely.

For Example. Pen is an object. Its name is Reynolds; color is white, known as its state. It is
used to write, so writing is its behavior.

● An object is an instance of a class. A class is a template or blueprint from which


objects are created. So, an object is the instance(result) of a class.

❖ Class

● A class is a group of objects which have common properties. It is a template or


blueprint from which objects are created. It is a logical entity. It can't be physical.

● A class can contain:

1. Fields
2. Methods
3. Constructors
4. Blocks
5. Nested class and interface

Instance variable
● A variable which is created inside the class but outside the method is known as an
instance variable. Instance variable doesn't get memory at compile time. It gets
memory at runtime when an object or instance is created. That is why it is known as
an instance variable.

Method

● A method is like a function which is used to expose the behavior of an object

➢ Advantage of Method

★ Code Reusability
★ Code Optimization

The Four Principles of Object- Oriented-Programming (OOP)

❖ Encapsulation

● Encapsulation is a process of wrapping code and data together into a single unit, for
example, a capsule which is mixed of several medicines.

● We can create a fully encapsulated class by making all the data members of the
class private. Now we can use setter and getter methods to set and get the data in it.

● Technically in encapsulation, the variables or data of a class is hidden from any other
class and can be accessed only through any member function of its own class in
which it is declared.

● As in encapsulation, the data in a class is hidden from other classes using the data
hiding concept which is achieved by making the members or methods of a class
private, and the class is exposed to the end-user or the world without providing any
details behind implementation using the abstraction concept, so it is also known as a
combination of data-hiding and abstraction.

❖ Abstraction

● Data Abstraction is the property by virtue of which only the essential details are
displayed to the user. The trivial or the non- essentials units are not displayed to the
user. Ex: A car is viewed as a car rather than its individual components.

● Data Abstraction may also be defined as the process of identifying only the required
characteristics of an object ignoring the irrelevant details. The properties and
behaviors. ors of a of an object differentiate it from other objects of similar type and
also help in classifying/grouping the objects.
● Consider a real-life example of a man driving a car. The man only knows that
pressing the accelerators will increase the speed of car or applying brakes will stop
the car, but he does not know about how on pressing the the a accelerator the speed
is actually increasing, he does not know about the inner mechanism of the car or the
implementation of the accelerator, brakes, etc in the car. This is what abstraction is.

❖ Inheritance

● Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the


mechanism in which one class is allowed to inherit the features(fields and methods)
of another class.

➢ Important terminology:

★ Super Class: The class whose features are inherited is known as superclass(or a
base class or a parent class).

★ Sub Class: The class that inherits the other class is known as a subclass(or a derived
class, extended class, or child class). The subclass can add its own fields and
methods in addition to the superclass fields and methods.

★ Reusability: Inheritance supports the concept of "reusability". i.e. when we want to


create a new class and there is already a class that includes some of the code that
we want, we can derive our new class from the existing class. By doing this, we are
reusing the fields and methods of the existing class.

❖ Polymorphism

● Polymorphism is a concept by which we can perform a single action in different ways.


Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly"
means many and "morphs" means forms. So polymorphism means many forms.

● There are two types of polymorphism in Java: compile-time polymorphism and


runtime polymorphism.
● We can perform polymorphism in java by method overloading and method
overriding.

★ Compile Time Polymorphism: Whenever an object is bound with their functionality at


the compile-time, this is known as the compile time polymorphism.

★ Run-Time Polymorphism: Whenever an object is bound with the functionality at run


time, this is known as runtime polymorphism.

You might also like