Object Oriented
Programming
Object Oriented Programming
versus Procedural Approach
Procedural Programming focuses on
code acting on data thought, whereas
OOPA works on the thought of data
controlling access to code through
objects.
Object Oriented Programming is at
the heart of Java and not an option as
it is in C++.
OOPS Concepts
Abstraction
Encapsulation
Inheritance/Code reusability
Polymorphism
OOPS Components
Object
Class
Abstraction
The process of hiding irrelevant
details of code complexity from user.
Controls the visibility of information.
Focuses on the essential
characteristics of some object, relative
to the perspective of the viewer.
Encapsulation
The method of combining the data
member & member functions of an
object into a single unit.
Isolates the functional details of the
object from outside the class.
Inheritance/Code reusability
The process of deriving new classes from
the existing ones.
Allows a class to inherit the properties from
another class.
Inheritance provides the idea of reusability,
because using it we can modify the existing
classes by adding new features into it. The
resulting class will have the combined
features of both the classes.
Polymorphism
Ability to take more than one form.
The same operation may exhibit different
behavior in different instances.
Behavior depends on the types of data used
in the operation.
Types –
Runtime polymorphism
Compile time polymorphism
Object
Basic building block for OOP.
Usually correspond to real life entities.
A unit of s/w comprising of:
State/Attribute – defines the properties of the object &
the current values of each of these properties.
Behavior – defines the response of an object in terms
of its state changes and message passing.
Identity – defines a unique name.
An object stores its state in fields (variables in some
programming languages) and exposes its behavior
through methods (functions in some programming
languages).
Class
a blueprint that describes the nature
of something.
User-defined datatype.
Used to represent a template for
several similar type of objects.
Objects of the same class have the
same definition both for their state and
behavior.
Instance – creation of an object for a
particular class.
Constructors – initialize an object
immediately upon creation. A constructor is
automatically called immediately after the
object is created.
Interface - an interface is a group of related
methods with empty bodies.
Package - A package is a namespace that
organizes a set of related classes and
interfaces. Conceptually we can think of
packages as being similar to different
folders on your computer.
Access specifiers
Java allows us to control access to classes,
methods, and fields via so-called access
specifiers.
Java offers four access specifiers, listed
below in decreasing accessibility:
Public
Protected
Default
Private
Access level permitted by
each specifier
publi priva
Situation c
protected default
te
Accessible to class
yes yes yes no
from same package?
Accessible to class
from different yes no, unless it is a subclass no no
package?