• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion with Interfaces  RSS feed

 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has been said that Interfaces are the alternatives to implement Multiple_Inheritance in java .
but while using Interfaces i am getting the same ambiguity problem as while using Multiple-Inheritance in C++.

for example I have 2 variable named x and y in my Interface (Testable) .
and the same set of variables in my Class (Test)

So when i use like this

So please do tell me when the ambiguity problem still exist then why don't we use Multiple Intheritance .
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables in interfaces are static and final and so should always be accessed using the interface name. They should therefore not be thought of as inherited.
You can inherit multiple interfaces in Java by implementing multiple interfaces in one class but multiple inheritance of implementation is not allowed.

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look here, you find there are disadvantages to implementing interfaces with fields in: that led to the development on static imports.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

@Neeraj,
Inheritance is used to share one object characteristics with another object. Through Interfaces we cannot create objects so inheritance is not allowed. Inheritance generally used to achieve re usability and dynamic binding. Through interface we achieve dynamic binding only. As Armitage mentioned multiple ineritance is not allowed but implementation for several interfaces in a class is allowed and class/interface name with static variable require to access.


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeraj jain wrote:So please do tell me when the ambiguity problem still exist then why don't we use Multiple Intheritance.

Simply put: probably because the designers didn't think it was worth the bother. I think the others here have explained that what you're running into is not, strictly speaking, an inheritance issue; but you may also find some useful information in the DontSweatIt page - in particular this topic.

HIH

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