Object Oriented Programming (OOP) Concepts - 2

oops
Hi Readers, 

We'll continue with OOPs concepts. Feel free to raise doubts/queries in comments section. 

Read IT Officers Study Material here

What's a singleton Class?

A class which can have only one object (a single instance) at one time. It is used oftenly to control access to resources like database connections or sockets. 

What is double check locking in singleton?

It's a technique with which we prevent the creation of another instance after calling getInstance ( ) method. This is also called Lazy Loading because instance is created only when the client calls getInstance ( ). 

However, there is another way to implement singleton pattern in which instance is created the moment class is loaded into memory. That is called Early Loading. 

Some of the common ways to create singleton pattern in java are as follows:
  • getInstance ( ) method
  • Initialize public final static field while loading the class
  • With static nested class (singleton holder pattern)
  • Using Enums. 
What is the difference between static class and singleton pattern?

The single instance created with singleton can be passed as parameters to other methods i.e we can implement interfaces with it but not with static class as it holds only static methods. 

What is friend( ) function & friend( ) class?

Friend ( ) function allows access to private and protected data of a class. Declaration of a friend ( ) function can be made either in private or public section of a class with the keyword "friend".

Similarly, a class can be declared as a friend of another class with 'friend' keyword. Suppose we declare class X as friend in class Y, all the member functions of class X can access private and protected data of class Y but not vice versa. 

What is constructor & destructor ?

Constructors - A constructor method is called every time an instance of a class is created. It has the same name as that of class and doesn't return any type. . They are used for setting initial values for variables as well. 

Destructors - They are inverse of constructor functions and are called when the purpose of an object ends i.e memory allocated to that object has to be vacated. It can not take any arguments and has no return type. They also have the same name as that of a class, preceded by a 'tilde'.

Join 40,000+ readers and get free notes in your email

0 comments:

Post a Comment

Thanks for commenting. It's very difficult to answer every query here, it's better to post your query on IBPSToday.com