• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusions in Interface  RSS feed

 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well , I have few short queries to clarify :

*************************************************************************************************************************

Q1) we all known the famous Comparable interface in java . This is how it is implemented :



Now , my question is Why make it generic ? I mean we could have implemented it as :



What would be difference and drawback ??

***************************************************************************************************************************

Q2)Well, they say multiple inheritance can be implemented in java by interface. Below is a scenario given. How can I achieve this multiple inheritance in java by interfaces ?? If possible add some more examples of your own to explain this concept.We have 2 superclasses A and B , And subclass C Which needs to inherit methods of Class A and B.




***************************************************************************************************************************



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) I've got a better question: why not? What about from the point of view of the implementer?

2) "They" (whoever they" may be) may say that, but they would be wrong.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
2) "They" (whoever they" may be) may say that, but they would be wrong.


Agreed. Anyone who ever used the multiple inheritance of C++, will *not* consider using interfaces as remotely the same as multiple inheritance, even with the new Java 8 default methods feature.

Henry
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q2: Some people say that being able to implement multiple interfaces alleviates the need for multiple inheritance, but Java doesn't have multiple inheritance by design.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generics is a feature that introduces the notion of type safe programming for unknown types... Also thinking about comparisons where it make more sense to compare objects of the same type which is introduced by the generic notation...

If we were to use a non generic version we can have the following:


Without generics we can introduce run-time errors if we do not take the precautions of testing every object we are comparing to make sure that its of the same type... which will inherently introduce clutter into the intentions of the code
 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all , but ... one confusion remains ...
We say - "Although Java does not have multiple inheritance, a Java class does have the ability to implement multiple interfaces instead."
So, can every multiple inheritance be modeled in java using interfaces ?? If yes, how can we be so sure of that ??
 
Mark King
Ranch Hand
Posts: 55
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushant Kunwara wrote:So, can every multiple inheritance be modeled in java using interfaces ??

Prior to Java 8 no multiple inheritance was able to be modeled, because all methods of a interface must be abstract . Even in Java 8, when default methods were added, full multiple inheritance is not available
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushant Kunwara wrote: . . . We say . . .
Who are “we”? Where did you get that quote from?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote: . . .
. . .
Careful. If you write complicated‑looking formulae like that the ?: operator, a lot of the people who post on this forum will believe you
 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Who are “we”? Where did you get that quote from?


HERE
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushant Kunwara wrote: . . .
HERE
Thank you. Though any site which says implements Wolf, Canine and doesn't realise that it should read… is going to appear dubious to me.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushant Kunwara wrote:
Now , my question is Why make it generic ? I mean we could have implemented it as :

What would be difference and drawback ??

The difference and drawback will be immediately apparent if you try to create a class that implements this proposed interface. Go ahead, try it with a non-trivial class where you have to access a few fields to determine the result of compareTo(Comparable o). Of course, you're going to have to put it in a different package so that it doesn't conflict with the standard java.lang.Comparable
 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
The difference and drawback will be immediately apparent if you try to create a class that implements this proposed interface. Go ahead, try it with a non-trivial class where you have to access a few fields to determine the result of compareTo(Comparable o). Of course, you're going to have to put it in a different package so that it doesn't conflict with the standard java.lang.Comparable


Not able to understood what you are saying.... Please give small example to illustrate your point ... Thanks
 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Where did you get that quote from?


I am unable to understand what you are trying to tell me. Is the quote wrong ?

OR The answer that

Mark King wrote: Prior to Java 8 no multiple inheritance was able to be modeled, because all methods of a interface must be abstract . Even in Java 8, when default methods were added, full multiple inheritance is not available


is wrong ?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushant Kunwara wrote:
Junilu Lacar wrote:
The difference and drawback will be immediately apparent if you try to create a class that implements this proposed interface. Go ahead, try it with a non-trivial class where you have to access a few fields to determine the result of compareTo(Comparable o). Of course, you're going to have to put it in a different package so that it doesn't conflict with the standard java.lang.Comparable


Not able to understood what you are saying.... Please give small example to illustrate your point ... Thanks


Rico Felix did this earlier in the thread, and he gives a good example of why using the raw (non-generic) Compare is not good.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushant Kunwara wrote:
Campbell Ritchie wrote:Where did you get that quote from?


I am unable to understand what you are trying to tell me. Is the quote wrong ?

OR The answer that

Mark King wrote: Prior to Java 8 no multiple inheritance was able to be modeled, because all methods of a interface must be abstract . Even in Java 8, when default methods were added, full multiple inheritance is not available


is wrong ?


The point is that there is no multiple inheritance in Java. The article you quoted says (and I said) that implementing several interfaces can be concerned a substitute for multiple inheritance, although others disagree.
 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:Without generics we can introduce run-time errors if we do not take the precautions of testing every object we are comparing to make sure that its of the same type... which will inherently introduce clutter into the intentions of the code


Thanks ... But i still have one confusion .If 2 different classes , here for ex Dog and Car implements Comparable interface then it means that they can be compared with each other also !! For example we can compare them with their Date of birth and manufacturing date , as to who came first . What I mean to say that if 2 classes who don't have a common superclass implements comparable , does that mean they will never be comparable to each to other ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushant Kunwara wrote:
Thanks ... But i still have one confusion .If 2 different classes , here for ex Dog and Car implements Comparable interface then it means that they can be compared with each other also !!


If you implement it correctly, with generics, then trying to compare two different class type objects should fail to compile. If you didn't use generics, but still implemented it correctly, then it should throw a class cast exception, if the class type makes the instances non-comparable.

Sushant Kunwara wrote:
What I mean to say that if 2 classes who don't have a common superclass implements comparable , does that mean they will never be comparable to each to other ?


There is no such a thing as "2 classes who don't have a common superclass" in Java.

Henry
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such a thing as "2 classes who don't have a common superclass" in Java.


That is, all classes are subclasses of java.lang.Object.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushant Kunwara wrote: . . .
I am unable to understand what you are trying to tell me. Is the quote wrong ?
. . .
We request that people quote their sources so as to confirm there are not copyright problems about the quotes.
There are thousands of sources of information on the net. Some are better than others; there are many which are not actually bad but are sloppily written and add little to what the reader already knows. So you need to be careful that you are reading the better ones.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushant Kunwara wrote:
Junilu Lacar wrote:
The difference and drawback will be immediately apparent if you try to create a class that implements this proposed interface. Go ahead, try it with a non-trivial class where you have to access a few fields to determine the result of compareTo(Comparable o). Of course, you're going to have to put it in a different package so that it doesn't conflict with the standard java.lang.Comparable

Not able to understood what you are saying.... Please give small example to illustrate your point ... Thanks

That's kind of my point. I was telling you to try implementing your version in actual code. Your proposed variation of Comparable is not going to allow you to write good implementations because if you try to actually implement it, you will see that it's not possible using only a reference to another Comparable.

You can't learn how to swim just by asking questions -- eventually, you have to get in the water and actually learn how to float and paddle around; you have to do it yourself. Similarly, you can't learn how to program if you don't try things out yourself. Try to write an implementation using your proposed version so you can see what the differences and drawbacks are for yourself.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to write an implementation using your proposed version so you can see what the differences and drawbacks are for yourself.


And I would add, then when you do, come back here and talk about what you discovered.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushant Kunwara wrote:If 2 different classes , here for ex Dog and Car implements Comparable interface then it means that they can be compared with each other also !! For example we can compare them with their Date of birth and manufacturing date , as to who came first . What I mean to say that if 2 classes who don't have a common superclass implements comparable , does that mean they will never be comparable to each to other ?


From the first line docs for Comparable:
"
This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering
"

In your scenario there would be no such thing as a natural ordering of cars and dogs. It makes no sense.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!