#Java_Interview_Question
62) What is interface?
Interface is a blueprint of a class that have static constants and abstract methods.It can be used to achieve fully abstraction and multiple inheritance.
@javaCode☕️
62) What is interface?
Interface is a blueprint of a class that have static constants and abstract methods.It can be used to achieve fully abstraction and multiple inheritance.
@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 62) What is interface? Interface is a blueprint of a class that have static constants and abstract methods.It can be used to achieve fully abstraction and multiple inheritance. @javaCode☕️
More Details:
👉Interface in Java
An interface in java is a blueprint of a class. It has static constants and abstract methods.
The interface in java is a mechanism to achieve abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve abstraction and multiple inheritance in Java.
Java Interface also represents IS-A relationship.
It cannot be instantiated just like abstract class.
👉Why use Java interface?
There are mainly three reasons to use interface. They are given below.
▪️It is used to achieve abstraction.
▪️By interface, we can support the functionality of multiple inheritance.
▪️It can be used to achieve loose coupling.
@javaCode☕️
👉Interface in Java
An interface in java is a blueprint of a class. It has static constants and abstract methods.
The interface in java is a mechanism to achieve abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve abstraction and multiple inheritance in Java.
Java Interface also represents IS-A relationship.
It cannot be instantiated just like abstract class.
👉Why use Java interface?
There are mainly three reasons to use interface. They are given below.
▪️It is used to achieve abstraction.
▪️By interface, we can support the functionality of multiple inheritance.
▪️It can be used to achieve loose coupling.
@javaCode☕️
#Java_Interview_Question
63) Can you declare an interface method static?
No, because methods of an interface is abstract by default, and static and abstract keywords can't be used together.
@javaCode☕️
63) Can you declare an interface method static?
No, because methods of an interface is abstract by default, and static and abstract keywords can't be used together.
@javaCode☕️
#Java_Interview_Question
64) Can an Interface be final?
No, because its implementation is provided by another class.
@javaCode☕️
64) Can an Interface be final?
No, because its implementation is provided by another class.
@javaCode☕️
#Design_Patterns
#Object_Pool_Design_Pattern
👉Structure
The general idea for the Connection Pool pattern is that if instances of a class can be reused, you avoid creating instances of the class by reusing them.
▪️Reusable - Instances of classes in this role collaborate with other objects for a limited amount of time, then they are no longer needed for that collaboration.
▪️Client - Instances of classes in this role use Reusable objects.
▪️ReusablePool - Instances of classes in this role manage Reusable objects for use by Client objects.
Usually, it is desirable to keep all Reusable objects that are not currently in use in the same object pool so that they can be managed by one coherent policy. To achieve this, the ReusablePool class is designed to be a singleton class. Its constructor(s) are private, which forces other classes to call its getInstance method to get the one instance of the ReusablePool class.
A Client object calls a ReusablePool object's acquireReusable method when it needs a Reusable object. A ReusablePool object maintains a collection of Reusable objects. It uses the collection of Reusable objects to contain a pool of Reusable objects that are not currently in use.
If there are any Reusable objects in the pool when the acquireReusable method is called, it removes a Reusable object from the pool and returns it. If the pool is empty, then the acquireReusable method creates a Reusable object if it can. If the acquireReusable method cannot create a new Reusable object, then it waits until a Reusable object is returned to the collection.
Client objects pass a Reusable object to a ReusablePool object's releaseReusable method when they are finished with the object. The releaseReusable method returns a Reusable object to the pool of Reusable objects that are not in use.
In many applications of the Object Pool pattern, there are reasons for limiting the total number of Reusable objects that may exist. In such cases, the ReusablePool object that creates Reusable objects is responsible for not creating more than a specified maximum number of Reusable objects. If ReusablePool objects are responsible for limiting the number of objects they will create, then the ReusablePool class will have a method for specifying the maximum number of objects to be created. That method is indicated in the above diagram as setMaxPoolSize.
@javaCode☕️
#Object_Pool_Design_Pattern
👉Structure
The general idea for the Connection Pool pattern is that if instances of a class can be reused, you avoid creating instances of the class by reusing them.
▪️Reusable - Instances of classes in this role collaborate with other objects for a limited amount of time, then they are no longer needed for that collaboration.
▪️Client - Instances of classes in this role use Reusable objects.
▪️ReusablePool - Instances of classes in this role manage Reusable objects for use by Client objects.
Usually, it is desirable to keep all Reusable objects that are not currently in use in the same object pool so that they can be managed by one coherent policy. To achieve this, the ReusablePool class is designed to be a singleton class. Its constructor(s) are private, which forces other classes to call its getInstance method to get the one instance of the ReusablePool class.
A Client object calls a ReusablePool object's acquireReusable method when it needs a Reusable object. A ReusablePool object maintains a collection of Reusable objects. It uses the collection of Reusable objects to contain a pool of Reusable objects that are not currently in use.
If there are any Reusable objects in the pool when the acquireReusable method is called, it removes a Reusable object from the pool and returns it. If the pool is empty, then the acquireReusable method creates a Reusable object if it can. If the acquireReusable method cannot create a new Reusable object, then it waits until a Reusable object is returned to the collection.
Client objects pass a Reusable object to a ReusablePool object's releaseReusable method when they are finished with the object. The releaseReusable method returns a Reusable object to the pool of Reusable objects that are not in use.
In many applications of the Object Pool pattern, there are reasons for limiting the total number of Reusable objects that may exist. In such cases, the ReusablePool object that creates Reusable objects is responsible for not creating more than a specified maximum number of Reusable objects. If ReusablePool objects are responsible for limiting the number of objects they will create, then the ReusablePool class will have a method for specifying the maximum number of objects to be created. That method is indicated in the above diagram as setMaxPoolSize.
@javaCode☕️
#Java_Interview_Question
65) What is marker interface?
An interface that have no data member and method is known as a marker interface.For example Serializable, Cloneable etc.
@javaCode☕️
65) What is marker interface?
An interface that have no data member and method is known as a marker interface.For example Serializable, Cloneable etc.
@javaCode☕️
#Java_Interview_Question
66) What is difference between abstract class and interface?
1) An abstract class can have method body (non-abstract methods).
Interface have only abstract methods.
2) An abstract class can have instance variables.
An interface cannot have instance variables.
3) An abstract class can have constructor.
Interface cannot have constructor.
4) An abstract class can have static methods.
Interface cannot have static methods.
5) You can extends one abstract class.
You can implement multiple interfaces.
@javaCode☕️
66) What is difference between abstract class and interface?
1) An abstract class can have method body (non-abstract methods).
Interface have only abstract methods.
2) An abstract class can have instance variables.
An interface cannot have instance variables.
3) An abstract class can have constructor.
Interface cannot have constructor.
4) An abstract class can have static methods.
Interface cannot have static methods.
5) You can extends one abstract class.
You can implement multiple interfaces.
@javaCode☕️
#Java_Interview_Question
67) Can we define private and protected modifiers for variables in interfaces?
No, they are implicitly public.
@ javaCode☕️
67) Can we define private and protected modifiers for variables in interfaces?
No, they are implicitly public.
@ javaCode☕️
#Java_Interview_Question
68) When can an object reference be cast to an interface reference?
An object reference can be cast to an interface reference when the object implements the referenced interface.
@ javaCode☕️
68) When can an object reference be cast to an interface reference?
An object reference can be cast to an interface reference when the object implements the referenced interface.
@ javaCode☕️
☕️JAVA Language Community
#Object_Pool_Design_Pattern
#Design_Patterns
#Object_Pool_Design_Pattern
👉Example
Object pool pattern is similar to an office warehouse. When a new employee is hired, office manager has to prepare a work space for him. She figures whether or not there's a spare equipment in the office warehouse. If so, she uses it. If not, she places an order to purchase new equipment from Amazon. In case if an employee is fired, his equipment is moved to warehouse, where it could be taken when new work place will be needed.
@javaCode☕️
#Object_Pool_Design_Pattern
👉Example
Object pool pattern is similar to an office warehouse. When a new employee is hired, office manager has to prepare a work space for him. She figures whether or not there's a spare equipment in the office warehouse. If so, she uses it. If not, she places an order to purchase new equipment from Amazon. In case if an employee is fired, his equipment is moved to warehouse, where it could be taken when new work place will be needed.
@javaCode☕️
#Java_Interview_Question
69) What is package?
A package is a group of similar type of classes interfaces and sub-packages. It provides access protection and removes naming collision.
@javaCode☕️
69) What is package?
A package is a group of similar type of classes interfaces and sub-packages. It provides access protection and removes naming collision.
@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question
#Java_Interview_Question
👉Java Package
A java package is a group of similar types of classes, interfaces and sub-packages.
Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Here, we will have the detailed learning of creating and using user-defined packages.
👉Advantage of Java Package
1) Java package is used to categorize the classes and interfaces so that they can be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
👉How to access package from another package?
There are three ways to access the package from outside the package.
1️⃣import package.*;
2️⃣import package.classname;
3️⃣fully qualified name.
✅1) Using packagename.*
If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages.
The import keyword is used to make the classes and interface of another package accessible to the current package.
✅2) Using packagename.classname
If you import package.classname then only declared class of this package will be accessible.
✅3) Using fully qualified name
If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface.
It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class.
@javaCode☕️
👉Java Package
A java package is a group of similar types of classes, interfaces and sub-packages.
Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Here, we will have the detailed learning of creating and using user-defined packages.
👉Advantage of Java Package
1) Java package is used to categorize the classes and interfaces so that they can be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
👉How to access package from another package?
There are three ways to access the package from outside the package.
1️⃣import package.*;
2️⃣import package.classname;
3️⃣fully qualified name.
✅1) Using packagename.*
If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages.
The import keyword is used to make the classes and interface of another package accessible to the current package.
✅2) Using packagename.classname
If you import package.classname then only declared class of this package will be accessible.
✅3) Using fully qualified name
If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface.
It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class.
@javaCode☕️
#Java_Interview_Question
70) Do I need to import java.lang package any time? Why ?
No. It is by default loaded internally by the JVM.
@javaCode☕️
70) Do I need to import java.lang package any time? Why ?
No. It is by default loaded internally by the JVM.
@javaCode☕️
#Java_Interview_Question
71) Can I import same package/class twice? Will the JVM load the package twice at runtime?
One can import the same package or same class multiple times. Neither compiler nor JVM complains about it.But the JVM will internally load the class only once no matter how many times you import the same class.
@javaCode☕️
71) Can I import same package/class twice? Will the JVM load the package twice at runtime?
One can import the same package or same class multiple times. Neither compiler nor JVM complains about it.But the JVM will internally load the class only once no matter how many times you import the same class.
@javaCode☕️
#Design_Patterns
#Object_Pool_Design_Pattern
👉Check list
1️⃣Create ObjectPool class with private array of Objects inside
2️⃣Create acquire and release methods in ObjectPool class
3️⃣Make sure that your ObjectPool is Singleton
👉Rules of thumb
▪️The Factory Method pattern can be used to encapsulate the creation logic for objects. However, it does not manage them after their creation, the object pool pattern keeps track of the objects it creates.
▪️Object Pools are usually implemented as Singletons.
@javaCode☕️
#Object_Pool_Design_Pattern
👉Check list
1️⃣Create ObjectPool class with private array of Objects inside
2️⃣Create acquire and release methods in ObjectPool class
3️⃣Make sure that your ObjectPool is Singleton
👉Rules of thumb
▪️The Factory Method pattern can be used to encapsulate the creation logic for objects. However, it does not manage them after their creation, the object pool pattern keeps track of the objects it creates.
▪️Object Pools are usually implemented as Singletons.
@javaCode☕️
#Java_Interview_Question
72) What is static import ?
By static import, we can access the static members of a class directly, there is no to qualify it with the class name.
@javaCode☕️
72) What is static import ?
By static import, we can access the static members of a class directly, there is no to qualify it with the class name.
@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 72) What is static import ? By static import, we can access the static members of a class directly, there is no to qualify it with the class name. @javaCode☕️
More details:
👉Java Static Import
The static import feature of Java 5 facilitate the java programmer to access any static member of a class directly. There is no need to qualify it by the class name.
✅Advantage of static import:
Less coding is required if you have access any static member of a class oftenly.
❌Disadvantage of static import:
If you overuse the static import feature, it makes the program unreadable and unmaintainable.
@javaCode☕️
👉Java Static Import
The static import feature of Java 5 facilitate the java programmer to access any static member of a class directly. There is no need to qualify it by the class name.
✅Advantage of static import:
Less coding is required if you have access any static member of a class oftenly.
❌Disadvantage of static import:
If you overuse the static import feature, it makes the program unreadable and unmaintainable.
@javaCode☕️
☕️JAVA Language Community
More details: 👉Java Static Import The static import feature of Java 5 facilitate the java programmer to access any static member of a class directly. There is no need to qualify it by the class name. ✅Advantage of static import: Less coding is required…
#Java_Interview_Question
👉What is the difference between import and static import?
The import allows the java programmer to access classes of a package without package qualification whereas the static import feature allows to access the static members of a class without the class qualification. The import provides accessibility to classes and interface whereas static import provides accessibility to static members of the class.
@javaCode☕️
👉What is the difference between import and static import?
The import allows the java programmer to access classes of a package without package qualification whereas the static import feature allows to access the static members of a class without the class qualification. The import provides accessibility to classes and interface whereas static import provides accessibility to static members of the class.
@javaCode☕️