my dog learned polymorphism*
The moose likes Java in General and the fly likes Isn't Implementing Interfaces Inheritence ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Isn Watch "Isn New topic
Author

Isn't Implementing Interfaces Inheritence ?

Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
Actually i got a question does java supports multiple inheritance.....i answered YES-NO...... actually I am bit confused with this statement

When we write

class Class0 extends Class1 implements Interface1,Interface2,Interface13

isnt above Multiple inheritance




In above we can see Class1 using state and defining behavior which is declared in interface.

If it is not Multiple Inheritance then what is it?
Does inheritance onnly to deal with Class - Class, Interface-Interface relation or EXTENDS keyword?

Please put light on the statement - java supports multiple inheritance


Regards
Azrael Noor
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

no implementing interfaces isn't inheritance. there is no behaviour defined in an interface. it is just declared. how is it to be defined is upon the class implementing the interface. an interface is a CONTRACT between the interface and class implementing the interface.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Fread's mom-dad reasoning..
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Java does not support multiple inheritance, that's the correct answer.
Implementing interfaces offers some benefits of multiple inheritance, but it is not multiple inheritance per se.


The quieter you are, the more you are able to hear.
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
Thank you for responding

It means when we see following thing

class Class0 implements Interface1,Interface2,Interface13

So do we use term CONTRACT, for above relation or any other Jargon available

What about STATE i.e int 1 in above example, a static final. We are also using it in the class where we implement the Interface.

Isn't using State also part of inheritance? if not so then why?

is inheritance only about variables and behaviors?

dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
When a class implements an Interface, it doesn't inherit anything. An Interface can't, for example, supply a default implementation of a method and bestow that on its implementing classes. Now, you have defined a variable in your Interface, and even though this is allowed in Java, it's not considered a best practice. More and more, people are discouraging it. It's really contrary to the intent of an Interface, which is simply to define, without specifying, a set of behaviors. Note that members so defined are implicitly static final; this means that the member is visible to any implementing class, and this is why you can access 'a' inside Class1. It is just like a static final field in a Class, and not at all the same situation as when a subclass inherits the member fields of its superclass.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Azrael Noor wrote:So do we use term CONTRACT, for above relation or any other Jargon available


Generally, the term "contract" is used when providing an implementation -- most typically a class implementing an interface or extending an abstract supertype.
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
Thank you Dennis Deems, Nice explanation


When we say Interface1 Extends Interface2, does this also means inheritance.
With the data provided till now means no it is not inheritance.
.
.
Am i correct?


Also as you said

Note that members so defined are implicitly static final; this means that the member is visible to any implementing class, and this is why you can access 'a' inside Class1. It is just like a static final field in a Class, and not at all the same situation as when a subclass inherits the member fields of its superclass.

Does this means Static Final variable in Subclass-Superclass relation and static final variable in interface-class relation have nothing to take with inheritance?

Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
Thank you Seetharaman Venkatasamy, the guy gives good reply in link you supplied

Thank you Kemal for answering, Implementing interfaces offers some benefits of multiple inheritance,
.
.
but in actual it should not be termed as multiple inheritance.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Azrael Noor wrote:
So do we use term CONTRACT, for above relation or any other Jargon available

What about STATE i.e int 1 in above example, a static final. We are also using it in the class where we implement the Interface.

Isn't using State also part of inheritance? if not so then why?

is inheritance only about variables and behaviors?

I think Mixin
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2994
    
    9
The truth is, there is no universal agreement on whether getting a declaration from an interface is considered inheritance or not. Clearly most people in this thread think it is not. The Java Language Specification, however, is quite clear that, as the JLS defines things, it is a form of inheritance - see JLS 8.4.8. Using this terminology, I would say Java does allow multiple inheritance of interfaces, but not of implementations. Obviously other people would define inheritance differently, and that's fine. But it's useful to know what definition they are using, and to understand that other definitions exist out there.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Azrael Noor wrote:Thank you Dennis Deems, Nice explanation


When we say Interface1 Extends Interface2, does this also means inheritance.
With the data provided till now means no it is not inheritance.
.
.
Am i correct?


That's a good question. Yes, this is correct. In this case, when we look at Interface2 we will also see all the methods defined on Interface1. So we can say that Interface2 "inherits" the methods of Interface1. Additionally, any Class that implements Interface2 is on the type hierarchy of Interface1. One of the most useful consequences of inheritance is that we can employ an instance of the subclass anywhere an instance of the superclass is required. The odd thing is that an Interface can extend more than one Interface! But a Class can extend only one Class. So on some level Java does distinguish between Class inheritance and Interface inheritance.


Also as you said

Note that members so defined are implicitly static final; this means that the member is visible to any implementing class, and this is why you can access 'a' inside Class1. It is just like a static final field in a Class, and not at all the same situation as when a subclass inherits the member fields of its superclass.

Does this means Static Final variable in Subclass-Superclass relation and static final variable in interface-class relation have nothing to take with inheritance?


Well ... the Java Language Specification does refer to this kind of relationship as "inheriting". But I think it's a little misleading because an ordinary inherited field is a property of the instance and has a state specific to that instance, while a static field exists at the Class level and has a state that is independent of any instance. Technically between superclass and subclass, and superinterface and subinterface, you are not incorrect to call this "inheritance", but between interface and implementing class I don't think this can be called inheritance. Perhaps someone can offer more insight here.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Mike Simmons wrote:Obviously other people would define inheritance differently, and that's fine. But it's useful to know what definition they are using, and to understand that other definitions exist out there.


So when you come to the SIQ* and somebody asks you if Java supports multiple inheritance, the answer is "That depends on your definition of inheritance. What did you have in mind for that?"

*Stupid Interview Question
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Mike Simmons wrote:The truth is, there is no universal agreement on whether getting a declaration from an interface is considered inheritance or not. Clearly most people in this thread think it is not. The Java Language Specification, however, is quite clear that, as the JLS defines things, it is a form of inheritance - see JLS 8.4.8.

Good point: the JLS does call it inheritance.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Mike Simmons wrote: I would say Java does allow multiple inheritance of interfaces, but not of implementations.

I will keep this in my mind. thanks Mike
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2994
    
    9
Paul Clapham wrote:
Mike Simmons wrote:Obviously other people would define inheritance differently, and that's fine. But it's useful to know what definition they are using, and to understand that other definitions exist out there.


So when you come to the SIQ* and somebody asks you if Java supports multiple inheritance, the answer is "That depends on your definition of inheritance. What did you have in mind for that?"

*Stupid Interview Question

Or I'd just say that Java allows multiple inheritance of interfaces, but not of implementations. That gets to the point quicker in an interview, and we can expand on it if needed.
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
If Question : Java Supports Multiple Inheritance (answer true or false)

Answer: TRUE

if Question: Does Java Supports Multiple Inheritance (question asked by interviewer)

Answer:

Mike Simmons wrote:Java allows multiple inheritance of interfaces, but not of implementations. (and this is as per JLS)


but not of implementations <--- By this we means CLASSES.....

So this is perfect!!!
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2994
    
    9
Azrael Noor wrote:If Question : Java Supports Multiple Inheritance (answer true or false)

Answer: TRUE


Well, this is risky. If this is asked as a yes-no question, I would say that there is a very good chance that the question writer simply doesn't know what they are talking about, and they're expecting the answer to be NO. because this is very commonly said about multiple inheritance in Java. You really need more than yes/no to answer this properly; if a company stupidly asks this as a yes/no with no chance of feedback, I would guess NO and point out the problem to an intelligent living person as soon as the opportunity presented itself.
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
Mike Simmons wrote:
Azrael Noor wrote:If Question : Java Supports Multiple Inheritance (answer true or false)

Answer: TRUE


Well, this is risky. If this is asked as a yes-no question, I would say that there is a very good chance that the question writer simply doesn't know what they are talking about, and they're expecting the answer to be NO. because this is very commonly said about multiple inheritance in Java. You really need more than yes/no to answer this properly; if a company stupidly asks this as a yes/no with no chance of feedback, I would guess NO and point out the problem to an intelligent living person as soon as the opportunity presented itself.


Ya due to risky, i ticked on both TRUE and FALSE.
Though i cleared that round
but did not continue interview as i got some urgent piece of work more important then interview.

Still i got nice experience with all of you for giving details on this topic
 
Consider Paul's rocket mass heater.
 
subject: Isn't Implementing Interfaces Inheritence ?
 
Similar Threads
Please HELP...Devaka Exam Test 2 Number 69. I totally don't understand it.
Questions for you
interface help
Inner Classes - Why can't i access a protected inner class' constructor in a subclass?
every java class inherits object?