0% found this document useful (0 votes)
2 views16 pages

Java - Class & Objects

Java is an object-oriented programming language where all programs are structured around classes, which define the shape and nature of objects. A class serves as a template for creating objects, encapsulating related data (fields) and functions (methods). Objects are instantiated from classes, allowing multiple instances to exist independently with their own state.

Uploaded by

Vinod Agarkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

Java - Class & Objects

Java is an object-oriented programming language where all programs are structured around classes, which define the shape and nature of objects. A class serves as a template for creating objects, encapsulating related data (fields) and functions (methods). Objects are instantiated from classes, allowing multiple instances to exist independently with their own state.

Uploaded by

Vinod Agarkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Class

Introduction

– Java is true object-oriented language.

– Therefore underlying structure of all


Java programs is classes.

– Anything to represent in Java program


must be encapsulated in class.

– Class creates Objects.

– Objects use methods to communicate


between them.
Class
– Core of Java

– Logical construct upon which the entire


Java program is built.

– Defines the shape and nature of an


object.

– Any concept you wish to implement in a


Java program must be encapsulated
within a class.

– A class defines a new data type.


Class
– Once defined, can be used to create
objects of that type.
– Thus, a class is a template for an object,
and an object is an instance of a class.
– represents the set of properties or
methods that are common to all objects.
– group together logically related data
items and functions that work on them.
– the data items are called fields and the
functions are called methods.
Defining Class
– Basic form of Class declaration is:

class classname [ extends superclassname]


{
[ fields declaration; ]
[ methods declaration; ]
}

Here, classname and superclassname are


valid Java identifiers.
Everything inside [ ] is optional.
No semicolon after closing brace }
Defining Class
– Basic form of Class declaration is:

class classname [ extends superclassname]


{
[ fields declaration; ]
[ methods declaration; ]
}

The keyword extends is used for inheritance.


The properties of the superclassname class are
extended to classname class.
Defining Class
– Detailed Form of Class declaration:
class classname [ extends superclassname]
{
type instance-variable1;
type instance-variable2;
………….
type instance-variableN;

type methodname1 {body of method};


type methodname2 {body of method};
………….
type methodnameN {body of method};
}
Defining Class
Example :-

class Box
{
double width;
double height;
double depth;
}
Objects

– When a class is created, actually a new


data type is created.

– This new data type is used to declare


objects of that type.

– An object in Java is essentially a block of


memory that contains space to store all
the instance variables.
Creating Objects

– Creating an object is also referred to as


instantiating an object.

– However, creating objects of a class is a


two-step process.

1) Declaring Object

2) Creating Object
1) Declaring Objects
– 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.

ClassName ObjectName;
This declares a variable to hold the object
reference. It just creates a reference and it
does not point to an actual object.
1) Declaring Objects
Example :-

Box mybox;

declares mybox as a reference to an object of


type Box.
After the execution of above statement, 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.
2) Creating (Instantiating Objects)
– Objects can be created using following :

ObjectName = new ClassName( );


Objects in Java are created using the new
operator.
The new operator creates an object of the
specified class and returns a reference to that
object.
This allocates an actual object and assigns object
reference to the variable.
2) Creating (Instantiating Objects)
Example :-

Mybox = new Box( );

After the execution of the statement, mybox


acquire an actual, physical copy of the object
and assign it to the variable mybox.
After this, mybox can be used as object of Box
class.
Declaring and Creating Object in ONE step
Declaration and creation of objects can be
combined into one statement as follows:.

ClassName ObjectName = new ClassName( );

Example :-

Box mybox = new Box( );


Objects
 Any number of object can be created for a
class.
 Each object has its own copy of the
instance variables of its class.
 This means that any changes to the
variables of one object have no effect on
the variables of another.
Example :-
Box mybox1 = new Box( );
Box mybox2 = new Box( );
Box mybox3 = new Box( );

You might also like