jpr3
jpr3
<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
behavior of Explanation of Concept: Key Definitions/
objects. Formulas
Defining a class:
Syantax: Class is a user defined data type. Once a new data type is defined using a
class, it can be used to create objects of that type. A class is a template for
class classname [ an object and object is an instance of a class. Classes create objects and A class is a template or
extends objects use methods to communicate between them. blueprint for objects.
superclassname]
{ Syntax:
[ fields class classname [ extends superclassname] A class may contain
declaration; ] { methods that describe
[ fields declaration; ] the behavior of objects.
[ methods [ methods declaration; ]
declaration; ] }
}
Object:
It is a basic unit of Object-Oriented Programming and represents the real life
entities. A typical Java program creates many objects, which as you know,
interact by invoking methods.
Define Object. An object consists of:
Ans: It is a basic State: It is represented by attributes of an object. It also reflects the
unit of Object- properties of an object.
Oriented Behavior: It is represented by methods of an object. It also reflects the
Programming and response of an object with other objects.
represents the real Identity: It gives a unique name to an object and enables one object to
life entities. 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