javainterface

Why Interfaces in Java if we have abstract class?


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.


Solution

    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.

    Often, classes are named as nouns (e.g. Integer, List) while interfaces are verbs (e.g. Cloneable, Serializable), but this is not always the case; for example, Collection and List are interfaces.

    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.