Re: OO is not that great: many repeated codes



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.
.



Relevant Pages

  • Re: question about assigning null to a reference object
    ... String getAuthor() ... int getPages() ... void setAuthor ... b.setTitle("Thinking in Java 54th Edition"); ...
    (comp.lang.java.help)
  • question about assigning null to a reference object
    ... String getAuthor() ... int getPages() ... void setAuthor ... b.setTitle("Thinking in Java 54th Edition"); ...
    (comp.lang.java.help)
  • Re: Help on Parsing
    ... void InitTokenString; ... int ch=getch; ... char *str; ... I am trying to search a string for a Java function definition using ...
    (comp.lang.c)
  • Re: The working of a++ in C
    ... void main ... {int a=1, b; ... You are modifying the value of a variable twice ... Are you trying to contrast this behaviour with Java? ...
    (comp.lang.java.programmer)
  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)