Home arrow static arrow Java Programming [Archive] - Inherited Method Not Working
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Inherited Method Not Working
This topic has 5 replies on 1 page.

Posts:340
Registered: 7/8/04
Inherited Method Not Working  
Aug 6, 2004 10:15 AM



 
Hi,

I have a situation like this:
class Parent {	private int mInt;	public int getInt() { return mInt; }} class Child extends Parent {	private static final int mInt = 100;} public class Test {	public static void main(String args[]) {		Child child = new Child();		System.out.println(child.getInt());	}}

Running "java Test" outputs the value 0, when I want it to output 100. I'm guessing this has something to do with the fact that added the "static" modifier to mInt in the child class. But can anyone explain what is going on in more detail?

Also, I want some of the Parent's subclasses to be able to set "mInt" on a per object basis (non-static), but in some of the other subclasses mInt will be a static value. I was hoping to be able to do this without explicitly overiding getInt() in the static subclasses, which is the only workaround I've found so far.

Thanks for any ideas,
John
 

Posts:105
Registered: 6/8/04
Re: Inherited Method Not Working  
Aug 6, 2004 10:23 AM (reply 1 of 5)



 
I believe the problem is that mInt is private in the parent--subclasses only have access to public and protected fields.

If, however, there were a Parent.setInt() method, and you called that to set mInt at 100, it should output 100. Or just change mInt to protected rather than private.

As for your desire for a semi-static field, I don't think that can work simply by redeclaring the variable as static. However, if you aren't going to change mInt (as implied by your declaration of "final"), perhaps simply create a static variable mIntStatic, and set mInt equal to mIntStatic upon initialization?
 

Posts:24,036
Registered: 2/3/03
Re: Inherited Method Not Working  
Aug 6, 2004 10:36 AM (reply 2 of 5)



 
Running "java Test" outputs the value 0, when I want
it to output 100. I'm guessing this has something to
do with the fact that added the "static" modifier to
mInt in the child class. But can anyone explain what
is going on in more detail?

It has more to do with the members being private. Try naming the variable something completely different in the child class and see if that helps your understanding.
 

Posts:24,036
Registered: 2/3/03
Re: Inherited Method Not Working  
Aug 6, 2004 10:38 AM (reply 3 of 5)



 
Also, I want some of the Parent's subclasses to be
able to set "mInt" on a per object basis (non-static),
but in some of the other subclasses mInt will be a
static value.

What are you envisioning here? A static member is one that is pertinent to a class, not each object. There's no way to have a member be static for some subclasses and an instance variable for others.
 

Posts:21,718
Registered: 98-02-20
Re: Inherited Method Not Working  
Aug 6, 2004 10:40 AM (reply 4 of 5)



 
Maybe this will clarify:

class Parent{    private int mInt;    public int getInt()    {        System.out.print("from parent: ");        return mInt;    }} class Child extends Parent{    public static final int mInt = 100;} public class Test{    public static void main(String args[])    {        Child child = new Child();        System.out.println(child.getInt());        System.out.println("from child: " + Child.mInt);    }}


You aren't overriding the parent data member mInt when you declare a static variable of the same name in Child. One is associated with the instance of the Parent class, the other with the Child class.

%
 

Posts:340
Registered: 7/8/04
Re: Inherited Method Not Working  
Aug 6, 2004 10:49 AM (reply 5 of 5)



 
Thanks everyone for the explanations. Based on some other tests, I see that the Child really is creating it's own member mInt, rather than inheriting it. I think I'll just need to re-implement getInt() in child classes to get the optionally static behaviour I want. A pretty easy workaround, though.

Thanks again,
John
 
This topic has 5 replies on 1 page.