Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

RCC Institute of Information Technology

Canal South Road, Beliaghata


Kolkata - 700 015, West Bengal, India

Topic: a. Class and Object


b. this

NAME: Ronit Roy


UNIVERSITY ROLL NO.: 11700220096
CLASS ROLL NO.: CSE2021/B07
PAPER CODE: PCC-CS503
MODE OF CA1: ppt presentation
a Classes and Objects

Class in Java
A class is a blueprint for the object. Before we create an object, we first need to define the class.
We can think of the class as a sketch (prototype) of a house. It contains all the details about the
floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.
Since many houses can be made from the same description, we can create many objects from a
class.

Creating a class in Java


Here, fields (variables) and methods represent the state and behavior of the
object respectively.
fields are used to store data
methods are used to perform some operations

For our bicycle object, we can create the class as (screenshot attatched)
In the example, I have created a class named Bicycle. It contains a field
named gear and a method named braking().
Here, Bicycle is a prototype. Now, we can create any number of bicycles using
the prototype. And, all the bicycles will share the fields and methods of the
prototype.
a Classes and Objects

Object in Java
An entity that has state and behavior is known as an object e.g., apple, plant, monitor, etc.
It can be physical or logical (tangible and intangible). An object is called an instance of a Bicycle
class. For example, suppose Bicycle is a class then MountainBicycle, SportsBicycle, (class)
TouringBicycle, etc can be considered as objects of the class

Creating an Object in Java


SportsBicycle
I've used the new keyword along with the constructor of the (object)
class to create an object. Constructors are similar to MountainBicycle
(object)
methods and have the same name as the class. For
example, Bicycle() is the constructor of the Bicycle class.

Here, sportsBicycle and touringBicycle are the names of


objects. We can use them to access fields and methods of
the class.
As you can see, we have created two objects of the class.
We can create multiple objects of a single class in Java.
b this

this in Java
The 'this' keyword refers to the current object in a method or constructor. For example,

OUTPUT

In the above example, I created an object named obj of the class Main. I then print the reference to the object obj and this
keyword of the class.
Here, we can see that the reference of both obj and this is the same. It means this is nothing but the reference to the
current object.
b this

Usage
There can be a lot of usage of Java this keyword. In Java, this is a reference variable that
refers to the current object.
The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same
name (because a class attribute is shadowed by a method or constructor parameter). If you omit the keyword in the example
above, the output would be "0" instead of "5".
this can also be used to:
Invoke current class constructor
Invoke current class method
Return the current class object
Pass an argument in the method call
Pass an argument in the constructor call

Using this for Ambiguity Variable Names


In Java, it is not allowed to declare two or more variables having the same name inside a scope
(class scope or method scope). However, instance variables and parameters may have the same
name.
b this

For example,
In the above program, the instance variable and the parameter have the same name: age.
Here, the Java compiler is confused due to name ambiguity. In such a situation, we use this
keyword. For example,

First, let's see an example without using this keyword: Now, let's rewrite the above code using 'this' keyword.

OUTPUT OUTPUT

Now, we are getting the expected output. It is because when the constructor is called, this inside the constructor is replaced
by the object obj that has called the constructor. Hence the age variable is assigned value 8.
The End

Thank you

You might also like