Access modifiers and hiding Java fields?

You cannot make a field in another class go private by extending it. When you make a new field in a sub class you are just hiding the super class field.

Why the flexibility with java fields You cannot make a field in another class go private by extending it. When you make a new field in a sub class you are just hiding the super class field. Class Base { protected int x; } class Ext extends Base { private int x; // not the same as Base.

X }.

Of course you can make a field private. I think you mean you can't change the access of a field in the base class by providing a private field in the subclass... but I suggest you clarify your answer. – Jon Skeet Jul 28 '11 at 21:31 Ups, updated!

:) – dacwe Jul 28 '11 at 21:32.

You never override fields to start with - you're always hiding them. Fields aren't polymorphic... in other words, if you write: Superclass x = new Subclass(); System.out. Println(x.

Field); and both Superclass and Subclass declare a field called field, it will always use the superclass one anyway, because that's all the compiler can "see". Personally I try to keep my variables private anyway...

Which is one of the many reasons I love properties in . Net - they look and feel like field, but can still be polymorphic. – Ken Wayne VanderLinde Jul 28 '11 at 21:24.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions