Re: OO is not that great: many repeated codes
- From: Bruno Desthuilliers <bdesth.quelquechose@xxxxxxxxxxxxxxxxxxx>
- Date: Tue, 26 Sep 2006 23:27:17 +0200
Shawn a écrit :
Hi,
I think OO is not great, compared to procedural languages.
Well, it's your right.
Each method has to be hosted in one class.
stop !
OO means *object* oriented. Not "class-based". Some OO languages don't even have a notion of "class".
So the identical method code could be repeatedly in several classes. I know inheritance /abstract classes/ interface concepts well. But please let me explain my point:
Professor and Student (two classes) know drive car well. So the method
void driveCarWell()
has to be in both classes.
From a purely OO POV, not at all. Instances of both classes have to be able to handle the driveCarWell message - how they do handle it is implementation-dependant, and doesn't necessarily requires implementing the method in each class.
HouseWife and Professor know how to cook. So the method
void cookDeliciousFood()
has to be in both classes.
Professor and Calculator (Two classes) can do some advanced calculation, say find the average of two integers:
int average(int a, int b)
has to be in both classes.
Now the classes are like the following:
public class Professor
{
void driveCarWell()
void cookDeliciousFood()
int average(int a, int b)
...
}
public class Student
{
void driveCarWell()
...
}
public class HouseWife
{
void cookDeliciousFood()
...
}
public class Calculator
{
int average(int a, int b)
...
}
As you see, methods are repeated in many classes. It is hard to use inheritance to solve the issue.
Declarative static typing issues set aside, inheritance is a very overrated concept, and not really central to OO (what's central is polymorphic dispatch of messages to methods). FWIW, it's just a special case of composition/delegation, where the child class delegates to it's parent class... If you have either type inference or dynamic typing, and a good support for delegation, you don't really need inheritence that much.
Particularly, Professor and Calculator cannot share a superclass in real world. And in Java, there is no multiple inheritance.
This problem is usually solved quite easily with either composition/delegation or mixins. But it's true that Java has no support for the former (you have to do it all by hand) and forbid the later. So I'd say your problem is with Java, not with OO. Learning Smalltalk, Ruby or Python might be a good idea.
In procedural language, you only need to write the method once and put them in the global place. Then you can grab them anytime you want. Because a method don't need to be buried inside a class.
FWIW, this can be emulated in Java with static methods. But let's pretend Java supports plain functions. Please show us how you would implement say cookDeliciousFood() or driveCarWell() as plain plain old procedures... You'll probably find out that writing generic procedural code with a braindead declarative static type system is not always that simple.
.
- References:
- OO is not that great: many repeated codes
- From: Shawn
- OO is not that great: many repeated codes
- Prev by Date: Re: Advice on finding more material
- Next by Date: Re: Abstract public member variales?
- Previous by thread: Re: OO is not that great: many repeated codes
- Next by thread: Advice on finding more material
- Index(es):
Relevant Pages
|