- #1
- 2,844
- 0
I'm trying to do something right in object-oriented programming. It's easy enough to come up with a bad, "non-object-oriented" solution, but I don't want to break the paradigm: it's appropriate, and also there's something to be said for using a language as it's intended.
The problem is one of inheritance. Suppose you want to create a number of children, each of which shares function and semantics from a parent. The parent might be a class, an abstract class, an interface, or something else. The parent has nontrivial functionality which relies on specific methods which will be defined only in children.
A quick mocked-up example should give some meaning to this. The code below is a Java-C# hybrid that should be easy enough for any OO programmer to read.
The big issue is how to deal with types. At best, the above code could be cobbled together but require constant casts and un/boxings. At worst, it won't compile because of conflicting requirements on types. Further, the code will always be ugly because each method will be filled with "X is Y" checks -- or, if C#, "X as Y". Would templates (Group<T>) be better? Is there a way (C#, C++, Java, etc.) to specify "the class itself"?
An obvious alternate method would be to build lightweight structs off an interface, copy/pasting the duplicate functionality. But this seems to be a bad idea... it leads to uneven updates and bug fixes. Also, the purpose here would be to force as many methods as possible to the parent (in this case, generic methods that work for any class) and that would defeat the purpose.
The problem is one of inheritance. Suppose you want to create a number of children, each of which shares function and semantics from a parent. The parent might be a class, an abstract class, an interface, or something else. The parent has nontrivial functionality which relies on specific methods which will be defined only in children.
A quick mocked-up example should give some meaning to this. The code below is a Java-C# hybrid that should be easy enough for any OO programmer to read.
Code:
abstract class Group {
// Abstract methods, must be overwritten
public static abstract Group identity();
public abstract Group add (Group a);
public abstract Group copy ();
public static Group + (Group left, Group right) {
return left.copy().add(right);
}
// A real implementation of this function would use a binary ladder
public Group scalarMult (Group a, int n) {
Group x = Group.identity();
for (int i = 0; i < n; ++i)
x = x.add(a);
return x;
}
}
public class ZModN extends Group {
private int value;
private int modulus;
// Overrides for abstract methods
public static override Group identity() {
// Code here
}
public override Group add (Group a) {
// Code here
}
public override Group copy () {
// Code here
}
// Custom programming
public static ZModN chineseRemainder (ZModN a, ZModN b) {
// ...
}
}
public class Integer extends Group {
private int value;
// Overrides for abstract methods
public static override Group identity() {
// Code here
}
public override Group add (Group a) {
// Code here
}
public override Group copy () {
// Code here
}
// Override general method with a faster version for this type
public override Group scalarMult (Group a, int n) {
if (!(a is Integer))
throw new Exception("...");
int result = ((Integer)a).value * n;
return new Integer (result);
}
}
The big issue is how to deal with types. At best, the above code could be cobbled together but require constant casts and un/boxings. At worst, it won't compile because of conflicting requirements on types. Further, the code will always be ugly because each method will be filled with "X is Y" checks -- or, if C#, "X as Y". Would templates (Group<T>) be better? Is there a way (C#, C++, Java, etc.) to specify "the class itself"?
An obvious alternate method would be to build lightweight structs off an interface, copy/pasting the duplicate functionality. But this seems to be a bad idea... it leads to uneven updates and bug fixes. Also, the purpose here would be to force as many methods as possible to the parent (in this case, generic methods that work for any class) and that would defeat the purpose.