Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why we need interface instead of class and what we are achieving from interface

Tags:

java

As we know that we can declare only the method signature and also can not create the instance of a Interface. then why we need interface. Unnecessary its loading into JVM. This is also one performance degradation. We are creating the interface and several classes implementing that interface and defining all the methods of the interface. Actually what we achieved from this interface. Could you please give me some example.

like image 598
Nishi Avatar asked Jan 20 '11 09:01

Nishi


People also ask

Why would you use an interface instead of a class?

Having interfaces separate from classes allows for clear separation between, well, the interface of an object and its implementation. Without them you would have no standard way to indicate that some class should not contain implementation details at all.

Why do we need interface instead of abstract class?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

What is interface Why is it needed?

Purpose of the interface Provides communication − One of the uses of the interface is to provide communication. Through interface you can specify how you want the methods and fields of a particular type.

Why is the interface concept present when something could be achieved using classes or abstract classes?

Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes. Interfaces are a good choice when we think that the API will not change for a while.


2 Answers

Interface is you are forcing your client to implement some specified thing, implementation will be remain to the client.Also java doesn't support multiple inheritance by extending multiple classes you can have multiple interface implemented.

For example : List declares add(..) method all the implementation of List provides it implementations.

Simpler would be.

You define an Interface Animal and a method speak() it means all Animal must will have to speak with different different implementation. Man will speak,Dog will bark,Lion will roar.

Why should we go for create class Animal extra. We can declare the speak() in every class. What is befit we will get from Animal class and implementing speak() in all the sub classes. Still I did not get this concept

Main advantage is inheritance and polymorphism [core concepts of OOP]

You are specifying Animal's behavior here also.

You can have

Animal obj = new Man();

Animal obj = getAnimalForThisCriteria(something here);//this will return some animal at runtime so you can catch instance using Animal.

You might have Three different Class Ma,Dog,Lion with same method but there is no way to tell they all are animal unless they extends or implements common class or interface, here comes the concept of structure

like image 130
jmj Avatar answered Oct 21 '22 11:10

jmj


Having interfaces separate from classes allows for clear separation between, well, the interface of an object and its implementation. Without them you would have no standard way to indicate that some class should not contain implementation details at all.

Second, since Java does not support multiple inheritance, interfaces are a partial workaround, by allowing inheritance on the outward features of the class.

like image 27
thkala Avatar answered Oct 21 '22 10:10

thkala