#Java_Interview_Question
131)What is the difference between Serializalble and Externalizable interface?
Serializable is a marker interface but Externalizable is not a marker interface.When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject() two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class's serialization process.
@javaCode☕️
131)What is the difference between Serializalble and Externalizable interface?
Serializable is a marker interface but Externalizable is not a marker interface.When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject() two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class's serialization process.
@javaCode☕️
#Java_Interview_Question
132)How do I convert a numeric IP address like 192.18.97.39 into a hostname like java.sun.com?
By InetAddress.getByName("192.18.97.39").getHostName() where 192.18.97.39 is the IP address.
@javaCode☕️
132)How do I convert a numeric IP address like 192.18.97.39 into a hostname like java.sun.com?
By InetAddress.getByName("192.18.97.39").getHostName() where 192.18.97.39 is the IP address.
@javaCode☕️
#Java_Interview_Question
133) What is reflection?
Reflection is the process of examining or modifying the runtime behaviour of a class at runtime.It is used in:
IDE (Integreted Development Environment) e.g. Eclipse, MyEclipse, NetBeans.
Debugger
Test Tools etc.
@javaCode☕️
133) What is reflection?
Reflection is the process of examining or modifying the runtime behaviour of a class at runtime.It is used in:
IDE (Integreted Development Environment) e.g. Eclipse, MyEclipse, NetBeans.
Debugger
Test Tools etc.
@javaCode☕️
#Java_Interview_Question
134) Can you access the private method from outside the class?
Yes, by changing the runtime behaviour of a class if the class is not secured.
@javaCode☕️
134) Can you access the private method from outside the class?
Yes, by changing the runtime behaviour of a class if the class is not secured.
@javaCode☕️
#Java_Interview_Question
👉Java Networking Terminology
The widely used java networking terminologies are given below:
1️⃣IP Address
2️⃣Protocol
3️⃣Port Number
4️⃣MAC Address
5️⃣Connection-oriented and connection-less protocol
7️⃣Socket
@javaCode☕️
👉Java Networking Terminology
The widely used java networking terminologies are given below:
1️⃣IP Address
2️⃣Protocol
3️⃣Port Number
4️⃣MAC Address
5️⃣Connection-oriented and connection-less protocol
7️⃣Socket
@javaCode☕️
#Java_Interview_Question
135) What is the difference between ArrayList and Vector?
1️⃣ArrayList is not synchronized.
Vector is synchronized.
2️⃣ArrayList is not a legacy class.
Vector is a legacy class.
3️⃣ArrayList increases its size by 50% of the array size.
Vector increases its size by doubling the array size.
@javaCode☕️
135) What is the difference between ArrayList and Vector?
1️⃣ArrayList is not synchronized.
Vector is synchronized.
2️⃣ArrayList is not a legacy class.
Vector is a legacy class.
3️⃣ArrayList increases its size by 50% of the array size.
Vector increases its size by doubling the array size.
@javaCode☕️
#Java_Interview_Question
136) What is the difference between ArrayList and LinkedList?
1️⃣ArrayList uses a dynamic array. LinkedList uses doubly linked list.
2️⃣ArrayList is not efficient for manipulation because a lot of shifting is required. LinkedList is efficient for manipulation.
3️⃣ArrayList is better to store and fetch data. LinkedList is better to manipulate data.
@javaCode☕️
136) What is the difference between ArrayList and LinkedList?
1️⃣ArrayList uses a dynamic array. LinkedList uses doubly linked list.
2️⃣ArrayList is not efficient for manipulation because a lot of shifting is required. LinkedList is efficient for manipulation.
3️⃣ArrayList is better to store and fetch data. LinkedList is better to manipulate data.
@javaCode☕️
#Java_Interview_Question
137) What is the difference between Iterator and ListIterator?
Iterator traverses the elements in forward direction only whereas ListIterator traverses the elements in forward and backward direction.
1) Iterator traverses the elements in forward direction only. ListIterator traverses the elements in backward and forward directions both.
2) Iterator can be used in List, Set and Queue. ListIterator can be used in List only.
@javaCode☕️
137) What is the difference between Iterator and ListIterator?
Iterator traverses the elements in forward direction only whereas ListIterator traverses the elements in forward and backward direction.
1) Iterator traverses the elements in forward direction only. ListIterator traverses the elements in backward and forward directions both.
2) Iterator can be used in List, Set and Queue. ListIterator can be used in List only.
@javaCode☕️
👍1
#Java_Interview_Question
138) What is the difference between Iterator and Enumeration?
1) Iterator can traverse legacy and non-legacy elements.
Enumeration can traverse only legacy elements.
2) Iterator is fail-fast.
Enumeration is not fail-fast.
3) Iterator is slower than Enumeration.
Enumeration is faster than Iterator.
@javaCode☕️
138) What is the difference between Iterator and Enumeration?
1) Iterator can traverse legacy and non-legacy elements.
Enumeration can traverse only legacy elements.
2) Iterator is fail-fast.
Enumeration is not fail-fast.
3) Iterator is slower than Enumeration.
Enumeration is faster than Iterator.
@javaCode☕️
#Java_Interview_Question
139) What is the difference between List and Set?
List can contain duplicate elements whereas Set contains only unique elements.
@javaCode☕️
139) What is the difference between List and Set?
List can contain duplicate elements whereas Set contains only unique elements.
@javaCode☕️
👍1
#Java_Interview_Question
140) What is the difference between HashSet and TreeSet?
HashSet maintains no order whereas TreeSet maintains ascending order.
@javaCode☕️
140) What is the difference between HashSet and TreeSet?
HashSet maintains no order whereas TreeSet maintains ascending order.
@javaCode☕️
#Java_Interview_Question
141) What is the difference between Set and Map?
Set contains values only whereas Map contains key and values both.
@javaCode☕️
141) What is the difference between Set and Map?
Set contains values only whereas Map contains key and values both.
@javaCode☕️
👍2
#Java_Interview_Question
142) What is the difference between HashSet and HashMap?
HashSet contains only values whereas HashMap contains entry(key,value). HashSet can be iterated but HashMap need to convert into Set to be iterated.
@javaCode☕️
142) What is the difference between HashSet and HashMap?
HashSet contains only values whereas HashMap contains entry(key,value). HashSet can be iterated but HashMap need to convert into Set to be iterated.
@javaCode☕️
#Java_Interview_Question
143) What is the difference between HashMap and TreeMap?
HashMap maintains no order but TreeMap maintains ascending order.
@javaCode☕️
143) What is the difference between HashMap and TreeMap?
HashMap maintains no order but TreeMap maintains ascending order.
@javaCode☕️
#Java_Interview_Question
144) What is the difference between HashMap and Hashtable?
1) HashMap is not synchronized. Hashtable is synchronized.
2) HashMap can contain one null key and multiple null values.
@javaCode☕️
144) What is the difference between HashMap and Hashtable?
1) HashMap is not synchronized. Hashtable is synchronized.
2) HashMap can contain one null key and multiple null values.
@javaCode☕️
👍1
#Java_Interview_Question
145) What is the difference between Collection and Collections?
Collection is an interface whereas Collections is a class.
Collection interface provides normal functionality of data structure to List, Set and Queue.
But, Collections class is to sort and synchronize collection elements.
@javaCode☕️
145) What is the difference between Collection and Collections?
Collection is an interface whereas Collections is a class.
Collection interface provides normal functionality of data structure to List, Set and Queue.
But, Collections class is to sort and synchronize collection elements.
@javaCode☕️
#Java_Interview_Question
👉The rules for #overriding a method are as follows:
* The argument list must exactly match that of the overridden method. If they don't match, you can end up with an overloaded method you didn't intend.
* The return type must be the same as, or a subtype of, the return type declared in the original overridden method in the superclass.
* The access level can't be more restrictive than the overridden method's.
* The access level CAN be less restrictive than that of the overridden method.
* Instance methods can be overridden only if they are inherited by the subclass. A subclass within the same package as the instance's superclass can override any superclass method that is not marked private or final. A subclass in a different package can override only those non-final methods marked pub- lic or protected (since protected methods are inherited by the subclass).
* The overriding method CAN throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception.
* The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.
* The overriding method can throw narrower or fewer exceptions. Just because an overridden method "takes risks" doesn't mean that the overriding subclass' exception takes the same risks. Bottom line: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
* You cannot override a method marked final.
* You cannot override a method marked static.
* If a method can't be inherited, you cannot override it. Remember that overriding implies that you're reimplementing a method you inherited!
@javaCode☕️
👉The rules for #overriding a method are as follows:
* The argument list must exactly match that of the overridden method. If they don't match, you can end up with an overloaded method you didn't intend.
* The return type must be the same as, or a subtype of, the return type declared in the original overridden method in the superclass.
* The access level can't be more restrictive than the overridden method's.
* The access level CAN be less restrictive than that of the overridden method.
* Instance methods can be overridden only if they are inherited by the subclass. A subclass within the same package as the instance's superclass can override any superclass method that is not marked private or final. A subclass in a different package can override only those non-final methods marked pub- lic or protected (since protected methods are inherited by the subclass).
* The overriding method CAN throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception.
* The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.
* The overriding method can throw narrower or fewer exceptions. Just because an overridden method "takes risks" doesn't mean that the overriding subclass' exception takes the same risks. Bottom line: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
* You cannot override a method marked final.
* You cannot override a method marked static.
* If a method can't be inherited, you cannot override it. Remember that overriding implies that you're reimplementing a method you inherited!
@javaCode☕️
#Java_Interview_Question
👉The rules for #overloading a method are as follows:
■ Overloaded methods MUST change the argument list.
■ Overloaded methods CAN change the return type.
■ Overloaded methods CAN change the access modifier.
■ Overloaded methods CAN declare new or broader checked exceptions.
■ A method can be overloaded in the same class or in a subclass. In other words, if class A defines a doStuff(int i) method, the subclass B could define a doStuff(String s) method without overriding the superclass version that takes an int. So two methods with the same name but in different classes can still be considered overloaded, if the subclass inherits one version of the method and then declares another overloaded version in its class definition.
@javaCode☕️
👉The rules for #overloading a method are as follows:
■ Overloaded methods MUST change the argument list.
■ Overloaded methods CAN change the return type.
■ Overloaded methods CAN change the access modifier.
■ Overloaded methods CAN declare new or broader checked exceptions.
■ A method can be overloaded in the same class or in a subclass. In other words, if class A defines a doStuff(int i) method, the subclass B could define a doStuff(String s) method without overriding the superclass version that takes an int. So two methods with the same name but in different classes can still be considered overloaded, if the subclass inherits one version of the method and then declares another overloaded version in its class definition.
@javaCode☕️
👍2
#Java_Interview_Question
☑️NOTICE:
Which overridden version of the method to call (in other words, from which class in the inheritance tree) is decided at runtime based on object type, but which overloaded version of the method to call is based on the reference type of the argument passed at compile time.
@javaCode☕️
☑️NOTICE:
Which overridden version of the method to call (in other words, from which class in the inheritance tree) is decided at runtime based on object type, but which overloaded version of the method to call is based on the reference type of the argument passed at compile time.
@javaCode☕️
#Java_Interview_Question
👉Rules for #Constructors
■ Constructors can use any access modifier, including private. (A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.)
■ The constructor name must match the name of the class.
■ Constructors must not have a return type.
■ It's legal (but stupid) to have a method with the same name as the class, but that doesn't make it a constructor. If you see a return type, it's a method rather than a constructor. In fact, you could have both a method and a constructor with the same name—the name of the class—in the same class, and that's not a problem for Java. Be careful not to mistake a method for a constructor—be sure to look for a return type.
■ If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler.
■ The default constructor is ALWAYS a no-arg constructor.
■ If you want a no-arg constructor and you've typed any other constructor(s) into your class code, the compiler won't provide the no-arg constructor (or any other constructor) for you. In other words, if you've typed in a constructor with arguments, you won't have a no-arg constructor unless you type it in yourself!
■ Every constructor has, as its first statement, either a call to an overloaded constructor (this()) or a call to the superclass constructor (super()), although remember that this call can be inserted by the compiler.
■ If you do type in a constructor (as opposed to relying on the compiler-gener- ated default constructor), and you do not type in the call to super() or a call to this(), the compiler will insert a no-arg call to super() for you, as the very first statement in the constructor.
■ A call to super() can be either a no-arg call or can include arguments passed to the super constructor.
■ A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no- arg constructor.
■ You cannot make a call to an instance method, or access an instance variable, until after the super constructor runs.
■ Only static variables and methods can be accessed as part of the call to super() or this().
■ Abstract classes have constructors, and those constructors are always called when a concrete subclass is instantiated.
■ Interfaces do not have constructors. Interfaces are not part of an object's inheritance tree.
■ The only way a constructor can be invoked is from within another constructor.
@javaCode☕️
👉Rules for #Constructors
■ Constructors can use any access modifier, including private. (A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.)
■ The constructor name must match the name of the class.
■ Constructors must not have a return type.
■ It's legal (but stupid) to have a method with the same name as the class, but that doesn't make it a constructor. If you see a return type, it's a method rather than a constructor. In fact, you could have both a method and a constructor with the same name—the name of the class—in the same class, and that's not a problem for Java. Be careful not to mistake a method for a constructor—be sure to look for a return type.
■ If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler.
■ The default constructor is ALWAYS a no-arg constructor.
■ If you want a no-arg constructor and you've typed any other constructor(s) into your class code, the compiler won't provide the no-arg constructor (or any other constructor) for you. In other words, if you've typed in a constructor with arguments, you won't have a no-arg constructor unless you type it in yourself!
■ Every constructor has, as its first statement, either a call to an overloaded constructor (this()) or a call to the superclass constructor (super()), although remember that this call can be inserted by the compiler.
■ If you do type in a constructor (as opposed to relying on the compiler-gener- ated default constructor), and you do not type in the call to super() or a call to this(), the compiler will insert a no-arg call to super() for you, as the very first statement in the constructor.
■ A call to super() can be either a no-arg call or can include arguments passed to the super constructor.
■ A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no- arg constructor.
■ You cannot make a call to an instance method, or access an instance variable, until after the super constructor runs.
■ Only static variables and methods can be accessed as part of the call to super() or this().
■ Abstract classes have constructors, and those constructors are always called when a concrete subclass is instantiated.
■ Interfaces do not have constructors. Interfaces are not part of an object's inheritance tree.
■ The only way a constructor can be invoked is from within another constructor.
@javaCode☕️