0% found this document useful (0 votes)
16 views22 pages

Lecture 6

Uploaded by

sohamtyu123
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)
16 views22 pages

Lecture 6

Uploaded by

sohamtyu123
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/ 22

Object Oriented Programming

Lecture - 6

Prof. Dnyaneshwar Kanade

10/26/2024 Object Oriented Programming 1


Private Constructors
• Private constructors allow us to restrict the instantiation of a
class.
• They prevent the creation of class instances in any place other
than the class itself.
• Public and private constructors, used together, allow control
over how we wish to instantiate our classes – this is known as
constructor delegation.
Usage of Private Constructors
• There are several patterns and benefits to restricting explicit
class instantiation:
• The singleton pattern

• Delegating constructors

• Uninstantiable classes

• The builder pattern


What is a Singleton class?
• As the name implies, a class is said to be singleton if it limits
the number of objects of that class to one.
• We can’t have more than a single object for such classes.
• Singleton classes are employed extensively in concepts like
Networking and Database Connectivity.
• The constructor of singleton class would be private so there
must be another way to get the instance of that class.
• This problem is resolved using a class member instance and a
factory method to return the class member.
What is a Singleton class?

Example

• We changed value of a.x, value of b.x also got updated because


both ‘a’ and ‘b’ refer to same object, i.e., they are objects of a
singleton class.
Copy Constructor
• A copy constructor in a Java class is a constructor that creates
an object using another object of the same Java class.
• That's helpful when we want to copy a complex object that has
several fields, or when we want to make a deep copy of an
existing object.
• Like C++, Java also supports copy constructor. But, unlike C++,
Java doesn’t create a default copy constructor if you don’t
write your own.
Why copy constructor is required?
• Sometimes, we face a problem where we required to create an
exact copy of an existing object of the class.
• There is also a condition, if we have made any changes in the
copy it should not reflect in the original one and vice-versa.
• For such cases, Java provides the concept of a copy
constructor.
• In Java, a copy constructor is a special type of constructor that
creates an object using another object of the same Java class.
• It returns a duplicate copy of an existing object of the class.
Use of Copy Constructor
• We can use the copy constructor if we want to:
– Create a copy of an object that has multiple fields.

– Generate a deep copy of the heavy objects.

– Avoid the use of the Object.clone() method.


Advantages of Copy Constructor
• If a field declared as final, the copy constructor can change it.

• There is no need for typecasting.

• Its use is easier if an object has several fields.

• Addition of field to the class is easy because of it.

• We need to change only in the copy constructor.


How to Create a Copy
Constructor

Example
Garbage Collection
• Since objects are dynamically allocated by using
the new operator, you might be wondering how
such objects are destroyed and their memory
released for later reallocation.
• In some languages, such as traditional C++,
dynamically allocated objects must be manually
released by use of a delete operator.
• Java does it automatically.
• The technique that accomplishes this is called
Garbage Collection.
How Garbage Collection Works
• It works like this:
• when no references to an object exist, that object is assumed to be no
longer needed, and the memory occupied by the object can be
reclaimed.
• There is no need to explicitly destroy objects.
• Garbage collection only occurs sporadically (if at all) during the
execution of your program.
• It will not occur simply because one or more objects exist that are no
longer used.
• Furthermore, different Java run-time implementations will take
varying approaches to garbage collection, but for the most part, you
should not have to think about it while writing your programs.
Garbage Collection
• Since objects are dynamically allocated by using
the new operator, you might be wondering how
such objects are destroyed and their memory
released for later reallocation.
• In some languages, such as traditional C++,
dynamically allocated objects must be manually
released by use of a delete operator.
• Java does it automatically.
• The technique that accomplishes this is called
Garbage Collection.
Ways to make an object eligible for garbage collection in Java-

There are 4 different ways in which an object can be


made eligible for the purpose of garbage collection-
Ways to make an object eligible for garbage collection in Java-

1. By nullifying the reference variable-

• In this method, programmer assigns null to the reference


variables of all those objects which are no longer
required.

• This makes the useless objects automatically eligible for


the purpose of garbage collection.
Ways to make an object eligible for garbage collection in Java-

1. By nullifying the reference variable- Example

1. Student s1 = new Student( );


2. Student s2 = new Student( );
3. .
4. .
5. .
6. .
7. s1 = null;
8. .
9. .
10. .
11. .
12. s2 = null;
13. .
Ways to make an object eligible for garbage collection in Java-

2. By reassigning the reference variable-

As the name suggests,


In this method, programmer reassigns the reference variables of all those
objects which are no longer needed to some other objects.

This makes the useless objects automatically eligible for the purpose of
garbage collection.
Ways to make an object eligible for garbage collection in Java-

1. By reassigning the reference variable-Example


1. Student s1 = new Student( );
2. Student s2 = new Student( );
3. .
4. .
5. .
6. .
7. s1 = new Student( );
8. .
9. .
10. .
11. .
12. s2 = s1;
13. .
Ways to make an object eligible for garbage collection in Java-

• 3. By creating objects inside a method-


• As the name suggests,
• In this method, programmer creates the objects inside the
methods because after the method gets executed completely,
objects present inside the method body automatically
becomes eligible for the purpose of garbage collection.
• The reason behind it is that the reference variables are the
local variables of the method and after the method has
executed completely, all the local variables are automatically
destroyed.
Ways to make an object eligible for garbage collection in Java-

• 3. By creating objects inside a method- Example


1. class Test
2. {
3. public static void main(String[ ] args)
4. {
5. m1( );
6. }
7.
8. public static void m1( )
9. {
10. Student s1 = new Student( );
11. Student s2 = new Student( );
12. }
13.}
Ways to make an object eligible for garbage collection in Java-

• 4. Island of Isolation-
• Island of Isolation describes the situation where there exists
one or more objects in the program which are not accessible
from anywhere in the application as they contain no external
reference to them.
• It is not possible to access such objects through any means as
they have no external reference to them and thus they become
eligible for garbage collection.
• So, it appears as if the objects are stuck on some isolated island
and they can not be accessed through any means. Hence, the
name “Island of Isolation” is quite apt.
Ways to make an object eligible for garbage collection in Java-
• 4. Island of Isolation-Example
1. class Demo
2. {
3. Demo d;
4.
5. public static void main(string[ ] args)
6. {
7. Demo d1 = new Demo( );
8. Demo d2 = new Demo( );
9. Demo d3 = new Demo( );
10. .
11. .
12. d1.d = d2;
13. d2.d = d3;
14. d3.d = d1;
15. .
16. d1 = null;
17. d2 = null;
18. d3 = null;
19. }
20. }

You might also like