Jump to content
  • Advertisement

Archived

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

FxMazter

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

This topic is 5350 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
Advertisement
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
That code is awfully convoluted. I''m not sure what exactly you are trying to achieve. Can you explain the class hierarchy you are looking for a little better?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!