Professional Documents
Culture Documents
<CO1>: <22412>: <Java Programming>: <Basic Syntactical Constructs in Java >: <LO1c>: <Study Material>
<Yogita Jore> <01/02/2021> <Vijay Patil>
Define class.
Ans:
Class is a user
defined data type.
A class is a
template or
blueprint for
objects.
A class may
contain methods
that describe the Explanation of Concept: Key Definitions/
behavior of Formulas
objects. Defining a class:
Class is a user defined data type. Once a new data type is defined using a
Syantax: class, it can be used to create objects of that type. A class is a template for
an object and object is an instance of a class. Classes create objects and A class is a template or
class classname [ objects use methods to communicate between them. blueprint for objects.
extends
superclassname] Syntax:
{ class classname [ extends superclassname]
[ fields A class may contain
{
declaration; ] methods that describe
[ fields declaration; ]
[ methods declaration; ] the behavior of objects.
[ methods }
declaration; ]
}
Object:
It is a basic unit of Object-Oriented Programming and represents the real life
Define Object. entities. A typical Java program creates many objects, which as you know,
Ans: It is a basic interact by invoking methods.
unit of Object-
Oriented An object consists of:
Programming and State: It is represented by attributes of an object. It also reflects the
represents the real properties of an object.
life entities. Behavior: It is represented by methods of an object. It also reflects the
response of an object with other objects.
Identity: It gives a unique name to an object and enables one object to
interact with other objects.
Creating Objects:
An object in java is essentially a block of memory that contains space to
store all the instance variables.
Steps for creating objects:
1. Declaring an object:
Also called instantiating a class.
When an object of a class is created, the class is said to be instantiated.
Declaring a variable to hold an object is just like declaring a variable to hold a
value of primitive type. Declaration does not create new objects. It is simply
variable that can refer to an object.
For example: Rectangle rect1;
2. Instantiating an object:
Creating an object is also referred to as instantiating an object. 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.
For example: Rectangle rect1 = new Rectangle ();
Assigns the object reference to the variable. The variable rect1 is now an
object of the rectangle class.
3. Initializing an object:
Classes provide constructor methods to initialize a new object of that type. A
class may provide multiple constructors to perform different kinds of
initialization on new objects.
For example:
Rectangle rect1=new Rectangle (10, 20);
Syntax:
objectname . variablename = value;
objectname . methodname (parameterlist);
Example:
rect1.length=10;
rect1.width=20;
//creating an object
Rectangle r1 = new Rectangle();
//calling the method using the object
r1.getdata(10,20);
Output:
Area1 = 150
Area2 = 240