Article View: ibm.software.vajava.language
Article #7Re: How to make a member variable of a derivation read-only?
From: Edward Kenworthy
Date: Thu, 02 Mar 2000 00:00
Date: Thu, 02 Mar 2000 00:00
87 lines
3464 bytes
3464 bytes
Hehe you can't and you shouldn't be able to.
Derivation inherits from Base and must, must, must fulfill the same
interface contract as Base, because by definition I must be able to
substitute an instance of Derivation for an instance of Base, anywhere I
wanted to and it must make sense and work. Making a member variable
read-only in Derivation that was writable in Base would break this.
To do what you "actually" want (isn't intuition wonderful) I expect you need
to reconsider your class hierarchy.
Perhaps something like:
public abstract class Parent
{
private int foo;
public int getFoo() {return foo;}
protected void setFoo(int argFoo) {this.foo = argFoo;}
}
public class ChildWithWritableFoo
{
public void setFoo(int argFoo) {super.setFoo(argFoo))
}
public class ChildWithReadOnlyFoo
{
}
Hannes Heckner <Hannes.Heckner@munich.ixos.de> wrote in message
news:951828737327@munich.ixos.de...
> 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:
<89mavf$bn4$1@news.software.ibm.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<951828737327@munich.ixos.de>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 100145,d86be700aa9afbe6 X-Google-Attributes: gid100145,public X-Google-Thread: 113ae9,d86be700aa9afbe6 X-Google-Attributes: gid113ae9,public X-Google-Thread: fbe9c,d86be700aa9afbe6 X-Google-Attributes: gidfbe9c,public X-Google-Thread: f74b3,d86be700aa9afbe6 X-Google-Attributes: gidf74b3,public X-Google-Thread: 10a146,d86be700aa9afbe6 X-Google-Attributes: gid10a146,public X-Google-Thread: 114aa0,d86be700aa9afbe6 X-Google-Attributes: gid114aa0,public From: "Edward Kenworthy" <edward.kenworthy@SPAM_ME_NOTcrispgroup.co.uk> Subject: Re: How to make a member variable of a derivation read-only? Date: 2000/03/02 Message-ID: <89mavf$bn4$1@news.software.ibm.com> X-Deja-AN: 592321291 References: <951828737327@munich.ixos.de> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Complaints-To: usenet@news.software.ibm.com X-Trace: news.software.ibm.com 952020783 12004 158.152.87.155 (2 Mar 2000 18:13:03 GMT) Organization: IBM Software Group X-MSMail-Priority: Normal NNTP-Posting-Date: 2 Mar 2000 18:13:03 GMT 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