A
Andrew McDonagh
Ok, there seems to have been a mis-communication between us on this...
I completely agree with you that Interfaces & Abstract classes are
different and are used for different purposes.
No you are completely right, it would be absurd at best.
So we agree.
Whilst a think Tony's stance is somewhat 'unique', I was trying to
highlight the benefits of using Interfaces are the reference type rather
than concrete or abstract classes, as this allows for a more flexible
(i.e. loosely coupled) design.
Weirdly enough, in my work Interfaces only come into existence when I
have a need to reference more than one version of the original concrete
type. Until then, I simply use the single concrete class types as the
reference.
As soon as I have a need for another version of the same type, then
first I'll 'extract interface', and then create the second concrete
class. If the two concrete classes have duplicate code, then I will of
course create an intermediate abstract class.
A very simple example....
Person ---uses--> Car
What about when they also need to drive a truck?
The code would be
Person ---uses --> vehicle
^
----------------
|
Car truck
then i remove the duplication..
Person ---uses --> vehicle
^
|
AbstractVehicle
^
|
----------------
|
Car truck
But what about when the person uses a boat, would be 'weird' for boat to
derive from AbstractVehicle as it doesn't have wheels. So by passing
refs around via the interface we over come any problems when we need to
change.
Ok...maybe that was too simplistic, but you get the idea...
We agree...
Ryan said:I remain unconvinced. Interfaces and abstract classes are two different
animals intended for two different purposes.
I completely agree with you that Interfaces & Abstract classes are
different and are used for different purposes.
Maybe in your work, interfaces
predominant, but should java.lang.Number be an interface? What about
java.util.AbstractCollection, java.iutputStream, or java.text.DateFormat?
If your answer is yes, you are at the least promoting heavy-duty code
duplication.
No you are completely right, it would be absurd at best.
So we agree.
Whilst a think Tony's stance is somewhat 'unique', I was trying to
highlight the benefits of using Interfaces are the reference type rather
than concrete or abstract classes, as this allows for a more flexible
(i.e. loosely coupled) design.
Weirdly enough, in my work Interfaces only come into existence when I
have a need to reference more than one version of the original concrete
type. Until then, I simply use the single concrete class types as the
reference.
As soon as I have a need for another version of the same type, then
first I'll 'extract interface', and then create the second concrete
class. If the two concrete classes have duplicate code, then I will of
course create an intermediate abstract class.
A very simple example....
Person ---uses--> Car
What about when they also need to drive a truck?
The code would be
Person ---uses --> vehicle
^
----------------
|
Car truck
then i remove the duplication..
Person ---uses --> vehicle
^
|
AbstractVehicle
^
|
----------------
|
Car truck
But what about when the person uses a boat, would be 'weird' for boat to
derive from AbstractVehicle as it doesn't have wheels. So by passing
refs around via the interface we over come any problems when we need to
change.
Ok...maybe that was too simplistic, but you get the idea...
We agree...