• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Interface in Java  RSS feed

 
Ansh Sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hii,

I have one doubt , Why do we write interface in java .
Interface does not provide any code reusability as by Abstract classes .
As a matter of fact I feel like this creates more code while defining the Interface .
Classes can anyways have their methods to define why explicitely I write Interface and
make unneccesary code also being injected in my Class through Interface .
 
Gautam Bhalla
Ranch Hand
Posts: 30
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ansh,

As you must already be knowing that interfaces does not contains method implementations it just contain method declarations and they are implicitly public abstract methods that are supposed to be overridden.
Technically there can be many answers to your question but logically from practical scenario,

Lets take an example of a flying object for which we want to calculate weight and implement its flying functionality after that.
i.e,
FLY that contains two methods------- calculateWieght() and startFly()


Now,we know that an aeroplane can fly.we also know that a mosquito can also fly.
We also know that way we calculate weight of a mosquito is completely different from weight calculcation of aeroplane.


But both are adhering to common contract of flying and weight of both can also be calculated(irrespective of the way this is accomplished).

It means both of them are adhering to a common contract of capability of flying and weight calculation.(Although each of them does this in its own way).

So In JAVA interface let us realize this thing effectively as it imposes a kind of compulsion that if a object has to be able to fly then its weight must be calculated
and must implement startfly functionality.(As per example).

Now let us take the example of abstract classes(where we can have chance of having body of member function,say,startfly() in our case).Then ,Even though both will have their own calculateweight() functionality but In this case we are not getting compulsion but a kind of option that either aeroplane and mosquito should use a common way of flying (which is offcourse not possible) or they can implement this flying functionality of their own(by overriding the previous one).

So,technically we can realize the way interfaces work through abstract classes but this is something which is not effective to model a realistic scenario (or contract) as specified in above example.

So its not about unneccesary coding but it is all about effective modeling or real world objects via OOP(which is its prime feature).

The example may seem funny but I hope this notion would help you to have a better grip on the concept.


 
Ansh Sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gautam ,

Many Thanks for the reply .
My doubt remains the same . Why can't we use the abstract class and have two abstract methods startFly() and calculateWeight()
instead of having Interface . For Example if tomorrow we find out some common behaviour between the classes (which in your case of Aeroplane and Mosquito might never happen ) we can have that concrete implementation in the abstract class .
Which design model of OOP is supported through this .. ?

Thanks!!
 
Gautam Bhalla
Ranch Hand
Posts: 30
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ansh Sharma wrote:Hi Gautam ,

Many Thanks for the reply .
My doubt remains the same . Why can't we use the abstract class and have two abstract methods startFly() and calculateWeight()
instead of having Interface . For Example if tomorrow we find out some common behaviour between the classes (which in your case of Aeroplane and Mosquito might never happen ) we can have that concrete implementation in the abstract class .
Which design model of OOP is supported through this .. ?

Thanks!!


Hi Ansh,

Aeroplane and mosquito were just a part of example but whenever we create a interface we never look for any specific thing rather we have a general idea that if something has to happen(flying in previous case) then its contract (i.e, associate functionality of startfly() and calculateweight()) should be taken as compulsion in order to make flying happen.

And as I already said In abstract class we might have a chance where startfly() functionality was already implemented.It means we have decided that all flying things should have startfly() functionality in a way specified in my abstract class which is wrong for modelling any kind of generalization.

Please do remember Abstract classes may talk about specialization but interface is just a implementation independent general contract whose actual realization depends upon more specialized sub classes.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Multiple inheritance has some inherent problems, which are by design decision avoided in Java so that multiple inheritance is only allowed from pure virtual ancestors, that is, ones with only declarations and constants but without implemented methods. As a "syntactic sugar", different keywords emphasize the difference between "real" (implementation) and pure interface inheritance.

Multiple inheritance - The diamond problem
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ansh,

One can draw analogy from real world. In real world something can be of a certain type only but can perform different types of roles. For example, a person is a human being but can play roles of father, son, manager etc etc. Similarly in java, a class can play different roles depending on which interfaces it implements. Parent class from which it inherits puts a IS-A constraint on the class but there are no such bindings with interfaces.

Manish
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ansh Sharma wrote:Why can't we use the abstract class

What happens if you want to implement more that one 'thing'?

You can only extend one abstract class, but you can extend multiple interfaces.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ansh Sharma wrote:My doubt remains the same . Why can't we use the abstract class and have two abstract methods startFly() and calculateWeight() ... Instead of having Interface . For Example if tomorrow we find out some common behaviour between the classes (which in your case of Aeroplane and Mosquito might never happen ) we can have that concrete implementation in the abstract class .

Right, but as as soon as you do that, you've tied yourself in to an implementation, which is exactly what interfaces are designed to avoid.

The best real-world case I know is a List (java.util.List) of which there are two basic patterns:
  • A random List (eg, ArrayList), which extends from AbstractList.
  • A sequential List (eg, LinkedList), which extends from AbstractSequentialList.

  • The implementations are very different (indeed, the docs for AbstractSequentialList mention it as being "the opposite of AbstractList", and if you didn't have a List interface to tie the two together, you couldn't do things like:
    List<String> aList = new ArrayList<String>();

    The reason you DO write that type of definition is that if you discover later on that ArrayList was the wrong sort of List for the job, you can easily change it to:
    List<String> aList = new LinkedList<String>();
    and you don't have to change anything else in your program.

    Which design model of OOP is supported through this .. ?

    Basically: Abstraction. but I suggest you read a bit more about it.

    You may also find the WhatNotHow (←click) page worth reading.

    Winston
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!