-
Notifications
You must be signed in to change notification settings - Fork 33
/
Problem_01.java
69 lines (61 loc) · 1.9 KB
/
Problem_01.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Cracking-The-Coding-Interview
* Problem_01.java
*/
package com.deepak.ctci.Ch13_Java;
/**
* <br> Problem Statement:
*
* In terms to Inheritance, what is the effect of keeping a constructor private?
*
* </br>
*
* @author Deepak
*/
public class Problem_01 {
/* In java, when a class is defined it gets a default public constructor.
* When we talk about inheritance, it allows us to use the properties and methods
* of the other class. The class which is using the properties and method is
* called a sub class whereas the class which actually owns those properties and methods
* is called a super class.
* For this particular problem, when a constructor is declared as private,
* it can be accessed by all those classes which can access the private methods
* of the that class. Moreover, a private constructor will not allow the class to
* be instantiated */
private static Problem_01 obj = null;
/**
* Private Constructor
*/
private Problem_01() {}
/**
* Method to make sure only one instance is created
* @return {@link Problem_01}
*/
public static Problem_01 objectCreationMethod(){
/* This logic will ensure that no more than one object can be created at a time */
if(obj == null){
obj = new Problem_01();
}
return obj;
}
/**
* Method to display a message
*/
public void display(){
System.out.println("Singleton class Example");
}
/**
* Main method to test the execution of program
* No unit tests needed
* @param args
*/
public static void main(String[] args) {
/* Creating singleton instance of the class because object cannot
* be created due to the existence of private constructor.
* You can instantiate the class here because this main method
* is inside the Problem_01 class and private methods can be
* accessed within the class. */
Problem_01 singeltonClass = Problem_01.objectCreationMethod();
singeltonClass.display();
}
}