Question about Inherited member variables

M

marcwentink

To my surprise this seems possible in Java:

//-------------
public class Base {
protected int size = 100;
public int getSize() {
return size;
}
}

public class SubClass extends Base {
protected int size = 10;
public int getSize(){
return size;
}
public static void main(String[] args) {
Base b = new SubClass();
System.out.println (b.size + "," + b.getSize());
}
}
//--------------

Questions:

1. Why does the declaration of size not lead to a compile error in
SubClass, it seems to use a name already used.

But I could accept that since in an inner loop you can also declare a
variable with the same name as in the outer loop. I probably can
compare it to that, right?

2. Is the Base version of size still reachable in the Subclass by
another statement? Something like Super()->size perhaps?

Furthermore, I know this example should show me that overridden
functions are subject of polymorphism, but vmember ariables are not.
The outcome of the program is: 100, 10.
 
R

Robert Klemme

To my surprise this seems possible in Java:

//-------------
public class Base {
protected int size = 100;
public int getSize() {
return size;
}
}

public class SubClass extends Base {
protected int size = 10;
public int getSize(){
return size;
}
public static void main(String[] args) {
Base b = new SubClass();
System.out.println (b.size + "," + b.getSize());
}
}
//--------------

IMHO this is bad design anyway. Rather make the member private and
accessible via a getter only. If you want to prevent overriding you can
make getter and setter final.
Questions:

1. Why does the declaration of size not lead to a compile error in
SubClass, it seems to use a name already used.

It's in a different scope and depending on the compiler you use you
might be able to get him give you a warning about a hidden member.
But I could accept that since in an inner loop you can also declare a
variable with the same name as in the outer loop. I probably can
compare it to that, right?

Can you? I get an error for this

for ( int i = 0; i < args.length; ++i ) {
for ( int i = 0; i < args.length; ++i ) {
System.out.println( i );
}
}
2. Is the Base version of size still reachable in the Subclass by
another statement? Something like Super()->size perhaps?

public class SubClass extends Base {

protected int size = 10;

public int getSize() {
return size;
}

public int getSuperSize() {
return super.size;
}

public static void main( String[] args ) {
SubClass b = new SubClass();
System.out.println( b.size + "," + b.getSize() );
System.out.println( b.getSuperSize() );
}
}
Furthermore, I know this example should show me that overridden
functions are subject of polymorphism, but vmember ariables are not.

What exactly do you want to say with this?
The outcome of the program is: 100, 10.

Right.

Kind regards

robert
 
M

marcwentink

Robert Klemme schreef:
IMHO this is bad design anyway.

Agreed, it is just an exercise example.
Can you? I get an error for this
for ( int i = 0; i < args.length; ++i ) {
for ( int i = 0; i < args.length; ++i ) {
System.out.println( i );
}
}

I will try that. Perhaps this only goes for C++ then.

public int getSuperSize() {
return super.size;
}

Aha!

What exactly do you want to say with this?

Nothing much, just that my question is not about what the example
program tried to explain, I got that.
Kind regards

Hey thanks a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top