Archived

This topic is now archived and is closed to further replies.

[java] Inheriting variables with the same name? getting rid of Shadowing variables?

This topic is 5044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys! If I have a super class that has a variable with the same name as a class inheriting from it, is there any way of telling the super class to use the inherited class's variable? eg:
public class NewClass extends BaseClass {
	public NewClass() {
		
	}
	
	//This is the Class that I want to override

	protected class CreatorClass {
		public NewClass created = new NewClass();
	}
}

public class BaseClass {
	BaseClass() {
	}

	public CreatorClass getCreator() {
		return creator;
	}
	
	CreatorClass creator = new CreatorClass();
	
	//This is the Class that I want other classes to override

	protected class CreatorClass {
		public BaseClass created = new BaseClass();
	}
}

//And I want to be able to do like this:

BaseClass obj = NewClass();
//And a call to getCreator is supposed to return the

//new CreatorClass 

obj.getCreator();

So, basically I want the BaseClass to use the CreatorClass in the NewClass. And I understand that I would need to bypass the Variable Shawoding somehow, to let the variable work similar to method overriding? I would like to know if there is any way of achieving this? Thank you! [edited by - FxMazter on February 22, 2004 4:32:12 PM] [edited by - FxMazter on February 22, 2004 5:45:34 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
No... If you want overriding semantics, you need to use a method.

Share this post


Link to post
Share on other sites