Skip to main content
added 906 characters in body
Source Link
ekj
  • 1.1k
  • 2
  • 12
  • 23

Something like the following would get you closer

class Clazz { public static <U extends Clazz> void doIt(U thing) { } } 

EDIT: Updated example with more detail

public abstract class Thingo { public static <U extends Thingo> void doIt(U p_thingo) { p_thingo.thing(); } protected abstract void thing(); } class SubThingoOne extends Thingo { @Override protected void thing() { System.out.println("SubThingoOne"); } } class SubThingoTwo extends Thingo { @Override protected void thing() { System.out.println("SuThingoTwo"); } } public class ThingoTest { @Test public void test() { Thingo t1 = new SubThingoOne(); Thingo t2 = new SubThingoTwo(); Thingo.doIt(t1); Thingo.doIt(t2); // compile error --> Thingo.doIt(new Object()); } } 

Something like the following would get you closer

class Clazz { public static <U extends Clazz> void doIt(U thing) { } } 

Something like the following would get you closer

class Clazz { public static <U extends Clazz> void doIt(U thing) { } } 

EDIT: Updated example with more detail

public abstract class Thingo { public static <U extends Thingo> void doIt(U p_thingo) { p_thingo.thing(); } protected abstract void thing(); } class SubThingoOne extends Thingo { @Override protected void thing() { System.out.println("SubThingoOne"); } } class SubThingoTwo extends Thingo { @Override protected void thing() { System.out.println("SuThingoTwo"); } } public class ThingoTest { @Test public void test() { Thingo t1 = new SubThingoOne(); Thingo t2 = new SubThingoTwo(); Thingo.doIt(t1); Thingo.doIt(t2); // compile error --> Thingo.doIt(new Object()); } } 
added 5 characters in body
Source Link
ekj
  • 1.1k
  • 2
  • 12
  • 23

Something like the following would get you closer

class Clazz { public static <U extends Clazz> void doIt(U thing) { } } 

Something like the following would get you closer

class Clazz { public static <U extends Clazz> doIt(U thing) { } } 

Something like the following would get you closer

class Clazz { public static <U extends Clazz> void doIt(U thing) { } } 
Source Link
ekj
  • 1.1k
  • 2
  • 12
  • 23

Something like the following would get you closer

class Clazz { public static <U extends Clazz> doIt(U thing) { } }