0% found this document useful (0 votes)
1 views17 pages

Lec 10

Uploaded by

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

Lec 10

Uploaded by

Ahmed Mehmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Object Oriented Programming

Lecture 10

1
Topics to be covered today
• Finalize() method
• Garbage Collection
• this keyword
• Object Argument
• Argument passing

2
Finalize() method
• A constructor helps to initialize an object
just after it has been created.
• In contrast, the finalize method is invoked
just before the object is destroyed:
– implemented inside a class as:
protected void finalize() { … }

3
Contd.
• How is the finalize method invoked?
– Garbage Collection to perform clean-up activity.
Clean-up activity means closing the resources
associated with that object like Database
Connection, Network Connection, or we can say
resource de-allocation.
Garbage Collection
Garbage collection is a mechanism to remove objects
from memory when they are no longer needed.
Garbage collection is carried out by the garbage
collector:
◦ The garbage collector keeps track of how many references
an object has.
◦ It removes an object from memory when it has no longer
any references.
◦ Thereafter, the memory occupied by the object can be
allocated again.
◦ The garbage collector invokes the finalize method.

5
Keyword this
• Keyword this allows a method to refer to the
object that invoked it.
• It can be used inside any method to refer to
the current object:

• The above use of this is redundant but correct.


• When is this really needed?
6
Instance Variable Hiding
• Variables with the same names:
– it is illegal to declare two local variables with the same
name inside the same or enclosing scopes
– it is legal to declare local variables or parameters with
the same name as the instance variables of the class.
• As the same-named local variables/parameters will hide
the instance variables, using this is necessary to regain
access to them:

7
Object Argument
• So far, all method received arguments of
simple types.
• They may also receive an object as an
argument. Here is a method to check if a
parameter object is equal to the invoking
object:

8
Object Argument

ob1 == ob2: true


ob1 == ob3: false

9
Passing object to Constructor
• A special case of object-passing is passing
an object to the constructor.
• This is to initialize one object with another
object:

10
11
Argument Passing
• Two types of variables:
– simple types
– class types
• Two corresponding ways of how the
arguments are passed to methods:
– by value a method receives a copy of the
original value;parameters of simple types
– by reference a method receives the memory
address of the original value, not the value itself;
parameters of class types
12
Simple Type Argument Passing
• Passing arguments of simple types takes
place by value:

13
Simple Type Argument Passing
• With by-value argument-passing what
occurs to the parameter that receives the
argument has no effect outside the
method:

14
Contd.
Call by Reference
 Byreference a method receives the memory
address of the original value, not the value itself;
parameters of class types

16
Questions

17

You might also like