4

I want to have the following setup:

abstract class Parent {
    public static String ACONSTANT; // I'd use abstract here if it was allowed

    // Other stuff follows
}

class Child extends Parent {
    public static String ACONSTANT = "some value";

    // etc
}

Is this possible in java? How? I'd rather not use instance variables/methods if I can avoid it.

Thanks!

EDIT:

The constant is the name of a database table. Each child object is a mini ORM.

6
  • 4
    Off-topic, but why use an object-oriented programming language if you're going to avoid the object-oriented part? Commented Oct 12, 2010 at 2:34
  • but what are you trying to achieve? Not creating an obj for class Child?, creating static is equally expensive. Commented Oct 12, 2010 at 2:35
  • Can you give an example of how you might use that? Commented Oct 12, 2010 at 2:36
  • I have my reasons, but they would be unpopular in a public forum. The program is small, this datum is unlikely to change or require a special accessor, and I come from a different language where this feels more natural. I'm not trying to avoid objects, just trying to apply KISS to my program design and keep the word count down. I have plenty of objects, trust me. Commented Oct 12, 2010 at 2:42
  • 1
    possible? yes, but there is a better approach for sure. Give more context and you'll get a better answer, and learn something in the mean time Commented Oct 12, 2010 at 3:01

2 Answers 2

18

you can't do it exactly as you want. Perhaps an acceptable compromise would be:

abstract class Parent {
    public abstract String getACONSTANT();
}

class Child extends Parent {
    public static final String ACONSTANT = "some value";
    public String getACONSTANT() { return ACONSTANT; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

What if there are many subclasses? Is there a cleaner way to do this than copy-pasting the same getACONSTANT() method into each subclass?
@ChiuBaka - make an adapter class, ie one which implements the method and then extend the adapter class. stackoverflow.com/questions/10170698/what-is-adapter-class
2

In this case you have to remember is in java you can't overried static methods. What happened is it's hide the stuff.

according to the code you have put if you do the following things answer will be null

Parent.ACONSTANT == null ; ==> true

Parent p = new Parent(); p.ACONSTANT == null ; ==> true

Parent c = new Child(); c.ACONSTANT == null ; ==> true

as long as you use Parent as reference type ACONSTANT will be null.

let's you do something like this.

 Child c = new Child();
 c.ACONSTANT = "Hi";
 Parent p = c;
 System.out.println(p.ACONSTANT);

Output will be null.

1 Comment

Whoa, cool example. Thanks for taking the time to post that :-) +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.