Java confusion: extends vs implements
Aug. 20th, 2006 06:42 pmGiven classes A and B,
means that instances of A can invoke methods implemented in B.
means that A implements the interfaces in B. The methods are implemented in A.
In this sense, "extends" and "implements" are opposites.
But I often think of them as being the same, because with the class names normally used, both of them can be read as "ISA". This is confusing.
I think the solution is to consciously distinguish "is a subtype of" from "is an implementation of".
---
Multiple inheritance is a bit tricky. I don't understand how all these things fit in, but from here I learned that:
* You need to create a new class Child, to be the result of the mix between Parent (first parent) and Other (second parent).
* Child extends Parent and implements 2 interfaces, MProvides and MRequires.
* There is a class called Mixin, used for the purpose of communicating with Other, which implements MProvides, and whose constructor requires an instance of MRequires. This instance simulates a parent-class for Mixin.
* The Child's constructor instantiates a Mixin, passing itself "this" as an argument. For each method
I don't yet understand whether this last annoyance is circumvented with "General Multiple Inheritance".
A extends Bmeans that instances of A can invoke methods implemented in B.
A implements Bmeans that A implements the interfaces in B. The methods are implemented in A.
In this sense, "extends" and "implements" are opposites.
But I often think of them as being the same, because with the class names normally used, both of them can be read as "ISA". This is confusing.
I think the solution is to consciously distinguish "is a subtype of" from "is an implementation of".
---
Multiple inheritance is a bit tricky. I don't understand how all these things fit in, but from here I learned that:
* You need to create a new class Child, to be the result of the mix between Parent (first parent) and Other (second parent).
* Child extends Parent and implements 2 interfaces, MProvides and MRequires.
* There is a class called Mixin, used for the purpose of communicating with Other, which implements MProvides, and whose constructor requires an instance of MRequires. This instance simulates a parent-class for Mixin.
* The Child's constructor instantiates a Mixin, passing itself "this" as an argument. For each method
func(...) that you inherit from Other, you need to write a implement a method mixin.func(...)I don't yet understand whether this last annoyance is circumvented with "General Multiple Inheritance".
(no subject)
Date: 2006-08-20 11:58 pm (UTC)(no subject)
Date: 2006-08-21 12:18 am (UTC)Thanks for the notes.
(no subject)
Date: 2006-08-21 09:41 pm (UTC)public class A extends B { ... } public class B { ... }how would you refactor A to use "implements" instead of "extends"?
(no subject)
Date: 2006-08-21 09:49 pm (UTC)(no subject)
Date: 2006-08-21 09:56 pm (UTC)He is implying that in the hardcore professional software engineer world, everyone gives up modularity.
(no subject)
Date: 2006-08-21 10:14 pm (UTC)(no subject)
Date: 2006-08-21 11:37 pm (UTC)(no subject)
Date: 2006-08-21 02:25 am (UTC)And they aren't really opposites. In both the implements and extends cases, the A class can call methods implemented in B. And in both cases, A can override methods implemented in B and add new methods. The only difference is that, in the implements case, somewhere in B is at least one method that hasn't been implemented yet and needs to be implemented in A.
(no subject)
Date: 2006-08-21 02:29 am (UTC)(no subject)
Date: 2006-08-21 05:59 am (UTC)(no subject)
Date: 2006-08-21 09:12 pm (UTC)(no subject)
Date: 2006-08-21 09:12 pm (UTC)(no subject)
Date: 2006-08-21 09:46 pm (UTC)If the compiler was smart enough to tell when a class was totally abstract (which I'm pretty sure is impossible in any practical sense), we wouldn't need interfaces at all. We could just allow multiple extends and require that at most one of the classes extended be a non-totally-abstract-class.
(no subject)
Date: 2006-08-21 10:13 pm (UTC)Can't you tell from the method declarations? i.e. a class without internal curly brackets is totally abstract.
Is there any reason they shouldn't have used "extends" for both superclasses and interfaces?
'implements' = OO, 'extends' = convenience
Date: 2006-08-24 12:44 pm (UTC)Aspects is the currently-hip way of providing the functionality that mixin classes used to give, namely added functionality. Also, some design patterns which need small code changes in different classes (such as visitors or observers) can be neatly brought together in one aspect.
-- Joost Cassee (no account yet)