© Prognoz Technologies Pvt. Ltd
Object & Class
© Prognoz Technologies Pvt. Ltd
What is Object ?
 Objects are key to understanding object-oriented technology.
 Look around right now and you'll find many examples of real-world
objects: your dog, your desk, your television set, your bicycle.
2
© Prognoz Technologies Pvt. Ltd
Continue…
 Real-world objects share two characteristics: They all have state and
behavior.
 Dogs have state (name, color, breed, hungry) and behavior (barking,
fetching, wagging tail).
© Prognoz Technologies Pvt. Ltd
Continue…
Bicycles also have state(current gear, current pedal cadence, current speed)
and behavior (changing gear, changing pedal cadence, applying brakes).
4
© Prognoz Technologies Pvt. Ltd
What is Class ?
 In the real world, you'll often find many individual objects all of the same
kind. There may be thousands of other bicycles in existence, all of the
same make and model.
 Each bicycle was built from the same set of blueprints and therefore
contains the same components.
 In object-oriented terms, we say that your bicycle is an instance of the
class of objects known as bicycles.
 A class is the blueprint from which individual objects are created.
© Prognoz Technologies Pvt. Ltd
Continue…
© Prognoz Technologies Pvt. Ltd
Continue…
OBJECTS
CLASSES
© Prognoz Technologies Pvt. Ltd
A Class acts as the template from which an instance of an object is
created. The class defines the properties of the object and the
methods used to control the object's behavior.
A Class specifies the structure of data as well as the methods which
manipulate that data. Such data and methods are contained in each
instance of the class.
A Class is a model or template that can be instantiated to create
objects with a common definition, and therefore common
properties, operations and behavior.
A Class provides a template for defining the behavior of a particular
type of object. Objects are referred to as “instances” of a class.
© Prognoz Technologies Pvt. Ltd
CLASSES
class One
{
}
class Book
{
}
class College
{
}
class AnyThing
{
}
© Prognoz Technologies Pvt. Ltd
Class Definition
A class contains a name, several variable declarations (instance variables)
and several method declarations. All are called members of the class.
General form of a class:
class classname {
type instance-variable-1;
…
type instance-variable-n;
type method-name-1(parameter-list) {
type method-name-2(parameter-list) {
…
type method-name-m(parameter-list) { …
}
© Prognoz Technologies Pvt. Ltd
Example: Class
A class with three variable members:
class Box {
double width;
double height;
double depth;
}
A new Box object is created and a new value assigned to its width variable:
Box myBox = new Box();
myBox.width = 100;
© Prognoz Technologies Pvt. Ltd
Example: Class Usage
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 * m
System.out.println("Volume is " +
}
}
© Prognoz Technologies Pvt. Ltd
Compilation and Execution
Place the Box class definitions in file Box.java:
class Box { … }
Place the BoxDemo class definitions in file BoxDemo.java:
class BoxDemo {
public static void main(…) { … }
}
Compilation and execution:
> javac BoxDemo.java
> java BoxDemo
© Prognoz Technologies Pvt. Ltd
Variable Independence
Each object has its own copy of the instance variables: changing the
variables of one object has no effect on the variables of another object.
© Prognoz Technologies Pvt. Ltd
OBJECTS
class Book
{
}
class JavaBook
{
public ststic void main(String args[])
{
Book b=new Book();
}
}
© Prognoz Technologies Pvt. Ltd
Declaring Objects
Obtaining objects of a class is a two-stage process:
1) Declare a variable of the class type:
Box myBox;
The value of myBox is a reference to an object, if one exists, or null.
At this moment, the value of myBox is null.
2) Acquire an actual, physical copy of an object and assign its address to
the variable. How to do this?
© Prognoz Technologies Pvt. Ltd
Operator new
Allocates memory for a Box object and returns its address:
Box myBox = new Box();
The address is then stored in the myBox reference variable.
Box() is a class constructor - a class may declare its own constructor or
rely on the default constructor provided by the Java environment.
Syntax:
accessing data member of the class: objectname.datamember name;
accessing methods of the class: objectname.method name();
So for accessing data of the class: we have to use (.) dot operator.
© Prognoz Technologies Pvt. Ltd
Memory Allocation
Memory is allocated for objects dynamically.
This has both advantages and disadvantages:
1) as many objects are created as needed
2) allocation is uncertain – memory may be insufficient
Variables of simple types do not require new:
int n = 1;
In the interest of efficiency, Java does not implement simple types as
objects. Variables of simple types hold values, not references.
© Prognoz Technologies Pvt. Ltd
Assigning Reference Variables
Assignment copies address, not the actual value:
Box b1 = new Box();
Box b2 = b1;
Both variables point to the same object.
Variables are not in any way connected. After
b1 = null;
b2 still refers to the original object.
© Prognoz Technologies Pvt. Ltd
Thank you!!
20

Basic concept of Object Oriented Programming

  • 1.
    © Prognoz TechnologiesPvt. Ltd Object & Class
  • 2.
    © Prognoz TechnologiesPvt. Ltd What is Object ?  Objects are key to understanding object-oriented technology.  Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle. 2
  • 3.
    © Prognoz TechnologiesPvt. Ltd Continue…  Real-world objects share two characteristics: They all have state and behavior.  Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).
  • 4.
    © Prognoz TechnologiesPvt. Ltd Continue… Bicycles also have state(current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). 4
  • 5.
    © Prognoz TechnologiesPvt. Ltd What is Class ?  In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model.  Each bicycle was built from the same set of blueprints and therefore contains the same components.  In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.  A class is the blueprint from which individual objects are created.
  • 6.
    © Prognoz TechnologiesPvt. Ltd Continue…
  • 7.
    © Prognoz TechnologiesPvt. Ltd Continue… OBJECTS CLASSES
  • 8.
    © Prognoz TechnologiesPvt. Ltd A Class acts as the template from which an instance of an object is created. The class defines the properties of the object and the methods used to control the object's behavior. A Class specifies the structure of data as well as the methods which manipulate that data. Such data and methods are contained in each instance of the class. A Class is a model or template that can be instantiated to create objects with a common definition, and therefore common properties, operations and behavior. A Class provides a template for defining the behavior of a particular type of object. Objects are referred to as “instances” of a class.
  • 9.
    © Prognoz TechnologiesPvt. Ltd CLASSES class One { } class Book { } class College { } class AnyThing { }
  • 10.
    © Prognoz TechnologiesPvt. Ltd Class Definition A class contains a name, several variable declarations (instance variables) and several method declarations. All are called members of the class. General form of a class: class classname { type instance-variable-1; … type instance-variable-n; type method-name-1(parameter-list) { type method-name-2(parameter-list) { … type method-name-m(parameter-list) { … }
  • 11.
    © Prognoz TechnologiesPvt. Ltd Example: Class A class with three variable members: class Box { double width; double height; double depth; } A new Box object is created and a new value assigned to its width variable: Box myBox = new Box(); myBox.width = 100;
  • 12.
    © Prognoz TechnologiesPvt. Ltd Example: Class Usage 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 * m System.out.println("Volume is " + } }
  • 13.
    © Prognoz TechnologiesPvt. Ltd Compilation and Execution Place the Box class definitions in file Box.java: class Box { … } Place the BoxDemo class definitions in file BoxDemo.java: class BoxDemo { public static void main(…) { … } } Compilation and execution: > javac BoxDemo.java > java BoxDemo
  • 14.
    © Prognoz TechnologiesPvt. Ltd Variable Independence Each object has its own copy of the instance variables: changing the variables of one object has no effect on the variables of another object.
  • 15.
    © Prognoz TechnologiesPvt. Ltd OBJECTS class Book { } class JavaBook { public ststic void main(String args[]) { Book b=new Book(); } }
  • 16.
    © Prognoz TechnologiesPvt. Ltd Declaring Objects Obtaining objects of a class is a two-stage process: 1) Declare a variable of the class type: Box myBox; The value of myBox is a reference to an object, if one exists, or null. At this moment, the value of myBox is null. 2) Acquire an actual, physical copy of an object and assign its address to the variable. How to do this?
  • 17.
    © Prognoz TechnologiesPvt. Ltd Operator new Allocates memory for a Box object and returns its address: Box myBox = new Box(); The address is then stored in the myBox reference variable. Box() is a class constructor - a class may declare its own constructor or rely on the default constructor provided by the Java environment. Syntax: accessing data member of the class: objectname.datamember name; accessing methods of the class: objectname.method name(); So for accessing data of the class: we have to use (.) dot operator.
  • 18.
    © Prognoz TechnologiesPvt. Ltd Memory Allocation Memory is allocated for objects dynamically. This has both advantages and disadvantages: 1) as many objects are created as needed 2) allocation is uncertain – memory may be insufficient Variables of simple types do not require new: int n = 1; In the interest of efficiency, Java does not implement simple types as objects. Variables of simple types hold values, not references.
  • 19.
    © Prognoz TechnologiesPvt. Ltd Assigning Reference Variables Assignment copies address, not the actual value: Box b1 = new Box(); Box b2 = b1; Both variables point to the same object. Variables are not in any way connected. After b1 = null; b2 still refers to the original object.
  • 20.
    © Prognoz TechnologiesPvt. Ltd Thank you!! 20