0% found this document useful (0 votes)
3 views

jpr3

Uploaded by

nikitamahadik182
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

jpr3

Uploaded by

nikitamahadik182
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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
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; ] }
}

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

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