An interface in java is a blueprint of a class. It defines what the class will do. The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritance in Java. An interface represents an IS-A relationship. A clas...

Read More

Marker or Tag interfaces are interfaces which do not have any method declaration in them. Natural question is why would anyone need such interfaces. These are mainly required by JVM/Compiler to identify certain object types which require specific handling. Some examples are Serializable, Cloneable, Remote etc. You can also create your own marker in...

Read More

Java 8 permits static interface methods: With Java 8, interfaces can have static methods. They can also have concrete instance methods, but not instance fields. There are really two questions here: Why, in the bad old days, couldn't interfaces contain static methods? Why can't static methods be overridden? Static methods in interfaces There was no ...

Read More

An interface can be declared as a member of a class or another interface. Such an interface is called a member interface or nested interface. A nested interface can be declared as public, private or protected as opposed to a top-level interface which must be public or default. When a nested interface is used outside of its enclosing scope, it must ...

Read More

There are a lot of uses for interfaces, but one the most common ways I've used them is to keep responsibilities clearly separated. Single responsibility principle You want an example though, so rather than blather on about techniques or reasons, here's an example that is simplified, but almost identical to a setup I've worked with before:     pub...

Read More

Java's predecessor in many ways, C++, allowed multiple inheritance i.e one class can extend more than one class. This used to cause a very unique problem which is popularly known as a Diamond problem. An illustration is shown in the image below.   Like in the image say we have 4 classes A,B,C,D as defined below class A{ void do(){ System....

Read More

No, we cannot instantiate an Interface in java. But some time it can come as a tricky interview question. Some interviewer can write a piece of code, by instantiating and interface and which will work fine. Consider the below code example: We have an Animal interface: interface Animal { void Eat(String foodName); } And in client code we are c...

Read More

FOLLOW US ON LinkedIn



Explore Tutu'rself