Flaw in JRE DefaultTableModel code? (OO design)

M

Mr Smith

Let's start with a simple sample on dynamic disptach:

--------------------------------
public class A {

public void add(String name, Object value) {
System.err.println("A.add(" + name + ", " + value + ")");
}

public void add(String name) {
System.err.println("A.add(" + name + ")");
add(name, null);
}

public class B extends A {

public void add(String name, Object value) {
System.err.println("B.add(" + name + ", " + value + ")");
super.add(name, value);
}

public void add(String name) {
System.err.println("B.add(" + name + ")");
super.add(name);
}

}

public static void main(String[] args) {
B b = new A().new B();

b.add("key2");
}
}

--------------------------------

output:
B.add(key2)
A.add(key2)
B.add(key2, null) <= argh
A.add(key2, null)

The problem is: from A.add(key2), B.add(key2, null) is called instead of A.add(key2, null).

i need to prevent that!!!


Real life:

A = javax.swing.table.DefaultTableModel
B = MyTableModel
add = addColumn

so i can't modify A, and i add to much column in B because of this problem.

Is there a way to use inheritance in a correct way and avoid copying "super." code?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top