Object Oriented Programming
Java Basics (EC605A)
What is Object Oriented
Programming?
Object-oriented programming (OOP) is a
programming language model organized
around objects rather than "actions" and
data rather than logic.
Contd….
• a program has been viewed as a logical
procedure that takes input data, processes it,
and produces output data.
• The programming challenge was seen as how
to write the logic, not how to define the data.
• Object-oriented programming takes the view
that what we really care about are the
objects we want to manipulate rather than
the logic required to manipulate them.
Contd….
• Examples of objects range from human beings
(described by name, address, and so forth) to
buildings and floors (whose properties can be
described and managed) down to the
little widgets on a computer desktop (such as
buttons and scroll bars).
Contd….
• The first step in OOP is to identify all the
objects the programmer wants to manipulate
and how they relate to each other, an exercise
often known as data modelling.
• Once an object has been identified, it is
generalized as a class of objects which
defines the kind of data it contains and any
logic sequences that can manipulate it.
Why Object Oriented Programming
An object-oriented programming is a way of
programming which enables programmers to
think like they are working with real-life
entities(a thing with distinct and independent
existence) or objects. In real-life, people have
knowledge and can do various works.
In OOP, objects have fields to
store knowledge/state/data
and
can do various works — methods.
Contd….
Object Oriented Programming
• The four principles of object-oriented
programming are Encapsulation,
Abstraction, Inheritance, and Polymorphis
m.
Encapsulation
• Say we have a program. It has a few logically
different objects which communicate with
each other — according to the rules defined in
the program.
• Encapsulation is achieved when each object
keeps its state private, inside a class. Other
objects don’t have direct access to this state.
Instead, they can only call a list of public
functions — called methods.
Contd….
• So, the object manages its own state via
methods — and no other class can touch it
unless explicitly allowed.
• If you want to communicate with the object,
you should use the methods provided. But (by
default), you can’t change the state.
Contd….
Let’s say we’re building a tiny Sims game.
There are people and there is a cat. They
communicate with each other. We want to
apply encapsulation, so we encapsulate all
“cat” logic into a Cat class. It may look like
this:
Contd….
Contd….
• Here the “state” of the cat is the private
variables mood, hungry and energy. It also has a
private method meow(). It can call it whenever it
wants, the other classes can’t tell the cat when to
meow.
• What they can do is defined in the public
methods sleep(), play() and feed(). Each of them
modifies the internal state somehow and may
invoke meow(). Thus, the binding between the
private state and public methods is made.
Inheritance
• Inheritance is an important pillar of OOP(Object
Oriented Programming). It is the mechanism in
java by which one class is allow to inherit the
features(fields and methods) of another class.
• Super Class: The class whose features are
inherited is known as super class(or a base class
or a parent class).
• Sub Class: The class that inherits the other class
is known as sub class(or a derived class,
extended class, or child class). The subclass can
add its own fields and methods in addition to the
super class fields and methods.
Contd…..
• 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 one of the OOPs feature that
allows us to perform a single action in different
ways.
• The process of representing one form in
multiple forms is known as Polymorphism.
• Example-Suppose if you are in class room that
time you behave like a student, when you are in
market at that time you behave like a customer,
when you at your home at that time you behave
like a son or daughter, Here one person present in
different-different behaviors.
Contd….
• 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.