Thread View: ibm.software.vajava.language
6 messages
6 total messages
Started by Hannes Heckner
Tue, 29 Feb 2000 00:00
How to make a member variable of a derivation read-only?
Author: Hannes Heckner
Date: Tue, 29 Feb 2000 00:00
Date: Tue, 29 Feb 2000 00:00
52 lines
2133 bytes
2133 bytes
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++
Re: How to make a member variable of a derivation read-only?
Author: Jon Skeet
Date: Tue, 29 Feb 2000 00:00
Date: Tue, 29 Feb 2000 00:00
17 lines
1558 bytes
1558 bytes
Hannes.Heckner@munich.ixos.de wrote: > How can I make foo final in the derivation by OVERLOADING > foo from the Base class? You can't. Variables aren't overridden, they are merely shadowed. (Overloading is something different, by the way.) The Java Language Specification explains this in more detail. > If you know a solution to this problem, please submit > it to http://www.the-codebook.com/show_code.asp?ProblemId=1356 Post in news, expect answers in news... -- Jon Skeet - skeet@pobox.com http://www.pobox.com/~skeet/
Re: How to make a member variable of a derivation read-only?
Author: Tony Alicea
Date: Tue, 29 Feb 2000 00:00
Date: Tue, 29 Feb 2000 00:00
63 lines
3070 bytes
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++
Re: How to make a member variable of a derivation read-only?
Author: 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++
Re: How to make a member variable of a derivation read-only?
Author: Noelle Adam
Date: Mon, 06 Mar 2000 00:00
Date: Mon, 06 Mar 2000 00:00
30 lines
2139 bytes
2139 bytes
Jon Skeet wrote: > Hannes.Heckner@munich.ixos.de wrote: > > > How can I make foo final in the derivation by OVERLOADING > > foo from the Base class? > > You can't. Variables aren't overridden, they are merely shadowed. > (Overloading is something different, by the way.) The Java Language > Specification explains this in more detail. > Yes, what you can do is create an accessor method for a private variable and override it in the descendant class. > > > If you know a solution to this problem, please submit > > it to http://www.the-codebook.com/show_code.asp?ProblemId=1356 > > Post in news, expect answers in news... > > -- > Jon Skeet - skeet@pobox.com > http://www.pobox.com/~skeet/ -- IRISA-INRIA, Campus de Beaulieu, 35042 Rennes cedex, France TīŋŊl: +33 (0) 2 99 84 71 00, Fax: +33 (0) 2 99 84 71 71
Re: How to make a member variable of a derivation read-only?
Author: Jon Skeet
Date: Mon, 06 Mar 2000 00:00
Date: Mon, 06 Mar 2000 00:00
21 lines
1814 bytes
1814 bytes
Noelle.Adam@irisa.fr wrote: > > Hannes.Heckner@munich.ixos.de wrote: > > > > > How can I make foo final in the derivation by OVERLOADING > > > foo from the Base class? > > > > You can't. Variables aren't overridden, they are merely shadowed. > > (Overloading is something different, by the way.) The Java Language > > Specification explains this in more detail. > > Yes, what you can do is create an accessor method for a private variable > and override it in the descendant class. That's still not overloading foo. I may be a way round the problem, but the specific qusetion "How can I make foo final [...] by overloading foo [...]" still has the answer "You can't." -- Jon Skeet - skeet@pobox.com http://www.pobox.com/~skeet/
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads