🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: ibm.software.vajava.language
Article #6

Re: How to make a member variable of a derivation read-only?

#6
From: Tony Alicea
Date: Tue, 29 Feb 2000 00:00
63 lines
3070 bytes
First of all, you don't overload nor override instance variables, but
you *shadow* them instead. Variables are not treated as methods, which
take part in polymorphism AKA dynamic binding. The way you have it, only
the 'foo' at the base class will be seen by the doFoo() in said class.
You need to override the doFoo() in class Derivation to print its own
'foo'.

Tony Alicea


-------------------------
Hannes Heckner wrote:
> 
> Hi,
> 
> I tried to following example to have my member variable foo
> read-only in the derivation:
> 
> public class Class1
> {
>         /**
>          * The main entry point for the application.
>          *
>          * @param args Array of parameters passed to the application
>          * via the command line.
>          */
>         public static void main (String[] args)
>         {
>                 // TODO: Add initialization code here
>                 Base b = new Base();
>                 Derivation d = new Derivation();
>                 b.doFoo();
>                 d.doFoo();
>         }
> }
> 
> class Base
> {
>         protected int foo = 0;
> 
>         public void doFoo()
>         {
>                 System.out.println(""+foo);
>         }
> }
> 
> class Derivation extends Base
> {
>         final int foo = 1;
> }
> 
> Unfortunately it seems that the Derivation class DOES NOT
> overload foo! Therefore you will get the following output:
> 0
> 0
> 
> How can I make foo final in the derivation by OVERLOADING
> foo from the Base class?
> 
> If you know a solution to this problem, please submit it to http://www.the-codebook.com/show_code.asp?ProblemId=1356
> 
> +++++++++++ Posted via The-Codebook.Com, The Source for Developers +++++++++++
> http://www.The-Codebook.com/                      Java, VisualBasic, VisualC++
Technical Details
Message-ID: <38BBE6C5.4FACA11@alicea.org>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <951828737327@munich.ixos.de>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fbe9c,d86be700aa9afbe6
X-Google-Attributes: gidfbe9c,public
X-Google-Thread: 100145,d86be700aa9afbe6
X-Google-Attributes: gid100145,public
X-Google-Thread: f74b3,d86be700aa9afbe6
X-Google-Attributes: gidf74b3,public
X-Google-Thread: 113ae9,d86be700aa9afbe6
X-Google-Attributes: gid113ae9,public
X-Google-Thread: 114aa0,d86be700aa9afbe6
X-Google-Attributes: gid114aa0,public
X-Google-Thread: 10a146,d86be700aa9afbe6
X-Google-Attributes: gid10a146,public
From: Tony Alicea <tony@alicea.org>
Subject: Re: How to make a member variable of a derivation read-only?
Date: 2000/02/29
Message-ID: <38BBE6C5.4FACA11@alicea.org>
X-Deja-AN: 591337008
Content-Transfer-Encoding: 7bit
References: <951828737327@munich.ixos.de>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
X-Trace: news4.mco 951838375 216.78.234.58 (Tue, 29 Feb 2000 10:32:55 EST)
Organization: Software Engineer
MIME-Version: 1.0
NNTP-Posting-Date: Tue, 29 Feb 2000 10:32:55 EST
Newsgroups: ibm.software.vajava.language,borland.public.jbuilder.java.language,borland.public.jbuilder.java,microsoft.public.java.visualj++,comp.lang.java.help,comp.lang.java.programmer