Problem
I'm trying to use Java generics to replace classes with similar methods. All of the examples that I've found cover simple examples, but I'm not sure if Java Generics are intended to be used this way.
I have 2 parent classes, and 2 child classes that have almost identical methods. Both parent classes derive from different classes as well. Ultimately, I want to be able to use one block of code to create and manipulate one of the parent classes, then its child class without a lot of switch statements or other flow control with duplicate code.
This is what I had in mind, even though I haven't beeen able to get it to work this way yet, whether it be syntax, or just not a feature of Generics.
Parent Classes
public class FooParent
{
private FooChild fooChild;
public FooChild getChild()
{
return fooChild;
}
}
public class BarParent
{
private BarChild barChild;
public BarChild getChild()
{
return barChild;
}
}
Child Classes
public class FooChild
{
public void print()
{
System.out.println("I'm a foo child");
}
}
public class BarChild
{
public void print()
{
System.out.println("I'm a bar child");
}
}
Generic Classes
public class GenericParent<T>
{
private T self;
public GenericParent(T self)
{
this.self = self;
}
public GenericChild getChild()
{
return new GenericChild(self.getChild());
}
}
public class GenericChild<T>
{
private T self;
public GenericChild(T self)
{
this.self = self;
}
public void print()
{
self.print();
}
}
How I want to use them
public static void main(String args[])
{
GenericParent parent;
// Only the initialization of the parent variable needs specialized code
switch(args[0])
{
case "foo":
parent = new GenericParent(new FooParent());
break;
case "bar":
parent = new GenericParent(new BarParent());
break;
}
// From here on out, it's all generic
parent.getChild().print();
}
Usage and desired output
java genericExample foo
> I'm a foo child
java genericExample bar
> I'm a bar child
Final Questions
Maybe "child" and "parent" are misnomers, because I know they're not actually inherited, but the bottom line is, the one class returns its "child" with certain methods. So this is a lot of code for a problem that may not actually be solvable this way, but hopefully you can answer me this:
- Is this something that Java Generics can accomplish?
- If not, is there a solution to this problem in Java?
Thanks!
Edit
My "Foo" and "Bar" classes are uneditable by me. My ultimate question is: can I store one instance of either class in a single variable without using a common parent class?
Your parent seems a wrapper, a wrapper is a container, so yes it may be something that can benefit from a type parameter.
But I can't see any type parameter except in the constructor signature (and what is self? No bounds, no hints, no anything...), so using a generic type doesn't buy anything to you here. It's no use to introduce a type parameter if the methods you are interested in return void and declare an empty parameter list.
Here's the guidance: if methods in your classes would benefit from having a type parameter, ie if a type parameter is useful in any method return type or in the signature, then genericize your class. Otherwise, stick with what you currently have.