Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Question about Inherited member variables

Thread view: 
marcwentink@hotmail.com - 24 Aug 2006 15:36 GMT
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.
Robert Klemme - 24 Aug 2006 16:18 GMT
> To my surprise this seems possible in Java:
>
[quoted text clipped - 17 lines]
>   }
> //--------------

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
marcwentink@hotmail.com - 24 Aug 2006 16:57 GMT
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!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.