Java Interview Questions

BY Author

Java – Interfaces

Interfaces are similar to classes.An interface can contain only abstract methods, not concrete methods.An interface can also contain variables. But these variables are not instance variables. They are public, final& staticimplicitly.

Interface just tells “what to do,” not “how to do.” It is the duty of a class that implements the interface.

For better understanding let’s take a small real life example.

Suppose you are an owner of some company & you want some software for that company.Then you’ll go to some software company as a customer & will demand them to create software for your company.You’ll just tell them your requirement that is “what to do.” Now it’s a job of that software company to implement your demand.Here, your demand acts as an interface & company as a class that will implement your demand.

The overridden method in a class that implements an interface must be declared as public, as well as its syntax, must match with the method specified in the interface.Any interface can be implemented by any number of classes.Each implementing class has complete independence to implement the interface in its own way.

If any class implements a specific interface but does not implement all the methods of that interface then it is known as a partial implementation of the interface.The class that implements an interface partially must be declared as abstract. That is the class must either implement interface completely or must be declared as abstractitself.