0

I am new to Java and have been reading up on its main topics. I recently covered abstract classes and instances. I have read their definitions so its not a definition i am looking for.

I need help understanding why I would need to use an interface. All the examples I have seen of interfaces on java tutorials online to me seem like they can be implemented using an abstract class/method combination instead.

Is there a common scenario where only an interface can be used to solve the issue and a combination of abstract class/method would not?

4 Answers 4

2

A good example from the Java APIs is the LinkedList class.

As you can see from the link, it implements both the List and the Deque interfaces (as well as a few others), allowing it to be used whenever one or the other is needed. This would not have been possible using only abstract classes.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks I had not gotten to LinkedList yet in my reading. This makes it much clearer.
2

An interface defines a new secondary datatype in Java. It (interface) is a group of final variables and abstract methods only.

The members of an interface are "public" by default. Infact private and protected modifiers are not allowed in an interface.

An interface is a reference type only its objects cannot be created.

An interface can inherit another interface but cannot inherit any class.

A class cannot inherit any interface but it (a class) can implement zero to many interfaces.

If a class implements interfaces then 1) It has to override all the abstract methods of all the implemented interfaces.

2) Type compatibilty gets created between the interface and the class. It allows an interface reference can refer to object of implementing class.

interface Iface
{
  int x = 3;//final and public by default
  void f();//abstract and public by default
}

interface AnotherI extends Iface
{ 
  //more declarations possible here
}

class InterfaceDemo implements Iface
{
 public void f()
 {
   int i;
   for(i =0 ; i< x; i++)
     System.out.println("Interfaces are widely used");
 }

 public static void main(String args[])
 {
   Iface ref = new InterfaceDemo();
   ref.f();//allowed
   //ref.newMethodsOfClass();//not allowed
 }
}

3 Comments

+1 Also, an interface can inherit multiple interfaces (in contrast to classes).
Thanks alot for the explanation and example.I could use an abstract class to get the same out put as this example. I was looking for an example where only an instance could achieve the desired result.
@MichelMichaelMeyer since most of the examples i saw weren't using multiple interfaces I didn't see it. I think I get it now thanks.
1

The main difference is that you can only extend one class but you can implement multiple interfaces.

This can be important if you want to (for example) implement a listener while also extending another class. Note though that in many cases you are better off using an anonymous inner class to do the listening.

Comments

0

Java does not support polymorphic inheritance - where a class can extend multiple classes. This choice was made to avoid the problems associated with hierarchical conflict resolution (where a method exists in both parent classes - which one do you use?). In simple parlance, in java there can only be one "is a" relationship.

However, to allow a form of polymorphism, interfaces were introduced. These are hollow templates for which the class must provide implementations, and offer a way of creating a "look like a" relationship. While only allowing an object to "be" a instance of one (parent) class, it may "look like" any number of interfaces.

By taking inheritance out of interfaces, it allows cross-cutting concepts to be handled - ie where classes of completely different inheritance structures may nevertheless share some abstract similarity. A good example from the JDK is Serializable, which is a marker interface (one without methods). Virtually any class has the possibility to be serializable, but virtually all serializable classes don't share any of their inheritance hierarchy. This concept couldn't be handled with abstract classes - you have to step out of the class hierarchy to achieve it - ie interfaces is the only way to do it.

1 Comment

thanks. THis is the exact kind of examples I as looking for. I will read up on it more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.