You are on page 1of 4

Study Material

<CO1>: <22412>: <Java Programming>: <Basic Syntactical Constructs in Java >: <LO1c>: <Study Material>
<Yogita Jore> <01/02/2021> <Vijay Patil>

Key words Learning Objective: Diagram/ Picture


Class, Object, new Students should understand the concept of Classes and Objects.
keyword

Key Questions Concept Map

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; ]
}

Rules for defining classes:


 Everything inside the square brackets is optional.
 No semicolon after closing curly bracket.
 Classname and superclassname are any valid java identifiers.
 The keyword extends indicates that the properties of the
superclassname class are extended to the classname class. This
concept is known as inheritance.
 The data or variables defined within a class are called instance
variables.
 Collectively the methods and variables defined within class are
called members of the class.

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);

Accessing Class members:


Object containing its own set of variables; we would assign values to these
variables in order to use them in our program. All variables must be assigned
values before they are used. Since we are outside the class, we cannot
access the instance variables and the methods directly. We can access those
using the dot operators.

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);

Program to calculate area of rectangle using classes and objects:


class rectangle
{
int length, width; //declaration of variables
void getdata(int x, int y)
//definition of method
{ length = x;
width = y;
}
int rectarea() //definition of another method
{ int area = length * width;
return (area);
}
}
class Rectarea //class with main method
{ public static void main (String args[ ])
{ int area1, area2;
Rectangle rect1 = new Rectangle (); //creating objects
Rectangle rect2 = new Rectangle ();
rect1.length=15; //accessing variables
rect1.width=10;
area1=rect1.length * rect1.width;
rect2.getdata (20, 12); //accessing methods
area2=rect2.rectarea ();
System.out.println(“Area1= “ + area1);
System.out.println(“Area2= “ + area2);
}
}

Output:
Area1 = 150
Area2 = 240

Differentiate between Class and Object:


Class Object
A class is a piece of the program’s An object is called an instance of
source code that describes a a class.
particular type of objects. Object A program can create and use
Oriented programmers write class more than one object (instance)
definitions. of the same class.
Class is a piece of the program’s Object is an entity in a running
source code. program.
Classes written by a programmer. Object created when the
program is running (by the main
method or a constructor or
another method).
Class specifies the structure (the Object holds specific values of
number and types) of its objects’ attributes; these values can
attributes — the same for all of its change while the program is
objects. running.

Class specifies the possible Object behaves appropriately


behaviors of its objects. when called upon.

Application of Concept/ Examples in real life:

Key Take away from this LO:


Concept of object and class.

You might also like