#Design_Patterns
#Singleton_Design_Pattern
👉Structure
Make the class of the single instance responsible for access and "initialization on first use". The single instance is a private static attribute. The accessor function is a public static method.
@javaCode☕️
#Singleton_Design_Pattern
👉Structure
Make the class of the single instance responsible for access and "initialization on first use". The single instance is a private static attribute. The accessor function is a public static method.
@javaCode☕️
#Java_Interview_Question
92) How can we create immutable class in java ?
There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc. In short, all the wrapper classes and String class is immutable. We can also create immutable class by creating final class that have final data members.
@javaCode☕️
92) How can we create immutable class in java ?
There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc. In short, all the wrapper classes and String class is immutable. We can also create immutable class by creating final class that have final data members.
@javaCode☕️
#Java_Interview_Question
93) What is the purpose of toString() method in java ?
The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.
@javaCode☕️
93) What is the purpose of toString() method in java ?
The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.
@javaCode☕️
☕️JAVA Language Community
#Singleton_Design_Pattern @javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern
👉Example
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, "The President of the United States" is a global point of access that identifies the person in the office.
@javaCode☕️
#Singleton_Design_Pattern
👉Example
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, "The President of the United States" is a global point of access that identifies the person in the office.
@javaCode☕️
#Java_Interview_Question
94)What is nested class?
A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class.
@javaCode☕️
94)What is nested class?
A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class.
@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 94)What is nested class? A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class. @javaCode☕️
More details:
👉Java Inner Class
Java inner class or nested class is a class i.e. declared inside the class or interface.
We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
Additionally, it can access all the members of outer class including private data members and methods.
❗️Syntax of Inner class
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
👉Advantage of java inner classes
There are basically three advantages of inner classes in java. They are as follows:
1) Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
2) Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
3) Code Optimization: It requires less code to write.
@javaCode☕️
👉Java Inner Class
Java inner class or nested class is a class i.e. declared inside the class or interface.
We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
Additionally, it can access all the members of outer class including private data members and methods.
❗️Syntax of Inner class
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
👉Advantage of java inner classes
There are basically three advantages of inner classes in java. They are as follows:
1) Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
2) Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
3) Code Optimization: It requires less code to write.
@javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern
👉Check list
1️⃣Define a private static attribute in the "single instance" class.
2️⃣Define a public static accessor function in the class.
3️⃣Do "lazy initialization" (creation on first use) in the accessor function.
4️⃣Define all constructors to be protected or private.
5️⃣Clients may only use the accessor function to manipulate the Singleton.
@javaCode☕️
#Singleton_Design_Pattern
👉Check list
1️⃣Define a private static attribute in the "single instance" class.
2️⃣Define a public static accessor function in the class.
3️⃣Do "lazy initialization" (creation on first use) in the accessor function.
4️⃣Define all constructors to be protected or private.
5️⃣Clients may only use the accessor function to manipulate the Singleton.
@javaCode☕️
#Java_Interview_Question
95) Is there any difference between nested classes and inner classes?
Yes, inner classes are non-static nested classes i.e. inner classes are the part of nested classes.
@javaCode☕️
95) Is there any difference between nested classes and inner classes?
Yes, inner classes are non-static nested classes i.e. inner classes are the part of nested classes.
@javaCode☕️
#Java_Interview_Question
96) Can we access the non-final local variable, inside the local inner class?
No, local variable must be constant if you want to access it in local inner class.
@javaCode☕️
96) Can we access the non-final local variable, inside the local inner class?
No, local variable must be constant if you want to access it in local inner class.
@javaCode☕️
#Java_Interview_Question
👉Types of Nested classes
There are two types of nested classes non-static and static nested classes.The non-static nested classes are also known as inner classes.
1️⃣Non-static nested class(inner class)
1)Member inner class
2)Annomynous inner class
3)Local inner class
2️⃣Static nested class
---------------------------------
👉Description:
1) Member Inner Class :
A class created within class and outside method.
2) Anonymous Inner Class :
A class created for implementing interface or extending class. Its name is decided by the java compiler.
3) Local Inner Class :
A class created within method.
4) Static Nested Class :
A static class created within class.
5) Nested Interface :
An interface created within class or interface.
@javaCode☕️
👉Types of Nested classes
There are two types of nested classes non-static and static nested classes.The non-static nested classes are also known as inner classes.
1️⃣Non-static nested class(inner class)
1)Member inner class
2)Annomynous inner class
3)Local inner class
2️⃣Static nested class
---------------------------------
👉Description:
1) Member Inner Class :
A class created within class and outside method.
2) Anonymous Inner Class :
A class created for implementing interface or extending class. Its name is decided by the java compiler.
3) Local Inner Class :
A class created within method.
4) Static Nested Class :
A static class created within class.
5) Nested Interface :
An interface created within class or interface.
@javaCode☕️
#Java_Interview_Question
97) What is nested interface ?
Any interface i.e. declared inside the interface or class, is known as nested interface. It is static by default.
@javaCode☕️
97) What is nested interface ?
Any interface i.e. declared inside the interface or class, is known as nested interface. It is static by default.
@javaCode☕️
#Java_Interview_Question
98) Can a class have an interface?
Yes, it is known as nested interface.
@javaCode☕️
98) Can a class have an interface?
Yes, it is known as nested interface.
@javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern
👉Rules of thumb
1️⃣Abstract Factory, Builder, and Prototype can use Singleton in their implementation.
2️⃣Facade objects are often Singletons because only one Facade object is required.
3️⃣State objects are often Singletons.
4️⃣The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, you can change your mind and manage any number of instances.
5️⃣The Singleton design pattern is one of the most inappropriately used patterns. Singletons are intended to be used when a class must have exactly one instance, no more, no less. Designers frequently use Singletons in a misguided attempt to replace global variables. A Singleton is, for intents and purposes, a global variable. The Singleton does not do away with the global; it merely renames it.
6️⃣When is Singleton unnecessary? Short answer: most of the time. Long answer: when it's simpler to pass an object resource as a reference to the objects that need it, rather than letting objects access the resource globally. The real problem with Singletons is that they give you such a good excuse not to think carefully about the appropriate visibility of an object. Finding the right balance of exposure and protection for an object is critical for maintaining flexibility.
7️⃣Our group had a bad habit of using global data, so I did a study group on Singleton. The next thing I know Singletons appeared everywhere and none of the problems related to global data went away. The answer to the global data question is not, "Make it a Singleton." The answer is, "Why in the hell are you using global data?" Changing the name doesn't change the problem. In fact, it may make it worse because it gives you the opportunity to say, "Well I'm not doing that, I'm doing this" – even though this and that are the same thing.
@javaCode☕️
#Singleton_Design_Pattern
👉Rules of thumb
1️⃣Abstract Factory, Builder, and Prototype can use Singleton in their implementation.
2️⃣Facade objects are often Singletons because only one Facade object is required.
3️⃣State objects are often Singletons.
4️⃣The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, you can change your mind and manage any number of instances.
5️⃣The Singleton design pattern is one of the most inappropriately used patterns. Singletons are intended to be used when a class must have exactly one instance, no more, no less. Designers frequently use Singletons in a misguided attempt to replace global variables. A Singleton is, for intents and purposes, a global variable. The Singleton does not do away with the global; it merely renames it.
6️⃣When is Singleton unnecessary? Short answer: most of the time. Long answer: when it's simpler to pass an object resource as a reference to the objects that need it, rather than letting objects access the resource globally. The real problem with Singletons is that they give you such a good excuse not to think carefully about the appropriate visibility of an object. Finding the right balance of exposure and protection for an object is critical for maintaining flexibility.
7️⃣Our group had a bad habit of using global data, so I did a study group on Singleton. The next thing I know Singletons appeared everywhere and none of the problems related to global data went away. The answer to the global data question is not, "Make it a Singleton." The answer is, "Why in the hell are you using global data?" Changing the name doesn't change the problem. In fact, it may make it worse because it gives you the opportunity to say, "Well I'm not doing that, I'm doing this" – even though this and that are the same thing.
@javaCode☕️
#Java_Interview_Question
99) Can an Interface have a class?
Yes, they are static implicitely.
@javaCode☕️
99) Can an Interface have a class?
Yes, they are static implicitely.
@javaCode☕️
#Java_Interview_Question
100) What is Thread in java?
A thread is a lightweight sub process, a smallest unit of processing. It is a separate path of execution.
Threads are independent, if there occurs exception in one thread, it doesn't affect other threads. It shares a common memory area.
@javaCode☕️
100) What is Thread in java?
A thread is a lightweight sub process, a smallest unit of processing. It is a separate path of execution.
Threads are independent, if there occurs exception in one thread, it doesn't affect other threads. It shares a common memory area.
@javaCode☕️
#Java_Interview_Question
More details:
👉Multithreading in Java
▪️Multithreading in java is a process of executing multiple threads simultaneously.
▪️Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.
▪️But we use multithreading than multiprocessing because threads share a common memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.
▪️Java Multithreading is mostly used in games, animation etc.
@javaCode☕️
More details:
👉Multithreading in Java
▪️Multithreading in java is a process of executing multiple threads simultaneously.
▪️Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.
▪️But we use multithreading than multiprocessing because threads share a common memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.
▪️Java Multithreading is mostly used in games, animation etc.
@javaCode☕️
👌1
#Java_Interview_Question
More details:
👉Advantages of Java Multithreading
1️⃣It doesn't block the user because threads are independent and you can perform multiple operations at same time.
2️⃣You can perform many operations together so it saves time.
3️⃣Threads are independent so it doesn't affect other threads if exception occur in a single thread.
@javaCode☕️
More details:
👉Advantages of Java Multithreading
1️⃣It doesn't block the user because threads are independent and you can perform multiple operations at same time.
2️⃣You can perform many operations together so it saves time.
3️⃣Threads are independent so it doesn't affect other threads if exception occur in a single thread.
@javaCode☕️
#Java_Interview_Question
👉Multitasking
Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved by two ways:
▪️Process-based Multitasking(Multiprocessing)
▪️Thread-based Multitasking(Multithreading)
1️⃣Process-based Multitasking (Multiprocessing)
Each process have its own address in memory i.e. each process allocates separate memory area.
Process is heavyweight.
Cost of communication between the process is high.
Switching from one process to another require some time for saving and loading registers, memory maps, updating lists etc.
2️⃣Thread-based Multitasking (Multithreading)
Threads share the same address space.
Thread is lightweight.
Cost of communication between the thread is low.
@javaCode☕️
👉Multitasking
Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved by two ways:
▪️Process-based Multitasking(Multiprocessing)
▪️Thread-based Multitasking(Multithreading)
1️⃣Process-based Multitasking (Multiprocessing)
Each process have its own address in memory i.e. each process allocates separate memory area.
Process is heavyweight.
Cost of communication between the process is high.
Switching from one process to another require some time for saving and loading registers, memory maps, updating lists etc.
2️⃣Thread-based Multitasking (Multithreading)
Threads share the same address space.
Thread is lightweight.
Cost of communication between the thread is low.
@javaCode☕️
Thread is executed inside the process. There is context-switching between the threads. There can be multiple processes inside the OS and one process can have multiple threads.
@javaCode☕️
@javaCode☕️