9

I have two questions

  1. Why do we need interface in java even if abstract class can do the functionality of interfaces?
    On searching i found a place where abstract class fails to do the functionality of interfaces, that is when a class needs to implement multiple interfaces.
    Is it the only reason for bringing up the interfaces concept in java?

  2. What is the use of static final variables in interfaces? Need example programs that state usage of variables in interfaces are welcome.

Thanks in Advance.

vignesh
  • 456
  • 1
  • 5
  • 14
  • If you have two questions, ask two questions. Or, better yet, google those two questions. There's 99% chance they'll both have an answer on the 1st page of the search results. – Dariusz Jun 26 '13 at 07:10

5 Answers5

5
  1. Like you said, you can only inherit from one abstract class, but many interfaces. Also, inheriting implies "is-a" relationship, but an interface specifies some set of operations an object must support -- essentially it's behavior. I see these as separate ideas. In my experience, classes tend to be nouns (e.g. Integer, List) and interfaces are verbs (e.g. Cloneable, Serializable).

  2. If a class implements an interface with constants, then the class can refer to those constants without a qualifying class name (javapractices.com). However, as this post explains, it is a bad practice.

Community
  • 1
  • 1
Colonel Panic
  • 1,604
  • 2
  • 20
  • 31
5

Interfaces are useful in these contexts:

1-> As you mentioned , In Java we can extend only one Class but can implement multiple interfaces.And this is really implement for example if you want to create a background Thread in your Class and your extending some Class for reusability,then how ill you extend Thread Class? In this case Runnable comes to rescue which is an interface.

2->Also interfaced help us to enforce a certain design pattern.

3->Also serve as markable interfaces as in case of Serializable Interface

Variables in interface are static variables that act as gloabal and can be shared with all Classes that implement that interface

Nargis
  • 4,687
  • 1
  • 28
  • 45
3

Why do we need interface in java even if abstract class can do the functionality of interfaces? On searching i found a place where abstract class fails to do the functionality of interfaces, that is when a class needs to implement multiple interfaces. Is it the only reason for bringing up the interfaces concept in java?

Java doesn't support multiple class inheritance. So you can implement multiple inteface. more importantly you can implement interface and extend other class at the same time. If you used abstract class in place of interface which would not be possible.

What is the use of variables in interfaces? Need example programs that state usage of variables in interfaces are welcome.

Any "field" you declare in an interface will be a public static final field. In other words, a constant. So if you want to use any constant in all the subclass then you can define those in interface

stinepike
  • 54,068
  • 14
  • 92
  • 112
2

1st) Put into one simple sentence: Your class can implement a virtually infinite number of interfaces but only extend one (abstract) class. And abstract classes may contain code - usually basic implementations shared among subclasses.

Just look at the ArrayList class:

public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable

This would not be possible without interfaces...

2nd) There are no variables in interfaces. They are public final by default.

3rd) Using interfaces you can for example create collections with an interface as element type and fill it with objects of totally different types that do not have a common base class (left out Object).

Without interfaces you could only have a collection of Objects and check the type and cast for every single Object.

With interfaces you can just treat them as implementations of this interface and call the interface's methods on them without even knowing their concrete type.

Marco Forberg
  • 2,634
  • 5
  • 22
  • 33
1

1.

Is it the only reason for bringing up the interfaces concept in java?

Yes. See also Interface vs Abstract Class (general OO) :

interfaces may be multiple-inherited, abstract classes may not (this is probably the key concrete reason for interfaces to exist separately from abtract classes - they permit an implementation of multiple inheritance that removes many of the problems of general MI).

2.

What is the use of variables in interfaces? Need example programs that state usage of variables in interfaces are welcome.

You mean static variables? I wouldn't ever use "stateful" static variables in interface, but static constants are quite OK - this way you can add additional information that is somehow relevant to how the interface is defined.

Community
  • 1
  • 1
Kos
  • 70,399
  • 25
  • 169
  • 233
  • any examples for such static constant variable usage which add additional information that is somehow relevant to how the interface is defined – vignesh Jun 26 '13 at 07:06