Re: Understanding Classes
alexandre_paterson@yahoo.fr wrote:
On Jul 21, 1:37 pm, brian void <brian.voi...@gmail.com> wrote:
Hi I'm new to programming and have some questions about classes.
You can learn OO concept using Java, but it may not be the
best option.
The problem is that Java makes it very hard on OO beginners by
providing two keywords to define an OO class: 'class' and
'interface' (similar -but not identical- to a C++ pure abstract
class).
I don't know that that makes life harder for beginners than anything else.
It's not a very complicated concept in Java.
Still, it's best for beginners to think of object-oriented structures as based
on "types", then follow Joshua Bloch's advice and use interfaces to represent
types. Put off thinking about classes for a while, when you are focused on
object-oriented concepts more intellectually. However, Java is also a highly
pragmatic language, and sooner or later one's code has to do something actual.
So when thinking pragmatics one can comprehend classes as reification for types.
Really, even for beginners this should be only a few minutes' thinking.
With Java 'class', you only have single inheritance. With Java
'interface', you have multiple inheritance.
You can decide to live with it and model your OO hierarchy by
both extending concrete classes (and abstract classes) and
interfaces in your codebase.
But to me (and quite some others) this is a huge mistake: you'd
really be mixing two different things and mix up implementation
details with your OO model. Implementation details do not exist
at the OO-Analysis/OO-Design level.
First, I like to have a pure mapping from my OOD to my
Java interfaces. Then the translation OOD -> OOP is often
not possible using single inheritance.
Inheritance of classes is easy to overuse. You can mix in any number of
interfaces to a concrete class and generally that will suffice.
Most people don't realize this, but it's because they hardly
do OO at all.
And OO is god.
On the small codebase I'm working on at the moment
(6 digits LOC) every single class is 'final' and there
is not a single instance of the 'abstract' keyword. And
multiple inheritance is used everywhere. And we're doing
persistence using an OO DB :)
Well, you have my sympathy on the database and kudos for your class
implementation.
Anyway, you'll have people arguing that having the ability
to extend a concrete class is a good thing (even tough
James Gosling himself said he regretted "not having gone
pure interface") but...
If by "good" you mean "useful" or "often saves labor" or "quite handy if used
correctly", then yes, it is good to have implementation inheritance available.
As with nearly all techniques, you don't have to use it just because you can.
You'll have way less people arguing that this is not
completely and utterly misleading for OO beginners.
I lost your antecedent there. Are you saying that concrete inheritance is
misleading for OO beginners?
Of course, it's only "misleading" in that it leads people to think about
things differently from how you personally feel they ought to think, given
that OO is god. If it even does that. I feel certain that anyone reading any
responsible literature on object-oriented programming will be able to spot
areas where Java differs from at least some of those theoretical models.
There's nothing about Java as a language that promises to be "pure" in some
purist's sense of the word, and if it isn't promising to be something that it
isn't then it isn't misleading anyone.
So IMHO Java could have been both simpler and cleaner
by removing the 'abstract' keyword and by preventing
to 'extend' (Java) classes but I'm one of those few
that like the mathematical definition of the word
'elegant'.
Programs that use concrete inheritance can be simpler to maintain than with
other approaches. It's a useful technique.
At times.
--
Lew