0% found this document useful (0 votes)
12 views15 pages

Lecture 4A Classes and Objects

The document provides an overview of classes and objects in programming, explaining that a class serves as a blueprint for creating objects, which are instances of that class. It details the components of a class, including variables, methods, and constructors, as well as access modifiers that determine visibility. Additionally, it outlines the process of obtaining objects and accessing their properties using the dot operator.

Uploaded by

totiya3351
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

Lecture 4A Classes and Objects

The document provides an overview of classes and objects in programming, explaining that a class serves as a blueprint for creating objects, which are instances of that class. It details the components of a class, including variables, methods, and constructors, as well as access modifiers that determine visibility. Additionally, it outlines the process of obtaining objects and accessing their properties using the dot operator.

Uploaded by

totiya3351
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Classes & Objects

22-01-25

Lecture 4A
Prof. Anita Agrawal
BITS-Pilani, K.K. Birla Goa campus
❑A class is a blueprint from which objects are created

❑It is an entity that determines how an object will behave


and what the object will contain.

❑It defines the methods and variables common to all objects


of a certain kind.

❑Syntax:
class classname
{
members of class
}

Classes 2
Anita Agrawal 1/22/2025
• Members of class are:
• Variables
• Methods
• Constructors
• Variables: represent the state of the class.
• Methods: represent behavior of the class.
• Constructors: initialize the state of a new instance of the
class

Class members 3
Anita Agrawal 1/22/2025
modifier class clsName
{
// instance variable declaration
modifier type1 varName1 = value1;
: Variables
modifier typeN varNameN = valueN;
// Constructor
clsName(cparam1)
{ Constructor
// body of constructor
}

Class
// Methods
modifier rType1 methodName1(mParams1) members
{
// body of method
}
: Methods
modifier rTypeN methodNameN(mParamsN)
{
// body of method
}
}

4
Anita Agrawal 1/22/2025
Package Private(accessible
only inside the package)
• Class access level: Public (accessible in code of any
package)

Private (only in the class)


• Access Level of members:
Package (only in this package)
(fields and methods)
Protected (in this package and all
sub-classes even if in different
pacakges
Public(everywhere)

Modifier/ access level 5


Anita Agrawal 1/22/2025
• A package is a namespace that organizes a
set of related classes and interfaces.

Packages 6
Anita Agrawal 1/22/2025
OBJECTS
7
Anita Agrawal 1/22/2025
• Obtaining objects: two step process.
• Declare a variable of the class type.
• does not define an object.
• simply refers to an object.
• Acquire an actual, physical copy of the object
and assign it to that variable.
• use the new operator.
• The new operator dynamically allocates
memory for an object and
• Returns a reference to it.

Objects 8
Anita Agrawal 1/22/2025
Step 1:
classname object_name
Step 2:
object_name = new classname()

Example:
Dog dog1; //dog1 contains null,
dog1 = new Dog(); // allocates an actual object

Syntax 9
Anita Agrawal 1/22/2025
Shorthand:
Dog dog1= new Dog();

10
Anita Agrawal 1/22/2025
• Classes are logical constructs

• Objects are actual physical reality (occupy space in


memory)

11
Anita Agrawal 1/22/2025
Each object of class will have its own copy of instance
variable. breed, size, color, age

• To access these variables (and methods)


– Dot(.) operator
• For example:
[Link]=" german_shepherd ";
[Link]="Small";

12
Anita Agrawal 1/22/2025
METHODS
13
Anita Agrawal 1/22/2025
modifier class clsName
{
// instance variable declaration Data:
type1 varName1 = value1;
: instance
typeN varNameN = valueN; variables

// Methods
rType1 methodName1(mParams1)
{
// body of method
} code:
: methods
rTypeN methodNameN(mParamsN)
{
// body of method
}
}

14
Anita Agrawal 1/22/2025
int addition()
{
ctr++;
return i + j;
}

void display()
{
[Link](……………….);
}

Example of method 15
Anita Agrawal 1/22/2025

You might also like