When is interface used




















That means all the methods in an interface are declared with an empty body and are public and all fields are public, static and final by default.

A class that implements an interface must implement all the methods declared in the interface. To implement interface use implements keyword. Why do we use interface? The reason is, abstract classes may contain non-final variables, whereas variables in interface are final, public and static.

So we make an interface and put all these common functionalities. And lets Bicycle, Bike, car …. We can now add default implementation for interface methods. This default implementation has special use and does not affect the intention behind interfaces.

Suppose we need to add a new function in an existing interface. Obviously the old code will not work as the classes have not implemented those new functions. So with the help of default implementation, we will give a default body for the newly added functions. Then the old codes will still work. Note: these methods are not inherited.

A class can implement more than one interface. An interface can extends another interface or interfaces more than one interface. A class that implements interface must implements all the methods in interface. All the methods are public and abstract. And all the fields are public, static, and final. It is used to achieve multiple inheritance. It is used to achieve loose coupling. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please explain this interface just provide us funtion declarations how it gets too much importance in java although we must define all function behavior in subclass.. Unknown, Flexibility is the keyword, interface provide the much needed flexibility, which means a single line of code like a call to interface method can do different things in different context, I mean when a different implementation is passed.

For example Executor. Feel free to comment, ask questions if you have any doubt. Pages Home core java spring online courses thread java 8 coding sql books oop interview certification free resources best.

An interface in Java has remained a complex topic for many beginners to understand. The first thing which puzzles many programmers is the fact that you cannot define any method inside interface , it a just declaration. By rule, all method inside interface must be abstract Well, this rule is changing in Java 8 to allow lambda expressions, now interface can have one non-abstract method, also known as a default method.

So, if you can't define anything, Why we need an interface? Well, if you are thinking in terms of behaviour then you are really missing the point of interface. I think one has to read Effective Java , to understand best use of interface. Interface is great to declare Type, they promote code reusability, and they are the real driver of polymorphism in Java. The interface also allows multiple inheritance in Java , which makes it possible for a class to become Canvas , as well as EventListener, which is used to draw graphics as well as to to process events.

In this post, I will share few points, which will help you to understand what is the actual use of interface in Java. The interface is usually the better choice, as client classes can implement any amount of interfaces, but they can only one have one superclass "inheritance is a scarce resource", as they put it.

Why would you want either an abstract class or an interface? Because sometimes, when you write an algorithm you don't care how a specific sub-step of it is done, just that it is done according to some kind of contract. An example would be the Collections API, with List being an interface - usually, when you use a List , you don't really care if it's saving stuff in an array, or in a linked list of nodes, or in some other kind of way.

As long as it stores the stuff you put in it in the order you put them there, you're happy. Then we have AbstractList : An abstract class implementing List , which provides implementation of almost everything a fully-fledged List needs - to create your own List implementation, all you have to do is extend AbstractList and fill in a few methods.

This is a prime example of when an abstract class is a good choice - when you want to provide an almost-complete implementation of something, which only is lacking a few gaps which needs to be filled in by the client code. Hint: If you make an abstract class containing only abstract methods, you should probably make an interface of it.

A basic principle of OOP is information hiding: hide the implementation details, and show only a description of the basic services to the caller. Java has to constructs for this goal: interfaces and abstract classes.

You can define an interface, and write your code, that it only depends on the interface by calling the "available methods" defined in it. Information hiding is usable both for reading external classes external in a sense that it comes outside the module you are writing - this way you can define what methods you need, and there is no need to infer its concrete implementation type - , or when defining a data type usable outside your classes - a typical example for this is e.

Both interfaces and abstract classes provide these details - but there are two major differences: interfaces support multiple inheritance, while abstract classes can hold a base implementation. To maximize their efficiency, when defining an interface, you should also define an abstract class with a meaningful default implementation.

If the user of the interface does not need to extend any other class, it can extend this abstract class, otherwise it needs to implement all methods from the interface. On the other hand make sure not to read this abstract class directly, the interface should be enough as abstraction. Interfaces can also be used to define an agreed upon "protocol" for communicating between different portions of the system, possibly through remote calls. So the interface only defines what can be called, with what parameters, and what will be returned after the call.

The client uses the interface and the server implements the concrete actual code for example. In Java you can only inherent extend from one class, but you can implement more than one interface, so sometimes you will have to use interfaces when you need that sort of multiple inheritance and when you decide not to use composition over inheritance.

Use interfaces whenever you plan to replace an implementation class by another class at some point of development. I also would recommend to use interface backends for all classes in inheritance relationships at least in more serious projects: Unfortunately I don't have the link anymore but a developer of the language Java once said, that including Class-Inheritance was the biggest mistake in designing the language.

The arguments are quite good: Using proper design, it is always possible to replace class inheritance by interface inheritance and you gain a lot concerning code maintainance. It is also easier to preserve natural type relations like e. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. When should I use an interface in java? Asked 11 years, 7 months ago. Active 4 years, 9 months ago. Viewed 67k times. Improve this question. TylerH Julio Julio 5, 11 11 gold badges 50 50 silver badges 63 63 bronze badges. Thanks for the replys everyone really helpful in pointing me in the right direction. You've found the search box just in case somebody may have asked something similar before?

Add a comment. Active Oldest Votes.



0コメント

  • 1000 / 1000