Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Interfaces in Java if we have abstract class? [duplicate]

Tags:

java

interface

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.

like image 642
vignesh Avatar asked Jun 26 '13 06:06

vignesh


3 Answers

  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.

like image 69
Colonel Panic Avatar answered Nov 18 '22 01:11

Colonel Panic


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

like image 5
Nargis Avatar answered Nov 17 '22 23:11

Nargis


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

like image 3
stinepike Avatar answered Nov 18 '22 01:11

stinepike