Object With the same functionality but defferent Parameters

Hi All,

i'll directly come to the problem.

i've one class name VoterListCallBack -

class VoterListCallback implements ListFieldCallback {

private Vector listElements = new Vector();

public VoterListCallback(boolean show, String userType){

}

public void drawListRow(ListField list, Graphics g, int index, int y, int w) {

}

public Object get(ListField list, int index) {

}

public int indexOfList(ListField list, String p, int s) {

}

public int getPreferredWidth(ListField list) {

}

public void insertVoter(int index, Voter v) {

}

public void removeVoter(int index, Voter v) {

}

public void updateVoter(Voter v, int index) {

}

public void erase() {

}

}

And another class is -

class PrecinctListCallback implements ListFieldCallback {

private Vector precinctListElements = new Vector();

public PrecinctListCallback() {}

public void drawListRow(ListField list, Graphics g, int index, int y, int w) {

}

public void insertPrecinct(int index, String precinct) {

}

public Object get(ListField list, int index) {

}

public int indexOfList(ListField list, String p, int s) {

}

public int getPreferredWidth(ListField list) {

}

public void erase() {

}

}

Here both classes has the common methods that you all can see and also both classes is being inherited from the same class. but here the problem is that all the method that is common in the both classes has different arguments. i did solve the problem by creating different classes for each problem, But somewhat i'm not feeling it is a good design. So i want some suggetion from you guys.

[1787 byte] By [dave123a] at [2007-10-3 11:20:55]
# 1
could you format your code, please
georgemca at 2007-7-15 13:45:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

OK,

Hi All,

i'll directly come to the problem.

i've one class name VoterListCallBack -

public class VoterListCallback implements ListFieldCallback {

private Vector listElements = new Vector();

public VoterListCallback(boolean show, String userType){

}

public void drawListRow(ListField list,Graphics g,int index, inty, int w) {

}

public Object get(ListField list, int index) {

}

public int indexOfList(ListField list, String p, int s) {

}

public int getPreferredWidth(ListField list) {

}

public void insertVoter(int index, Voter v) {

}

public void removeVoter(int index, Voter v) {

}

public void updateVoter(Voter v, int index) {

}

public void erase() {

}

}

And another class is -

publlic class PrecinctListCallback implements ListFieldCallback {\

private Vector precinctListElements = new Vector();

public PrecinctListCallback() { }

public void drawListRow(ListField list, Graphics g, int index, int y, int w) {

}

public void insertPrecinct(int index, String precinct) {

}

public Object get(ListField list, int index) {

}

public int indexOfList(ListField list, String p, int s) {

}

public int getPreferredWidth(ListField list) {

}

public void erase() {

}

}

Here both classes has the common methods that you all can see and also both classes is being inherited from the same class. but here the problem is that all the method that is common in the both classes has different arguments. i did solve the problem by creating different classes for each problem, But somewhat i'm not feeling it is a good design. So i want some suggetion from you guys.

IS it okay for you guys

dave123a at 2007-7-15 13:45:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
http://forum.java.sun.com/help.jspa?sec=formatting
georgemca at 2007-7-15 13:45:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

public class VoterListCallback implements ListFieldCallback {

private Vector listElements = new Vector();

public VoterListCallback(boolean show, String userType){

}

public void drawListRow(ListField list, Graphics g, int index, int y, int w) {

}

public Object get(ListField list, int index) {

}

public int indexOfList(ListField list, String p, int s) {

}

public int getPreferredWidth(ListField list) {

}

public void insertVoter(int index, Voter v) {

}

public void removeVoter(int index, Voter v) {

}

public void updateVoter(Voter v, int index) {

}

public void erase() {

}

}

And another class is -

publlic class PrecinctListCallback implements ListFieldCallback {

private Vector precinctListElements = new Vector();

public PrecinctListCallback() { }

public void drawListRow(ListField list, Graphics g, int index, int y, int w) {

}

public void insertPrecinct(int index, String precinct) {

}

public Object get(ListField list, int index) {

}

public int indexOfList(ListField list, String p, int s) {

}

public int getPreferredWidth(ListField list) {

}

public void erase() {

}

}

Here both classes has the common methods that you all can see and also both classes is being inherited from the same class. but here the problem is that all the method that is common in the both classes has different arguments. i did solve the problem by creating different classes for each problem, But somewhat i'm not feeling it is a good design. So i want some suggetion from you guys.

IS it okay for you guys

dave123a at 2007-7-15 13:45:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

When george mentioned to format your code, he was referring to the code-Tag, which makes your code look like this:

public class VoterListCallback implements ListFieldCallback {

private Vector listElements = new Vector();

public VoterListCallback(boolean show, String userType) {

}

public void drawListRow(ListField list, Graphics g, int index, int y, int w) {

}

public Object get(ListField list, int index) {

}

public int indexOfList(ListField list, String p, int s) {

}

public int getPreferredWidth(ListField list) {

}

public void insertVoter(int index, Voter v) {

}

public void removeVoter(int index, Voter v) {

}

public void updateVoter(Voter v, int index) {

}

public void erase() {

}

}

class PrecinctListCallback implements ListFieldCallback {

private Vector precinctListElements = new Vector();

public PrecinctListCallback() {

}

public void drawListRow(ListField list, Graphics g, int index, int y, int w) {

}

public void insertPrecinct(int index, String precinct) {

}

public Object get(ListField list, int index) {

}

public int indexOfList(ListField list, String p, int s) {

}

public int getPreferredWidth(ListField list) {

}

public void erase() {

}

}

That was simple. Now, to your question.

Where lies the problem you think you have? You show two classes that implement (not inherit) the ListFieldCallback interface. Which part does not satisfy your needs? Where are the doubts? What exactly are you referring to when telling about common methods and different arguments?

stefan.schulza at 2007-7-15 13:45:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

I meant like this

public class VoterListCallback implements ListFieldCallback {

private Vector listElements = new Vector();

public VoterListCallback(boolean show, String userType){

}

public void drawListRow(ListField list, Graphics g, int index, int y, int w) {

}

public Object get(ListField list, int index) {

}

public int indexOfList(ListField list, String p, int s) {

}

public int getPreferredWidth(ListField list) {

}

public void insertVoter(int index, Voter v) {

}

public void removeVoter(int index, Voter v) {

}

public void updateVoter(Voter v, int index) {

}

public void erase() {

}

}

publlic class PrecinctListCallback implements ListFieldCallback {

private Vector precinctListElements = new Vector();

public PrecinctListCallback() { }

public void drawListRow(ListField list, Graphics g, int index, int y, int w) {

}

public void insertPrecinct(int index, String precinct) {

}

public Object get(ListField list, int index) {

}

public int indexOfList(ListField list, String p, int s) {

}

public int getPreferredWidth(ListField list) {

}

public void erase() {

}

}

using the [ code ] tags. but ne'er mind

I'm not sure what your actual question is, after all that. both classes appear to have the same methods, to me. do you mean you want to change the method signatures, so that subclasses can provide the same methods but for different types of arguments? if so, look into Generics, a new feature in Java 1.5

if not, erm, re-phrase the question a bit?

georgemca at 2007-7-15 13:45:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

Hi guys,

The methods in the ListCallBack class

public int indexOfList(ListField list, Voter v , int s) {

}

public void insertVoter(int index, Voter v) {

}

public void removeVoter(int index, Voter v) {

}

The methods in the PrecintListCallBack class

public int indexOfList(ListField list, String p , int s) {

}

public void insertPrecinct(int index, String p ) {

}

public void removePrecinct(int index, String p ) {

}

The highlighted arguments are different here. I was thinking of using simply insert(), remove(), indexOfList() method for both of the classes. The other methods that are common in the both of the classes should have been written in the common parent class and the methods that has different argument should have been written in the child class. Since both of the classes has the same work. That's why i was thinking my design/implementation of the classes is correct.

If you guys still not clear what i'm expecting PLZ reply me back. I'll try to elaborate the problem again.

THANKS

DAVE THE DEVIL

dave123a at 2007-7-15 13:45:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 8
Sorry,the last sentence is -That's why i was thinking my design/implementation of the classes is not correct.
dave123a at 2007-7-15 13:45:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 9

> The highlighted arguments are different here. I was

> thinking of using simply insert(), remove(),

> indexOfList() method for both of the classes.

> The other methods that are common in the both of the

> classes should have been written in the common

> parent class and the methods that has different

> argument should have been written in the child class.

> Since both of the classes has the same work.

Well, I cannot see a problem here. If you want the classes to inherit common methods, that's fine. If the classes each have different parameters for same named methods, which the parent class does not implement, this is fine, too, with any JDK. Additionally, as george mentioned, in Java5 you could make the parent class use Generics to make children inherit or implement these methods.

stefan.schulza at 2007-7-15 13:45:57 > top of Java-index,Other Topics,Patterns & OO Design...