Reputation: 47471
I'm trying to refactor my code by using a BaseComponentType
class and inheriting from this in my ElectricalComponentType
class (and similar child classes), as follows:
public abstract class BaseComponentType {
public static BaseComponentType findByUid ( Class klass, String uid ) {
return new Select().from( klass ).where( "uid = ?", uid ).executeSingle();
}
}
public class ElectricalComponentType extends BaseComponentType {
public static ElectricalComponentType findByUid( String uid ) {
return (ElectricalComponentType) findByUid( ElectricalComponentType.class, uid );
}
}
What I need to do is call ElectricalComponentType.findByUid( 'a1234' )
but it would be great if I did not have to define findByUid
in the ElectricalComponentType
class and instead could inherit this functionality from the BaseComponentType
.
You'll notice that two things stand in the way:
I need the ElectricalComponentType
class in the findByUid
parent method.
I need to return ElectricalComponentType
object (or whatever the child class object is) instead of a BaseComponentType
class object.
Is there a way to do this?
Upvotes: 3
Views: 4942
Reputation: 424983
Use generics and only have the parent class method:
public abstract class BaseComponentType {
public static <T extends BaseComponentType> T findByUid(Class<T> klass, String uid) {
return new Select().from( klass ).where( "uid = ?", uid ).executeSingle();
}
}
Upvotes: 7
Reputation: 1831
I think you could reengineer this so that you have a ComponentFinder
class that finds Component
s.
public class ElectricalComponent extends Component {
@Override
public void method1(){
//specific stuff
}
}
public abstract class Component{
public abstract void method1();
}
public class ComponentFinder{
public static Component findByUid ( Class klass, String uid ) {
return new Select().from( klass ).where( "uid = ?", uid ).executeSingle();
}
}
Then you don't have to worry about the inheritance issue.
Upvotes: 0
Reputation: 4262
i hope you need something like following..
public class TestClass{
public static void main(String args[]){
Child c2;
c2 = (Child) Child.findByUid(Child.class, "123");
System.out.println(c2.getClass());
}
}
class Base{
public static Base findByUid ( Class klass, String uid ) {
System.out.println(klass);
Child c = new Child();
//execute your query here and expect it to return the type of object as the class by which it was called
//your parent class method always returns the type of the child by which the method was called
return c;
}
}
class Child extends Base{
/*public static Child findByUid( String uid ) {
System.out.println(Child.class);
return (Child) findByUid( Child.class, uid );
}*/
}
Upvotes: 0
Reputation: 6533
There are several points to note:
static
methods are not being inherited and cannot be overriden;static
method of your parent class, you have to write class name first: BaseComponentType.findById()
;If you want to get rid of method with the same name in child class, you may want to make it non-static or/and reconsider your classes design, because if you have two static methods with the same names in classes bound with inheritance relation, most likely there is something wrong with class design.
Upvotes: 0