Results 1 to 7 of 7
  1. #1
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Superclass and Inheritance question.

    A few weeks ago I began my endeavor of a open ended project for learning. However it stalled due to some circumstances and now I'm back at it.

    Details aside, I have a superclass with variables that are private. I also have my subclass which I want to inherit those variables and modify them.

    IE
    Java Code:
    public class Weapon
    {
         private string name;
    
         public void setName(newName)
         {
               name = newName;
         }
    
         public string getName()
         {
               return name;
          }
    }
    Java Code:
    public class Dagger extends Weapon
    {
     //Overriding code not related to question
         private string name = "Dagger";
    }
    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.
    • +Rep people for helpful posts.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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);
      }
    }

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.
    • +Rep people for helpful posts.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.
    Java Code:
    class Weapon{
    	String name;
    	public Weapon(String name){
    		this.name = name;
    	}
    	public Weapon(){
    	}
    }
    
    public class Dagger extends Weapon{
    	public Dagger(){
    		super("Dagger");
    	}
    	public Dagger(String name){
    		super(name);
    	}
    	public String toString(){
    		return name;
    	}
    	public static void main(String[] args){
    		Dagger d = new Dagger();
    		Dagger d1 = new Dagger("Curved Dagger");
    		System.out.println(d);
    		System.out.println(d1);
    		System.out.println(d.name);
    	}
    }

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Ok, so for every class that extends Weapon I would need something like that for each variable I want to change? I ask because I have approximately 20 variables that define each weapon.

    Also, what is this accomplishing?
    Java Code:
    public String toString(){
          return name;
    }
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Oh ok, yeah they haven't covered that yet.

    Thanks for the help, I will definitely try that in my code.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Superclass Question
    By Dark in forum New To Java
    Replies: 15
    Last Post: 04-12-2011, 11:40 AM
  2. Question about inheritance
    By hei1233212000 in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 11:38 AM
  3. Superclass question
    By keo in forum New To Java
    Replies: 6
    Last Post: 10-11-2010, 11:48 AM
  4. Generics & Inheritance Question
    By Lee Rhodes in forum Advanced Java
    Replies: 3
    Last Post: 07-03-2009, 05:04 AM
  5. a question about inheritance
    By litojs in forum New To Java
    Replies: 8
    Last Post: 11-10-2008, 10:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •