#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☕️
#Java_Interview_Question
👉 #Exception_Handling in Java
The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.
In this page, we will learn about java exception, its type and the difference between checked and unchecked exceptions.
👉What is exception
Dictionary Meaning: Exception is an abnormal condition.
In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.
👉What is exception handling
Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc.
👉Advantage of Exception Handling
The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling.
👉Let's take a scenario:
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
Suppose there is 10 statements in your program and there occurs an exception at statement 5, rest of the code will not be executed i.e. statement 6 to 10 will not run. If we perform exception handling, rest of the statement will be executed. That is why we use exception handling in java.
@javaCode☕️
👉 #Exception_Handling in Java
The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.
In this page, we will learn about java exception, its type and the difference between checked and unchecked exceptions.
👉What is exception
Dictionary Meaning: Exception is an abnormal condition.
In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.
👉What is exception handling
Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc.
👉Advantage of Exception Handling
The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling.
👉Let's take a scenario:
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
Suppose there is 10 statements in your program and there occurs an exception at statement 5, rest of the code will not be executed i.e. statement 6 to 10 will not run. If we perform exception handling, rest of the statement will be executed. That is why we use exception handling in java.
@javaCode☕️
👍1
☕️JAVA Language Community
Hierarchy of Java Exception classes @javaCode☕️
#Java_Interview_Question
👉Types of Exception
There are mainly two types of exceptions:
checked and unchecked where error is considered as unchecked exception.
▪️The sun microsystem says there are three types of exceptions:
1️⃣Checked Exception
2️⃣Unchecked Exception
3️⃣Error
👉Difference between checked and unchecked exceptions
1) Checked Exception
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.
2) Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
@javaCode☕️
👉Types of Exception
There are mainly two types of exceptions:
checked and unchecked where error is considered as unchecked exception.
▪️The sun microsystem says there are three types of exceptions:
1️⃣Checked Exception
2️⃣Unchecked Exception
3️⃣Error
👉Difference between checked and unchecked exceptions
1) Checked Exception
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.
2) Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
@javaCode☕️
#Java_Interview_Question
👉Common scenarios where exceptions may occur
▪️There are given some scenarios where unchecked exceptions can occur. They are as follows:
1️⃣ArithmeticException
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0;
//ArithmeticException
2️⃣NullPointerException
If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.
String s=null;
System.out.println(s.length());
//NullPointerException
3️⃣NumberFormatException
The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.
String s="abc";
int i=Integer.parseInt(s);
//NumberFormatException
4️⃣ArrayIndexOutOfBoundsException
If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[5];
a[10]=50;
//ArrayIndexOutOfBoundsException
@javaCode☕️
👉Common scenarios where exceptions may occur
▪️There are given some scenarios where unchecked exceptions can occur. They are as follows:
1️⃣ArithmeticException
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0;
//ArithmeticException
2️⃣NullPointerException
If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.
String s=null;
System.out.println(s.length());
//NullPointerException
3️⃣NumberFormatException
The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.
String s="abc";
int i=Integer.parseInt(s);
//NumberFormatException
4️⃣ArrayIndexOutOfBoundsException
If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[5];
a[10]=50;
//ArrayIndexOutOfBoundsException
@javaCode☕️