Class and Object: Deependra Rastogi
Class and Object: Deependra Rastogi
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.
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.
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.)
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.
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.