Lets take the java.io.Serializable marker interface. It does not have any member defined in it. Whenever a class is to be serialized, you should intimate the java compiler in some way that there is possibility of serializing this java class. In this scenario, marker interfaces are used. The...
the program will throw compile time error. Also notice that in for loop, we don’t need type casting of the element in the list, hence removing the ClassCastException at runtime.
As discussed above, an interface can not implement another interface. It has to extend the other interface. See the below example where we have two interfaces Inf1 and Inf2. Inf2 extends Inf1 so If class implements the Inf2 it has to provide implementation of all the methods of interfaces...
LinkedHashMap is a type of Collection, which takes all the functionalities of HashMap class i.e. it stores our data in a pair such that each element has a key associated with it. As, HashMap does not maintains the insertion order, that is when we retrieve values from it we do not g...
Java Comparable interface Example The example below shows how to implement the Comparable interface in a user defined class and define the compareTo() method to make the objects of that class comparable. import java.time.LocalDate; import java.util.Objects; class Employee implements Comparable<Emplo...
Example Attached. Why and when to use an Interfaces? An interface is a reference type in Java. It is similar to class. It is acollectionof abstract methods. It is used to achieve totalabstraction. I believe this is the first question you might expect inJava Interview. Very basic questions...
Example: Sorting arrays and Wrapper class importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Collections;importjava.util.List;publicclassDemo{publicstaticvoidmain(String[]args){/* * Integer class implements Comparable * Interface so we can use the sort method ...
Learn about the ThreadFactory interface in Java, its purpose, and see practical examples of how to implement it effectively.
Another way to achieveabstractionin Java, is with interfaces. Aninterfaceis a completely "abstract class" that is used to group related methods with empty bodies: ExampleGet your own Java Server // interfaceinterfaceAnimal{publicvoidanimalSound();// interface method (does not have a body)public...
Learn about the IntFunction interface in Java with examples. Understand its usage and how to implement it effectively in your Java applications.