The problem I'm coming across is that when I create a Dagger object it inherits the string name from Weapon, not Dagger. Which by default is null. My question is, how do I make it so that when I create a Dagger object it inherits all of the methods from Weapon, yet overrides the variables?

Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.

You generally don't override variables of the class, you add new variables. The idea of having weapon have a name and dagger have a name seems weird. Why do it? You can make each Dagger object have a different name through the constructor. If you want the extended class to have more details you create new variables. However; you can simply make the dagger class set the name of the name variable.

Java Code:

public class Dagger extends Weapon{
//inherits the variable name from Weapon
public Dagger(){ //default constructor sets name to "Dagger"
super("Dagger");
}
//other constructors can change the name to different types of daggers
public Dagger(String name){
super(name);
}
}

Hmm, I will try this out. The only thing I was trying to accomplish by putting private string name = "Dagger"; in the Dagger class was to set the name of the Dagger object to Dagger. The book I am reading did not explain the super(name); part of super and sub classes.

Will super(name) still work if all variables are private? It may seem like a silly question but I am curious. I'm about to search through the API for answers but any guidance definitely helps.

EDIT:

In my searching I cannot seem to find a good explanation of what happens with variables in super and sub classes. Do subclasses gain the variables from the superclass or do I have to directly change the superclass variable on my dagger object?

Last edited by Dark; 05-06-2011 at 06:03 AM.

Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.

It will work. What super(name) does is it calls the super classes constructor that takes a String argument. Private variables are inherited.

Think about it this way, instance variables represent state, methods represent behavior. It's fine to represent behavior because you may want to do different things, but the state is dependent on the object. It's impractical to override the instance variable of the class, why inherit something which is dependent on each object?

The method's of a class do the same thing per object. They each use the instance variables to do something. A toString method in a class does that same thing for the object, regardless of what the instance variables change. I hope this is making sense to you.

I wrote this code snippet before posting to make sure I don't make a fool of myself.

The toString method is overridden from the Object class. It makes t easier to print in my example, instead of d.name I can print with just d. You will see this soon I'd imagine in your book.

If you want 20 classes which all inherit from weapon they all should simply inherit the name from weapon. The subclasses should have a default(no arg constructor which sets a default name for the object.