This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Aug 3, 2007 12:02 AM by 791266 RSS

inner class on an abstract class?

807605 Newbie
Currently Being Moderated
Can i define an inner class on an abstract class?
public abstract class AbstractPropertySetter {

     void setValue(Object target, String value){
          
          
     }
     
     abstract Object convert(String value);

     
     public class StringPropertySetter extends AbstractPropertySetter {

          
          public StringPropertySetter(Class clazz, String property) {
               // TODO Auto-generated constructor stub
          }
          
          Object convert(String value) {
               // TODO Auto-generated method stub
               return null;
          }
          
     }
}
Eclipse gives no warnings, but when I try to
     public void testStringPropertySetter(){
          AbstractPropertySetter setter = new StringPropertySetter(TestContext.class, "testProperty");
     }
     
     class TestContext {
          private String testProperty;
     }
It can't find StringPropertySetter.
  • 1. Re: inner class on an abstract class?
    791266 Explorer
    Currently Being Moderated
         public void testStringPropertySetter(){
    In what class is that method located? You need to qualify the access to StringPropertySetter, and it also need an enclosing instance.
  • 2. Re: inner class on an abstract class?
    807605 Newbie
    Currently Being Moderated
    the test method is in a class in the same package, and is a JUnit TestCase.
  • 3. Re: inner class on an abstract class?
    807605 Newbie
    Currently Being Moderated
    sorry I reread your post -- by "enclosing instance" do you mean I need a concrete instance of an AbstractPropertySetter? (which would be impossible because its abstract?)
  • 4. Re: inner class on an abstract class?
    791266 Explorer
    Currently Being Moderated
    sorry I reread your post -- by "enclosing instance"
    do you mean I need a concrete instance of an
    AbstractPropertySetter? (which would be impossible
    because its abstract?)
    Correct, so make the StringPropertySetter static.

    Kaj
  • 5. Re: inner class on an abstract class?
    807605 Newbie
    Currently Being Moderated
    so the answer is it is impossible to have define a (non-static) inner class on an abstract class.

    Good enough. I was just trying to prevent package-bloat. the implementing classed don't have to be inner class (would have been nice), and unfortunately cannot be static....
  • 6. Re: inner class on an abstract class?
    807605 Newbie
    Currently Being Moderated
    unfortunately cannot be static....
    Why not?
    Declaring the class static only means that it doesn't need an instance of the enclosing class. In this case that's fine as it's extending the enclosing class, thus inheriting all it's members.
  • 7. Re: inner class on an abstract class?
    807605 Newbie
    Currently Being Moderated
    ahh, indeed. Although I find this syntax
    AbstractPropertySetter setter = new AbstractPropertySetter.StringPropertySetter(TestContext.class, "testProperty");
    a bit unnsettling.

    Thank you for the help...one more question:

    making the extending class(es) static does not prevent multiple instances, does it? These things will have state, so its important they aren't connected in any way....
  • 8. Re: inner class on an abstract class?
    791266 Explorer
    Currently Being Moderated
    making the extending class(es) static does not
    prevent multiple instances, does it?
    correct
    These things
    will have state, so its important they aren't
    connected in any way....
    They will have attributes that aren't shared.

    Kaj
  • 9. Re: inner class on an abstract class?
    807605 Newbie
    Currently Being Moderated
    ahh, indeed. Although I find this syntax
    AbstractPropertySetter setter = new
    AbstractPropertySetter.StringPropertySetter(TestContex
    t.class, "testProperty");
    a bit unnsettling.
    Could go with a factory.
  • 10. Re: inner class on an abstract class?
    doremifasollatido Journeyer
    Currently Being Moderated
    You don't need to make StringPropertySetter static in order to use it.
    public abstract class AbstractPropertySetter {
     
       void setValue(Object target, String value){
          
          
       }
       
       abstract Object convert(String value);
       Class clazz; 
       
       public class StringPropertySetter extends AbstractPropertySetter {
     
          public StringPropertySetter(Class clazz, String property) {
             this.clazz = clazz;
          }
          
          Object convert(String value) {
             return "Clazz: " + clazz + " : " + getClass() + " : " + value;
          }
          
       }
    }
    
    
    
    
    public class PropertySetter extends AbstractPropertySetter {
       
       Object convert(String value) {
          
          return "Clazz: " + clazz + " : " + getClass() + " : " + value;
       }
       
       public void testStringPropertySetter(){
          AbstractPropertySetter setter = new StringPropertySetter(TestContext.class, "testProperty");
          System.out.println(setter.convert("Testing"));
       }
       
       class TestContext {
          private String testProperty;
       }
     
    
       
       public static void main (String [] args) {
          AbstractPropertySetter abc = new PropertySetter();
          System.out.println(abc.convert("Testing"));
          
          AbstractPropertySetter.StringPropertySetter sps = abc.new StringPropertySetter(String.class, "length");
          System.out.println(sps.convert("Testing"));
    
          PropertySetter def = new PropertySetter();
          def.testStringPropertySetter();
       }
    }
  • 11. Re: inner class on an abstract class?
    791266 Explorer
    Currently Being Moderated
    You don't need to make StringPropertySetter static in
    order to use it.
    I'm not sure, but I thought that the OP didn't want to create a subclass such as PropertySetter.

    Kaj
  • 12. Re: inner class on an abstract class?
    doremifasollatido Journeyer
    Currently Being Moderated
    You don't need to make StringPropertySetter static
    in
    order to use it.
    I'm not sure, but I thought that the OP didn't want
    to create a subclass such as PropertySetter.
    Maybe you are right. It seems odd to me to make an inner class of a class extend the outer class, anyway--in order to use the inner class, you always end up with two concrete classes (you create one such as PropertySetter, in order to get access to the second one StringPropertySetter). But, if the inner class didn't extend the outer class, it makes more sense, and something similar to what I did will work.
  • 13. Re: inner class on an abstract class?
    796440 Guru
    Currently Being Moderated
    sorry I reread your post -- by "enclosing instance"
    do you mean I need a concrete instance of an
    AbstractPropertySetter? (which would be impossible
    because its abstract?)
    No. You need a concrete instance of some class that IS-A AbstractPropertySetter--i.e., a subclass.
    abstract class AbInnerParent {
      class Inner {
        void foo() {
          System.out.println(AbInnerParent.this);
        }
      }
    }
    
    public class AbInner extends AbInnerParent {
      public static void main(String[] args) throws Exception {
        AbInner ai = new AbInner();
        AbInnerParent.Inner inner = ai. new Inner();
        inner.foo();
      }
    }
  • 14. Re: inner class on an abstract class?
    807605 Newbie
    Currently Being Moderated
    >
    It seems odd to me to make an inner class of a class extend the outer class,


    yeah it is a bit odd. Like i said in my first post, the only reason to do that is to prevent package-bloat. Plus, all implementing classes look like
    static class IntegerPropertySetter extends PropertySetter {
    
              IntegerPropertySetter(Class clazz, String property) {
                   super(clazz, property);
              }
    
              Object convert(String value) {
                   return new Integer(value);
              }
         }
    which might be too small to justify a new file ;)

    The reason package bloat is an issue is we have a package private rule around here (nothing is visible outside package unless absolutely necessary), so I cant put the classes in a util sub package....tradeoffs tradeoffs....

    Message was edited by:
    shrndegruv
1 2 Previous Next