☕️JAVA Language Community
2.91K subscribers
144 photos
7 videos
31 files
42 links
☕️ Software, IT, Java, news
💻 IT highlights
🎯 AI update
🖥⌨️🖱
Download Telegram
☕️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☕️
#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☕️
#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☕️
#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☕️
#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☕️
#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☕️
#Java_Interview_Question

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☕️
#Java_Interview_Question

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☕️
#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☕️
👌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☕️
#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☕️
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☕️
#Java_Interview_Question

👉Life cycle of a Thread (Thread States)

A thread can be in one of the five states. According to sun, there is only 4 states in thread life cycle in java new, runnable, non-runnable and terminated. There is no running state.

But for better understanding the threads, we are explaining it in the 5 states.

The life cycle of the thread in java is controlled by JVM. The java thread states are as follows:

1️⃣New

The thread is in new state if you create an instance of Thread class but before the invocation of start() method.

2️⃣Runnable

The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.

3️⃣Running

The thread is in running state if the thread scheduler has selected it.

4️⃣Non-Runnable (Blocked)

This is the state when the thread is still alive, but is currently not eligible to run.

5️⃣Terminated

A thread is in terminated or dead state when its run() method exits.

@javaCode☕️
👍1
#Design_Patterns
#Creational_patterns

👉Creational patterns

In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.


1️⃣ #Abstract_Factory_Design_Pattern

▪️Creates an instance of several families of classes

2️⃣ #Builder_Design_Pattern

▪️Separates object construction from its representation
3️⃣ #Factory_Method_Design_Pattern

▪️Creates an instance of several derived classes

4️⃣ #Object_Pool_Design_Pattern

▪️Avoid expensive acquisition and release of resources by recycling objects that are no longer in use

5️⃣ #Prototype_Design_Pattern

▪️A fully initialized instance to be copied or cloned

6️⃣ #Singleton_Design_Pattern

▪️A class of which only a single instance can exist

@javaCode☕️
☕️JAVA Language Community
#Design_Patterns #Creational_patterns 👉Creational patterns In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form…
👉Rules of thumb

1️⃣Sometimes creational patterns are competitors: there are cases when either Prototype or Abstract Factory could be used profitably. At other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone and return product objects, Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementation.

2️⃣Abstract Factory, Builder, and Prototype define a factory object that's responsible for knowing and creating the class of product objects, and make it a parameter of the system. Abstract Factory has the factory object producing objects of several classes. Builder has the factory object building a complex product incrementally using a correspondingly complex protocol. Prototype has the factory object (aka prototype) building a product by copying a prototype object.

@javaCode☕️
#Java_Interview_Question

👉How to create thread

There are two ways to create a thread:

1️⃣By extending Thread class:

Thread class provide constructors and methods to create and perform operations on a thread.Thread class extends Object class and implements Runnable interface.

2️⃣By implementing Runnable interface:

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run().
public void run(): is used to perform action for a thread.


@javaCode☕️
#Java_Interview_Question

👉Thread Scheduler in Java

▪️Thread scheduler in java is the part of the JVM that decides which thread should run.

▪️There is no guarantee that which runnable thread will be chosen to run by the thread scheduler.

▪️Only one thread at a time can run in a single process.

▪️The thread scheduler mainly uses preemptive or time slicing scheduling to schedule the threads.

-----------------------------
👉Difference between preemptive scheduling and time slicing

▪️Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence.

▪️Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

@javaCode☕️