You are on page 1of 16

Object

Oriented
Programming
LECTURE 04

5/12/2018 OBJECT ORIENTED PROGRAMMING 1


Visibility Modifiers

5/12/2018 OBJECT ORIENTED PROGRAMMING 2


Visibility Modifiers
✓ There are following visibility modifiers.
i. default
ii. public
iii. private
iv. protected
✓ For classes, we can use only default and public visibility modifiers. In other
words, for classes we cannot use private and protected visibility modifiers.
✓ For methods and data fields, we can use all visibility modifiers.
✓ We will discuss protected visibility modifiers later on in Inheritance topic

5/12/2018 OBJECT ORIENTED PROGRAMMING 3


Visibility Modifiers
✓ public visibility modifier for classes, methods, and data fields denote that they
can be accessed from any other classes in the same or any other package.
✓ default visibility modifier for classes, methods, and data fields denote that
they can be accessed from any other classes in the same package.
✓ The private modifier makes methods and data fields accessible only from
within its own class.

5/12/2018 OBJECT ORIENTED PROGRAMMING 4


5/12/2018 OBJECT ORIENTED PROGRAMMING 5
5/12/2018 OBJECT ORIENTED PROGRAMMING 6
Memory allocation for Object
✓ Function space is common for every object
✓ Whenever a new object is created:
◦ Memory is reserved for variables only
◦ Previously defined functions are used over and over again

5/12/2018 OBJECT ORIENTED PROGRAMMING 7


Differences between Variables of
Primitive Types and Reference Types

5/12/2018 OBJECT ORIENTED PROGRAMMING 8


Differences between Variables of
Primitive Types and Reference Types

5/12/2018 OBJECT ORIENTED PROGRAMMING 9


Differences between Variables of
Primitive Types and Reference Types

5/12/2018 OBJECT ORIENTED PROGRAMMING 10


Garbage and Garbage Collection
✓ As illustrated in Figure 9.9, after the assignment statement c1 = c2, c1 points
to the same object referenced by c2.
✓ The object previously referenced by c1 is no longer useful and therefore is
now known as garbage.
✓ Garbage occupies memory space, so the Java runtime system detects garbage
and automatically reclaims the space it occupies. This process is called
garbage collection.

5/12/2018 OBJECT ORIENTED PROGRAMMING 11


Accessing an Object’s Data and Methods
✓ An object’s member refers to its data fields and methods.
✓ After an object is created, its data can be accessed and its methods can be
invoked using the dot operator (.), also known as the object member access
operator:
✓ For example, myCircle.radius and myCircle.getArea()

5/12/2018 OBJECT ORIENTED PROGRAMMING 12


Static Data Member
✓ Normally each object of a class has its own copy of all the data members of
the class
✓ But if a data member of a class is static, then in this case, only one copy of a
variable should be shared by all objects of a class.
✓ Static variable is shared by all instances/objects and is not specific to any one
instance/object of the class.
✓ So static variable represents “class-wide” information i.e. Static variables are
class variables

5/12/2018 OBJECT ORIENTED PROGRAMMING 13


Static Data Member
Definition
“A variable that is part of a class, yet is not
part of an object of that class, is called
static data member”

5/12/2018 OBJECT ORIENTED PROGRAMMING 14


Static Data Member (Syntax)
✓Keyword static is used to make a data
member static

class ClassName{

static DataType VariableName;
};
5/12/2018 OBJECT ORIENTED PROGRAMMING 15
All done, time for Lunch!

5/12/2018 OBJECT ORIENTED PROGRAMMING 16

You might also like