You are on page 1of 13

Java Tutorials

Introduction to
Objects
What is Object Oriented
Programming (OOPs)
A methodology that is used to design a
program using classes and objects
A process to simplify the software
development concepts that provides
Classes
Objects
Inheritance
Polymorphism
Abstraction
Encapsulation
What is an object?
Object can be considered as a fancy
object that stores data but there is an
addition, you can make a request to that
object to perform an operation on itself
In a single line, an object has state,
behavior, and identity
Thus an object has internal data
(represents the state), methods ( defines
the behavior), and has unique address
space ( identity of the object)
An object as an interface
Let us consider a example, banker teller
problem
We have accounts, transactions,
customers, and unit of money and all
these are objects
Objects which are identical except from
the state in which they are during the
execution can be said as classes of
objects
Classes are the fundamental of object
oriented programming and they are
called as abstract data type
Abstract Data Type
A data type that is defined by its
behavior from the users point of view of
that data which can operations that can
be performed on those objects, behavior
of those operations on the object, and the
possible values
ADT is thus a class of objects that have
same logical behavior which is defined by
set of values and set of operations.
Class
Objects that behave the same way but
are in different state during the execution
can be grouped into a classes
Classes is an abstract data type
Thus a class describes a set of objects
with identical behavior and
characteristics
We can consider class as a data type
because a integer for example has
characteristics and behavior
Manipulating objects with
Reference
Each and every programming language has
its own way of manipulating the elements in
memory
In Java, when you want to manipulate an
object, you will manipulate the reference
to that object.
For example, you can consider a remote
control and a television. As long as you hold
the remote control, you can operate on the
television. Here television is the object and
remote control is the reference to the object
Manipulating objects with
Reference
A reference is not necessarily connected to
an object like a remote controller can stand
on its own without a television
String s; - Creates a string reference but not
the object. If you try to operate on s now,
youll get an error because it is not actually
attached to anything
String s = asdf; - Now the reference is
initialized and the string to which it is
initialized is the object. So if we manipulate
the variable s then the string asdf is altered.
Creating all the objects
When we create a reference, we need to
connect it with a new object.
new operator is used to create a new
object. The keyword new is make me a
new one of these objects.
Example String s = new String(asdf);
In the above example, new operator
means Make me a new string and we
do give the information on how we make
the string as we have given the initial
string
Where Storage lives
Registers
Fastest storage as it exists inside the
processor.
Number of registers are limited and are
allocated whenever needed
We dont have direct control on the registers
Stack
Lives in Random access memory but there is a
direct support from the process using the
stack pointer
Stack pointer is moved down to create a new
memory and moved up when the memory is
released
Fast and efficient storage
Where Storage lives
Heap
General-purpose pool of memory inside RAM
where all the Java objects live
Compiler doesnt need to know how long that
storage must stay on the heap
With new operator, the object is created in
heap when the code is executed
The time taken to allocate and clean up of the
memory in heap is slower when compared to
allocating memory in stack
Where Storage lives
Constant Storage
Constant value is stored directly in the
program code and sometimes it is stored in the
read-only memory for embedded systems
Non-RAM Storage
Data that lives completely outside of a
program when a program is running
Streamed objects Objects are turned into
streams of bytes to be sent to another machine
Persistent objects Objects are placed on disk
so they will hold their state even when the
program is terminated

You might also like