4

It's been quite awhile since I worked on anything in Java, and I really have very limited experience making anything with it at all, for the most part I write in higher level specified languages, scripting languages and the sort.

I assume I'm not understanding the concept behind assigning the default value of a class's members.

// parent
public class A {
    protected int value = 0;
    public void print() {
        System.out.println(value);
    }
}

// child class
public class B extends A {
    int value = 1;
}

To keep it short, creating an instance of B and calling print() prints the original value of "value" set in A.

I assume rather than being some kind of general identifier for "a variable named 'value' owned by this object," the defined function on A has a completely different context for "value" than B does.

What other method is there to organize common variations of a certain class but creating a child of said class? Some kind of factory object? It doesn't seem to be interfaces are the answer because then I have to redefine every single facet of the class, so I've gained nothing in doing so.

Any help would be much appreciated, thanks.

4
  • 2
    Declaring value in B is hiding the value in A. You need to make a constructor for B and set value=1 there. Commented Dec 17, 2014 at 18:42
  • 3
    See hiding fields for more information. Commented Dec 17, 2014 at 18:48
  • Field hiding isn't the culprit in this question. With field hiding, B.value would hide A.value. Instead, the OP is getting A.value, and expecting B.value. Commented Dec 17, 2014 at 19:14
  • I wonder if A.this.value and B.this.value would work. Commented Dec 17, 2014 at 19:27

2 Answers 2

6

Subclass fields do not override superclass fields.

You can set a default value through constructors, as illustrated below.

// parent
public class A {
    private int value = 0;
    public A( int initialValue ) {
        value = initialValue;
    }

    public void print() {
        System.out.println(value);
    }
}

// child class
public class B extends A {
    public B() {
       super( 1 );
    }
}

Also, in most cases, your life will be easier if you avoid protected fields.

Protected fields open your data to direct access and modification by other classes, some of which you may not control. The poor encapsulation can allow bugs and inhibit modification. As suggested in the Java tutorials on access control:

"Use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to."

Sign up to request clarification or add additional context in comments.

4 Comments

"In most cases, your life will be easier if you avoid protected fields." This may be true, but probably deserves more explanation.
@dnault - Added explanation. Thanks for the idea.
This is pretty much the conclusion I came to after a few minutes of fiddling with it. I kind of dislike the idea of having to set the values during creation of the object since I'm so used to redefining members in subclasses just overriding the values, but this seems to be what the object oriented paradigm calls for (I think C++ doesn't let you override either?). Thanks for the input.
In both Java and C++, alternatives include: 1) defining a protected or public setter method in the superclass, and 2) defining and consistently using a getter method [in C++, declared virtual], which can be overridden by a subclass.
3

@user2038045 - good question.

The source of your confusion is that in Java, a subclass can override methods ... but cannot override member variables.

If your class B had it's own "print()" method - you'd see the behavior you expected. B.print() would override A.print().

If your class B had any other method that accessed "value", you'd again see the behavior you expected. B.value would hide A.value, and B.someOtherMethod() would access B.value.

But in your case, you called A.print() ... which accesses A.value.

Here are more details:

Is there a way to override class variables in Java?

Comments

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.