Vous êtes sur la page 1sur 20

Class and Object

Deependra Rastogi
Assiatant Professor
Galgotias University, Greater Noida
Introduction to Object-Oriented
Programming
Object-oriented programming, or OOP for short, is the dominant
programming paradigm these days, having replaced the “structured,”
procedural programming techniques that were developed in the
1970s. Since Java is object-oriented, you have to be familiar with
OOP to become productive with Java.

An object-oriented program is made of objects. Each object has a


specific functionality, exposed to its users, and a hidden
implementation. Many objects in your programs will be taken “off-
the-shelf” from a library; others will be custom-designed. Whether
you build an object or buy it might depend on your budget or on
time. But, basically, as long as an object satisfies your specifications,
you don’t care how the functionality is implemented.
Introduction to Object-Oriented
Programming
Traditional structured programming consists of designing a set of
procedures (or algorithms) to solve a problem. Once the procedures
are determined, the traditional next step was to find appropriate
ways to store the data.

This mimics the way programmers worked at that time. First, they
decided on the procedures for manipulating the data; then, they
decided what structure to impose on the data to make the
manipulations easier. OOP reverses the order: puts the data first,
then looks at the algorithms to operate on the data.

For small problems, the breakdown into procedures works very well.
But objects are more appropriate for larger problems. Consider a
simple web browser. It might require 2,000 procedures for its
implementation, all of which manipulate a set of global data. In the
object-oriented style, there might be 100 classes with an average of
20 methods per class.
Introduction to Object-Oriented
Programming
This structure is much easier for a programmer to grasp. It is also
much easier to find bugs in. Suppose the data of a particular object
is in an incorrect state. It is far easier to search for the culprit among
the 20 methods that had access to that data item than among 2,000
procedures.
Class and Object
A class is the template or blueprint from which objects are
made. When you construct an object from a class, you are
said to have created an instance of the class.

Encapsulation (sometimes called information hiding) is a key concept


in working with objects. Formally, encapsulation is simply combining
data and behavior in one package and hiding the implementation
details from the users of the object. The bits of data in an object are
called its instance fields, and the procedures that operate on the
data are called its methods. A specific object that is an instance of a
class will have specific values of its instance fields. The set of those
values is the current state of the object. Whenever you invoke a
method on an object, its state may change.
Class and Object
To work with OOP, you should be able to identify three key
characteristics of objects:

The object’s behavior—What can you do with this object, or what


methods can you apply to it?
The object’s state—How does the object react when you invoke
those methods?
The object’s identity—How is the object distinguished from others
that may have the same behavior and state?

All objects that are instances of the same class share a family
resemblance by supporting the same behavior. The behavior of an
object is defined by the methods that you can call.
Class and Object
Next, each object stores information about what it currently looks
like. This is the object’s state. An object’s state may change over
time, but not spontaneously. A change in the state of an object must
be a consequence of method calls. (If an object’s state changed
without a method call on that object, someone broke encapsulation.)

However, the state of an object does not completely describe it,


because each object has a distinct identity. For example, in an order
processing system, two orders are distinct even if they request
identical items. Notice that the individual objects that are instances
of a class always differ in their identity and usually differ in their
state.
Class and Object
The process of creating objects from a class is called
instantiation. An object is an instance of a class. The object is
constructed using the class as a blueprint and is a concrete
instance of the abstraction that the class represents. An
object must be created before it can be used in a program.

A reference value is returned when an object is created. A


reference value denotes a particular object. An object reference (or
simply reference) is a variable that can store a reference value. A
reference thus provides a handle to an object, as it can indirectly
denote an object whose reference value it holds. In Java, an object
can only be manipulated via its reference value, or equivalently by a
reference that holds its reference value.
Relationship Between Classes
The most common relationships between classes are
Dependence (“uses–a”)
Aggregation (“has–a”)
Inheritance (“is–a”)
The general form of Class
class classname
{
type instance-variable1;
// ...
type instance-variableN;

type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
Class Variables
A class can contain any of the following variable types.

Local variables: Variables defined inside methods, constructors or


blocks are called local variables. The variable will be declared and
initialized within the method and the variable will be destroyed when
the method has completed.

Instance variables: Instance variables are variables within a class


but outside any method. These variables are initialized when the
class is instantiated. Instance variables can be accessed from inside
any method, constructor or blocks of that particular class.

Class variables: Class variables are variables declared with in a


class, outside any method, with the static keyword.
Class Example
class Box
{
double width;
double height;
double depth;
}
class BoxDemo
{
public static void main(String args[])
{
Box mybox = new Box();
double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
Class Variables
A class can contain any of the following variable types.

Local variables: Variables defined inside methods, constructors or


blocks are called local variables. The variable will be declared and
initialized within the method and the variable will be destroyed when
the method has completed.

Instance variables: Instance variables are variables within a class


but outside any method. These variables are initialized when the
class is instantiated. Instance variables can be accessed from inside
any method, constructor or blocks of that particular class.

Class variables: Class variables are variables declared with in a


class, outside any method, with the static keyword.
Declaring Object
When you create a class, you are creating a new data type.
Obtaining objects of a class is a two-step process.

First, you must declare a variable of the class type. This variable
does not define an object. Instead, it is simply a variable that can
refer to an object.

Second, you must acquire an actual, physical copy of the object


and assign it to that variable. You can do this using the new
operator. The new operator dynamically allocates (that is, allocates
at run time) memory for an object and returns a reference to it. This
reference is, more or less, the address in memory of the object
allocated by new. This reference is then stored in the variable. Thus,
in Java, all class objects must be dynamically allocated.
Declaring Object
Box mybox; // declare reference to object
mybox = new Box(); // allocate a Box object

The first line declares mybox as a reference to an object of type


Box. After this line executes, mybox contains the value null, which
indicates that it does not yet point to an actual object. Any attempt
to use mybox at this point will result in a compile-time error. The
next line allocates an actual object and assigns a reference to it to
mybox. After the second line executes, you can use mybox as if it
were a Box object. But in reality, mybox simply holds the memory
address of the actual Box object.
Declaring Object
Declaring Object
It has this general form:
class-var = new classname( );

Here, class-var is a variable of the class type being created. The


classname is the name of the class that is being instantiated. The
class name followed by parentheses specifies the constructor for the
class. Aconstructor defines what occurs when an object of a class is
created. Constructors are an important part of all classes and have
many significant attributes. Most real-world classes explicitly define
their own constructors within their class definition. However, if no
explicit constructor is specified, then Java will automatically supply a
default constructor.
Declaring Object
It is important to understand that new allocates memory for an
object during run time. The advantage of this approach is that your
program can create as many or as few objects as it needs during the
execution of your program. However, since memory is finite, it is
possible that new will not be able to allocate memory for an object
because insufficient memory exists. If this happens, a run-time
exception will occur.

The distinction between a class and an object. A class creates a new


data type that can be used to create objects. That is, a class creates
a logical framework that defines the relationship between its
members. When you declare an object of a class, you are creating an
instance of that class. Thus, a class is a logical construct. An object
has physical reality.
Assigning Object Reference Variable
Object reference variables act differently than you might expect
when an assignment takes place. For example, what do you think the
following fragment does?
Box b1 = new Box();
Box b2 = b1;

You might think that b2 is being assigned a reference to a copy of


the object referred to by b1. That is, you might think that b1 and b2
refer to separate and distinct objects. However, this would be wrong.
Instead, after this fragment executes, b1 and b2 will both refer to
the same object. The assignment of b1 to b2 did not allocate any
memory or copy any part of the original object. It simply makes b2
refer to the same object as does b1. Thus, any changes made to the
object through b2 will affect the object to which b1 is referring, since
they are the same object.
Assigning Object Reference Variable

Vous aimerez peut-être aussi