Re: OO is not that great: many repeated codes



Shawn wrote:
Hi,

I think OO is not great, compared to procedural languages. Each method has to be hosted in one 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.

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. Particularly, Professor and Calculator cannot share a superclass in real world. And in Java, there is no multiple inheritance.

Nicely rigged example.

If, however, we created a class called "cooking skills"
which contains 27 methods, and 4 fields, it suddenly looks
much more reasonable for a person to "HAVE" cooking
skills (the difference between "has a" and "is a" in OO design)

ditto for maths and driving.

BugBear
.



Relevant Pages

  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • problem in java swings
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • Re: OO is not that great: many repeated codes
    ... void cookDeliciousFood() ... Professor and Calculator can do some advanced calculation, ... int average ... public class Professor ...
    (comp.object)
  • OO is not that great: many repeated codes
    ... void cookDeliciousFood() ... Professor and Calculator can do some advanced calculation, say find the average of two integers: ... int average ... public class Professor ...
    (comp.lang.java.programmer)
  • OO is not that great: many repeated codes
    ... void cookDeliciousFood() ... Professor and Calculator can do some advanced calculation, say find the average of two integers: ... int average ... public class Professor ...
    (comp.object)