PSOOP-Lecture 02-24-25
PSOOP-Lecture 02-24-25
Classes
Objects
Constructors
Scope of Variables
2
CONCEPT OF CLASS
A class is a description/blueprint/template of a group of objects
with common properties (attributes) & behavior (operations)
An object is a real world entity which is an instance of a class
e.g. Mary is an object of Student class
Jane is an object of Student class
The main method may or may not be present depending on whether the class is a starter class
note
ACCESS MODIFIERS – PRIVATE & PUBLIC
Four Access Modifiers: Default is
private NOT a
protected keyword in
public Java
default
The methods which expose the behavior of the object are kept public
However, we can have helper methods which are private
note
CONSTRUCTORS
Special methods used to initialize a newly created object
A constructor
Has the same name as that of the class
obj1
→ Objects :2 obj3
2
Heap
obj2
LIFETIME OF OBJECTS (CONTD…)
obj1
obj3 = obj1;
→ References : 3
1
obj3
→ Objects :2
2
Heap
obj2
obj1
obj2 = null;
→ Active References : 2 1
→ Abandoned objects : 1
obj2
note
SCOPE OF VARIABLES
Instance Variables (also called Member Variables)
Declared inside a class
Outside any method or constructor
Belong to the object
Stored in heap area with the object to which they belong
to
Lifetime depends on the lifetime of object
class Echo {
public static void main (String args[]) {
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
Try this: Invoke the Echo application as
follows
C:\> java Echo Drink Hot Java
Drink
Hot
Java
note
THANK YOU