I'm having problems fixing my “getters” and “setters” in inheritance












0














Please bear with me, this is my first time dealing with inheritance in Java. :)



I have the following superclass which identifies ships and returns a name of the ship and its length:



public class Ship {

private String name="";
private int length=0;

public Ship(String name, int length) {
this.setName(name);
this.length = length;
}

public static String getName() {
return name;
}

public void setName(String name) {
Ship.name = name;
}

public String toString() {
return " Ship "+name+" l="+ length ;
}

}


This is done by creating an object such as:



Ship SSAnne = new Ship("S.S. Anne", 45);


Then I have "sub-classes" that extend from my superclass which define objects Container Ships, Tankers, and Cruise Ships. Here is my sub-class for container ships (the other types of ships are identical, except for different variable names):



public class ContainerShip extends Ship {

private int teu=0;

public ContainerShip(String string, int length, int teu) {
super(getName(), length);
this.teu = teu;
}

public void setName(String name) {
ContainerShip.name = name;
}

public String toString() {
return super.toString()+" container carrier with "+teu+" TEU";
}

}


These sub-classes are called like this:



ContainerShip dave = new ContainerShip("Dave Oest", 100, 2000);


Where an extra variable is added and the first two arguments are the same variables from the superclass.



My issue is with my getters and setters for the "name" variable. When I run my code, it generates the "S.S. Anne" name for all objects, even the objects from the sub-classes. I can't seem to get it to "get" the name of the ships for my sub-classes.



Can anybody help me how to properly set up the getters and setters in my sub-classes?





@GBlodgett:



public class Ship {

private String name="";
private int length=0;

public Ship(String name, int length) {
this.setName(name);
this.length = length;
}

public void getName(String name) {
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return " Ship "+name+" l="+ length ;
}

}

public class ContainerShip extends Ship {

private int teu=0;

public ContainerShip(String string, int length, int teu) {
super(getName(), length);
this.teu = teu;
}

public void getName(String name) {
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return super.toString()+" container carrier with "+teu+" TEU";
}

}









share|improve this question
























  • You should have some warnings and error on your code to tell you about
    – azro
    Nov 20 at 19:29
















0














Please bear with me, this is my first time dealing with inheritance in Java. :)



I have the following superclass which identifies ships and returns a name of the ship and its length:



public class Ship {

private String name="";
private int length=0;

public Ship(String name, int length) {
this.setName(name);
this.length = length;
}

public static String getName() {
return name;
}

public void setName(String name) {
Ship.name = name;
}

public String toString() {
return " Ship "+name+" l="+ length ;
}

}


This is done by creating an object such as:



Ship SSAnne = new Ship("S.S. Anne", 45);


Then I have "sub-classes" that extend from my superclass which define objects Container Ships, Tankers, and Cruise Ships. Here is my sub-class for container ships (the other types of ships are identical, except for different variable names):



public class ContainerShip extends Ship {

private int teu=0;

public ContainerShip(String string, int length, int teu) {
super(getName(), length);
this.teu = teu;
}

public void setName(String name) {
ContainerShip.name = name;
}

public String toString() {
return super.toString()+" container carrier with "+teu+" TEU";
}

}


These sub-classes are called like this:



ContainerShip dave = new ContainerShip("Dave Oest", 100, 2000);


Where an extra variable is added and the first two arguments are the same variables from the superclass.



My issue is with my getters and setters for the "name" variable. When I run my code, it generates the "S.S. Anne" name for all objects, even the objects from the sub-classes. I can't seem to get it to "get" the name of the ships for my sub-classes.



Can anybody help me how to properly set up the getters and setters in my sub-classes?





@GBlodgett:



public class Ship {

private String name="";
private int length=0;

public Ship(String name, int length) {
this.setName(name);
this.length = length;
}

public void getName(String name) {
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return " Ship "+name+" l="+ length ;
}

}

public class ContainerShip extends Ship {

private int teu=0;

public ContainerShip(String string, int length, int teu) {
super(getName(), length);
this.teu = teu;
}

public void getName(String name) {
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return super.toString()+" container carrier with "+teu+" TEU";
}

}









share|improve this question
























  • You should have some warnings and error on your code to tell you about
    – azro
    Nov 20 at 19:29














0












0








0







Please bear with me, this is my first time dealing with inheritance in Java. :)



I have the following superclass which identifies ships and returns a name of the ship and its length:



public class Ship {

private String name="";
private int length=0;

public Ship(String name, int length) {
this.setName(name);
this.length = length;
}

public static String getName() {
return name;
}

public void setName(String name) {
Ship.name = name;
}

public String toString() {
return " Ship "+name+" l="+ length ;
}

}


This is done by creating an object such as:



Ship SSAnne = new Ship("S.S. Anne", 45);


Then I have "sub-classes" that extend from my superclass which define objects Container Ships, Tankers, and Cruise Ships. Here is my sub-class for container ships (the other types of ships are identical, except for different variable names):



public class ContainerShip extends Ship {

private int teu=0;

public ContainerShip(String string, int length, int teu) {
super(getName(), length);
this.teu = teu;
}

public void setName(String name) {
ContainerShip.name = name;
}

public String toString() {
return super.toString()+" container carrier with "+teu+" TEU";
}

}


These sub-classes are called like this:



ContainerShip dave = new ContainerShip("Dave Oest", 100, 2000);


Where an extra variable is added and the first two arguments are the same variables from the superclass.



My issue is with my getters and setters for the "name" variable. When I run my code, it generates the "S.S. Anne" name for all objects, even the objects from the sub-classes. I can't seem to get it to "get" the name of the ships for my sub-classes.



Can anybody help me how to properly set up the getters and setters in my sub-classes?





@GBlodgett:



public class Ship {

private String name="";
private int length=0;

public Ship(String name, int length) {
this.setName(name);
this.length = length;
}

public void getName(String name) {
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return " Ship "+name+" l="+ length ;
}

}

public class ContainerShip extends Ship {

private int teu=0;

public ContainerShip(String string, int length, int teu) {
super(getName(), length);
this.teu = teu;
}

public void getName(String name) {
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return super.toString()+" container carrier with "+teu+" TEU";
}

}









share|improve this question















Please bear with me, this is my first time dealing with inheritance in Java. :)



I have the following superclass which identifies ships and returns a name of the ship and its length:



public class Ship {

private String name="";
private int length=0;

public Ship(String name, int length) {
this.setName(name);
this.length = length;
}

public static String getName() {
return name;
}

public void setName(String name) {
Ship.name = name;
}

public String toString() {
return " Ship "+name+" l="+ length ;
}

}


This is done by creating an object such as:



Ship SSAnne = new Ship("S.S. Anne", 45);


Then I have "sub-classes" that extend from my superclass which define objects Container Ships, Tankers, and Cruise Ships. Here is my sub-class for container ships (the other types of ships are identical, except for different variable names):



public class ContainerShip extends Ship {

private int teu=0;

public ContainerShip(String string, int length, int teu) {
super(getName(), length);
this.teu = teu;
}

public void setName(String name) {
ContainerShip.name = name;
}

public String toString() {
return super.toString()+" container carrier with "+teu+" TEU";
}

}


These sub-classes are called like this:



ContainerShip dave = new ContainerShip("Dave Oest", 100, 2000);


Where an extra variable is added and the first two arguments are the same variables from the superclass.



My issue is with my getters and setters for the "name" variable. When I run my code, it generates the "S.S. Anne" name for all objects, even the objects from the sub-classes. I can't seem to get it to "get" the name of the ships for my sub-classes.



Can anybody help me how to properly set up the getters and setters in my sub-classes?





@GBlodgett:



public class Ship {

private String name="";
private int length=0;

public Ship(String name, int length) {
this.setName(name);
this.length = length;
}

public void getName(String name) {
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return " Ship "+name+" l="+ length ;
}

}

public class ContainerShip extends Ship {

private int teu=0;

public ContainerShip(String string, int length, int teu) {
super(getName(), length);
this.teu = teu;
}

public void getName(String name) {
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return super.toString()+" container carrier with "+teu+" TEU";
}

}






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 19:39

























asked Nov 20 at 19:23









WoeIs

47010




47010












  • You should have some warnings and error on your code to tell you about
    – azro
    Nov 20 at 19:29


















  • You should have some warnings and error on your code to tell you about
    – azro
    Nov 20 at 19:29
















You should have some warnings and error on your code to tell you about
– azro
Nov 20 at 19:29




You should have some warnings and error on your code to tell you about
– azro
Nov 20 at 19:29












4 Answers
4






active

oldest

votes


















3














You have declared name as static, which means there's only one copy shared among all instances of the class. You want to remove the static keyword, and then in your getters and setters, use the this keyword, which refers to the object the method is invoked on:



 public void setName(String name) {
this.name = name;
}


In which case the setName method will be the same in both classes and you will not need to override it in the subclass





The first problem with your updated code is that you cannot refer to an instance method while explicitly invoking a constructor. Instead of calling getName(), pass the String argument you pass into the constructor:



public SubClass(String string, int length, int teu) {
super(string, length);
this.teu = teu;
}


Then in your getName() method, you are treating it as a setter. You should instead return the name field:



public String getName() {
return this.name;
}


This leads to your last problem: You are using the superclass copy of name. Because you marked it as private, it will not be accessible to other classes. You can either give ContainerShip it's own name variable, or make the name in Ship protected, or use super again like:



public void setName(String name) {
super.setName(name);
}





share|improve this answer























  • Thanks for the comment! I changed the declaration to private String name=""; but I'm having a bit difficulty figuring out where to put my "this" keyword. It seems to give me an error "The field Ship.name is not visible" when I add the keyword. Am I supposed to do it in both my superclass and subclass?
    – WoeIs
    Nov 20 at 19:35










  • @WoeIs Where are you adding the this keyword? Are you doing it like posted in my answer?
    – GBlodgett
    Nov 20 at 19:36










  • I edited my main post and pasted my updated code below. I'm getting the error in my sub-class.
    – WoeIs
    Nov 20 at 19:39










  • @WoeIs What errors are you getting?
    – GBlodgett
    Nov 20 at 19:42






  • 1




    Thank you for the update. I finally managed to get it to work, and you were correct in your suggestions. I noticed however that the getters and setters in my subclass were redundant, so when I removed them from my subclass and only kept the getters and setters in my superclass, then it was able to still retrieve the names and also keep the declaration as private. Thank you again for the help!
    – WoeIs
    Nov 20 at 20:01



















1














You can't access instance variables from static methods,



private String name="";
private int length=0;


These are instance variables, where as your method is declared as static



public static String getName() {


One key thing you should always remember while implementing inheritance.



Do not use static word with variable declaration or use static word in method declaration.



Inheritance only happens when we deal everything with instance variables and instance (non-static) methods. So you will have to change couple of things,



Change this,



public static String getName() {


to,



public String getName() {


Similarly, you can't access instance variables with class name and for referring to the current object, you can use this. Hence change,



public void setName(String name) {
Ship.name = name;
}


to



public void setName(String name) {
this.name = name;
}


Also, in this constructor you have a problem,



public ContainerShip(String string, int length, int teu) {
super(getName(), length);
this.teu = teu;
}


You aren't using string variable that is your first parameter. You need to change this super class from this,



super(getName(), length);


to,



super(string, length);


This way, whatever was passed to constructor, will get passed to constructor of super class.



You should fix these problems first.



Also, once you create an object using ContainerShip class, it will have access to getter/setter methods from parent class Ship as your sub class will inherit those methods which is called inheritance. Its a broad subject and as you proceed, you will know more such constructs. Let me know if you further have any query regarding inheritance.



As you have declared your instance variables in super class as private, your instance variables aren't visible directly to sub classes, and only available through public getter/setter, but in case you want to directly access them, you can declare them as protected and can directly access them. Inheritance is the reason you can access variables from super class in sub class provided they are not declared as private. Private means only accessible in its own class.






share|improve this answer





















  • Thank you for your answer. I removed all static keywords and added the "string" argument to my superclass method. When I changed private to proteced in my declaration, it appears to now work. However, is there not any solution where I can declare the "name" variable as private? Do I need to declare it private for all my subclasses?
    – WoeIs
    Nov 20 at 19:55










  • No, no, you can declare instance variables as private but then they won't be directly accessible to sub classes and still you can make them available by public getter/setters.
    – Pushpesh Kumar Rajwanshi
    Nov 20 at 20:02



















0














You can use Interfaces to define Type and Abstract classes to define default behavior. For instance:



interface Ship {
String getName();
void setName();
}

abstract class DefaultShip implements Ship {
private int length;

public DefaultShip(int length) {
this.length = length;
}

public String toString() {
return " Ship " + getName() + " l=" + this.length;
}
}

public class ContainerShip extends DefaultShip {

private int teu;
private String name;

public ContainerShip(String name, int length, int teu) {
super(length);
this.name = name;
this.teu = teu;
}

public void getName(String name) {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return super.toString() +" container carrier with " + this.teu + " TEU";
}
}





share|improve this answer





























    0














    You dont need setters and getters in the sub-class for name and length values.
    Only if you want to override them.



    Ship superclass



    public class Ship {

    private String name;
    private int length;

    public Ship(String name, int length) {
    this.name = name;
    this.length = length;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String toString() {
    return " Ship "+name+" l="+ length ;
    }

    }


    ContainerShip subclass



    public class ContainerShip extends Ship {

    private int teu;

    public ContainerShip(String string, int length, int teu) {
    super(string, length);
    this.teu = teu;
    }

    public String toString() {
    return super.toString()+" container carrier with "+teu+" TEU";
    }

    }


    Main method



    public static void main(String args) {

    ContainerShip cont = new ContainerShip("Name", 100, 4);
    System.out.println(cont.getName());
    cont.setName("new name");
    System.out.println(cont.getName());
    }


    OUTPUT:




    Name



    new name







    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400123%2fim-having-problems-fixing-my-getters-and-setters-in-inheritance%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      You have declared name as static, which means there's only one copy shared among all instances of the class. You want to remove the static keyword, and then in your getters and setters, use the this keyword, which refers to the object the method is invoked on:



       public void setName(String name) {
      this.name = name;
      }


      In which case the setName method will be the same in both classes and you will not need to override it in the subclass





      The first problem with your updated code is that you cannot refer to an instance method while explicitly invoking a constructor. Instead of calling getName(), pass the String argument you pass into the constructor:



      public SubClass(String string, int length, int teu) {
      super(string, length);
      this.teu = teu;
      }


      Then in your getName() method, you are treating it as a setter. You should instead return the name field:



      public String getName() {
      return this.name;
      }


      This leads to your last problem: You are using the superclass copy of name. Because you marked it as private, it will not be accessible to other classes. You can either give ContainerShip it's own name variable, or make the name in Ship protected, or use super again like:



      public void setName(String name) {
      super.setName(name);
      }





      share|improve this answer























      • Thanks for the comment! I changed the declaration to private String name=""; but I'm having a bit difficulty figuring out where to put my "this" keyword. It seems to give me an error "The field Ship.name is not visible" when I add the keyword. Am I supposed to do it in both my superclass and subclass?
        – WoeIs
        Nov 20 at 19:35










      • @WoeIs Where are you adding the this keyword? Are you doing it like posted in my answer?
        – GBlodgett
        Nov 20 at 19:36










      • I edited my main post and pasted my updated code below. I'm getting the error in my sub-class.
        – WoeIs
        Nov 20 at 19:39










      • @WoeIs What errors are you getting?
        – GBlodgett
        Nov 20 at 19:42






      • 1




        Thank you for the update. I finally managed to get it to work, and you were correct in your suggestions. I noticed however that the getters and setters in my subclass were redundant, so when I removed them from my subclass and only kept the getters and setters in my superclass, then it was able to still retrieve the names and also keep the declaration as private. Thank you again for the help!
        – WoeIs
        Nov 20 at 20:01
















      3














      You have declared name as static, which means there's only one copy shared among all instances of the class. You want to remove the static keyword, and then in your getters and setters, use the this keyword, which refers to the object the method is invoked on:



       public void setName(String name) {
      this.name = name;
      }


      In which case the setName method will be the same in both classes and you will not need to override it in the subclass





      The first problem with your updated code is that you cannot refer to an instance method while explicitly invoking a constructor. Instead of calling getName(), pass the String argument you pass into the constructor:



      public SubClass(String string, int length, int teu) {
      super(string, length);
      this.teu = teu;
      }


      Then in your getName() method, you are treating it as a setter. You should instead return the name field:



      public String getName() {
      return this.name;
      }


      This leads to your last problem: You are using the superclass copy of name. Because you marked it as private, it will not be accessible to other classes. You can either give ContainerShip it's own name variable, or make the name in Ship protected, or use super again like:



      public void setName(String name) {
      super.setName(name);
      }





      share|improve this answer























      • Thanks for the comment! I changed the declaration to private String name=""; but I'm having a bit difficulty figuring out where to put my "this" keyword. It seems to give me an error "The field Ship.name is not visible" when I add the keyword. Am I supposed to do it in both my superclass and subclass?
        – WoeIs
        Nov 20 at 19:35










      • @WoeIs Where are you adding the this keyword? Are you doing it like posted in my answer?
        – GBlodgett
        Nov 20 at 19:36










      • I edited my main post and pasted my updated code below. I'm getting the error in my sub-class.
        – WoeIs
        Nov 20 at 19:39










      • @WoeIs What errors are you getting?
        – GBlodgett
        Nov 20 at 19:42






      • 1




        Thank you for the update. I finally managed to get it to work, and you were correct in your suggestions. I noticed however that the getters and setters in my subclass were redundant, so when I removed them from my subclass and only kept the getters and setters in my superclass, then it was able to still retrieve the names and also keep the declaration as private. Thank you again for the help!
        – WoeIs
        Nov 20 at 20:01














      3












      3








      3






      You have declared name as static, which means there's only one copy shared among all instances of the class. You want to remove the static keyword, and then in your getters and setters, use the this keyword, which refers to the object the method is invoked on:



       public void setName(String name) {
      this.name = name;
      }


      In which case the setName method will be the same in both classes and you will not need to override it in the subclass





      The first problem with your updated code is that you cannot refer to an instance method while explicitly invoking a constructor. Instead of calling getName(), pass the String argument you pass into the constructor:



      public SubClass(String string, int length, int teu) {
      super(string, length);
      this.teu = teu;
      }


      Then in your getName() method, you are treating it as a setter. You should instead return the name field:



      public String getName() {
      return this.name;
      }


      This leads to your last problem: You are using the superclass copy of name. Because you marked it as private, it will not be accessible to other classes. You can either give ContainerShip it's own name variable, or make the name in Ship protected, or use super again like:



      public void setName(String name) {
      super.setName(name);
      }





      share|improve this answer














      You have declared name as static, which means there's only one copy shared among all instances of the class. You want to remove the static keyword, and then in your getters and setters, use the this keyword, which refers to the object the method is invoked on:



       public void setName(String name) {
      this.name = name;
      }


      In which case the setName method will be the same in both classes and you will not need to override it in the subclass





      The first problem with your updated code is that you cannot refer to an instance method while explicitly invoking a constructor. Instead of calling getName(), pass the String argument you pass into the constructor:



      public SubClass(String string, int length, int teu) {
      super(string, length);
      this.teu = teu;
      }


      Then in your getName() method, you are treating it as a setter. You should instead return the name field:



      public String getName() {
      return this.name;
      }


      This leads to your last problem: You are using the superclass copy of name. Because you marked it as private, it will not be accessible to other classes. You can either give ContainerShip it's own name variable, or make the name in Ship protected, or use super again like:



      public void setName(String name) {
      super.setName(name);
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 20 at 19:57

























      answered Nov 20 at 19:25









      GBlodgett

      8,79341531




      8,79341531












      • Thanks for the comment! I changed the declaration to private String name=""; but I'm having a bit difficulty figuring out where to put my "this" keyword. It seems to give me an error "The field Ship.name is not visible" when I add the keyword. Am I supposed to do it in both my superclass and subclass?
        – WoeIs
        Nov 20 at 19:35










      • @WoeIs Where are you adding the this keyword? Are you doing it like posted in my answer?
        – GBlodgett
        Nov 20 at 19:36










      • I edited my main post and pasted my updated code below. I'm getting the error in my sub-class.
        – WoeIs
        Nov 20 at 19:39










      • @WoeIs What errors are you getting?
        – GBlodgett
        Nov 20 at 19:42






      • 1




        Thank you for the update. I finally managed to get it to work, and you were correct in your suggestions. I noticed however that the getters and setters in my subclass were redundant, so when I removed them from my subclass and only kept the getters and setters in my superclass, then it was able to still retrieve the names and also keep the declaration as private. Thank you again for the help!
        – WoeIs
        Nov 20 at 20:01


















      • Thanks for the comment! I changed the declaration to private String name=""; but I'm having a bit difficulty figuring out where to put my "this" keyword. It seems to give me an error "The field Ship.name is not visible" when I add the keyword. Am I supposed to do it in both my superclass and subclass?
        – WoeIs
        Nov 20 at 19:35










      • @WoeIs Where are you adding the this keyword? Are you doing it like posted in my answer?
        – GBlodgett
        Nov 20 at 19:36










      • I edited my main post and pasted my updated code below. I'm getting the error in my sub-class.
        – WoeIs
        Nov 20 at 19:39










      • @WoeIs What errors are you getting?
        – GBlodgett
        Nov 20 at 19:42






      • 1




        Thank you for the update. I finally managed to get it to work, and you were correct in your suggestions. I noticed however that the getters and setters in my subclass were redundant, so when I removed them from my subclass and only kept the getters and setters in my superclass, then it was able to still retrieve the names and also keep the declaration as private. Thank you again for the help!
        – WoeIs
        Nov 20 at 20:01
















      Thanks for the comment! I changed the declaration to private String name=""; but I'm having a bit difficulty figuring out where to put my "this" keyword. It seems to give me an error "The field Ship.name is not visible" when I add the keyword. Am I supposed to do it in both my superclass and subclass?
      – WoeIs
      Nov 20 at 19:35




      Thanks for the comment! I changed the declaration to private String name=""; but I'm having a bit difficulty figuring out where to put my "this" keyword. It seems to give me an error "The field Ship.name is not visible" when I add the keyword. Am I supposed to do it in both my superclass and subclass?
      – WoeIs
      Nov 20 at 19:35












      @WoeIs Where are you adding the this keyword? Are you doing it like posted in my answer?
      – GBlodgett
      Nov 20 at 19:36




      @WoeIs Where are you adding the this keyword? Are you doing it like posted in my answer?
      – GBlodgett
      Nov 20 at 19:36












      I edited my main post and pasted my updated code below. I'm getting the error in my sub-class.
      – WoeIs
      Nov 20 at 19:39




      I edited my main post and pasted my updated code below. I'm getting the error in my sub-class.
      – WoeIs
      Nov 20 at 19:39












      @WoeIs What errors are you getting?
      – GBlodgett
      Nov 20 at 19:42




      @WoeIs What errors are you getting?
      – GBlodgett
      Nov 20 at 19:42




      1




      1




      Thank you for the update. I finally managed to get it to work, and you were correct in your suggestions. I noticed however that the getters and setters in my subclass were redundant, so when I removed them from my subclass and only kept the getters and setters in my superclass, then it was able to still retrieve the names and also keep the declaration as private. Thank you again for the help!
      – WoeIs
      Nov 20 at 20:01




      Thank you for the update. I finally managed to get it to work, and you were correct in your suggestions. I noticed however that the getters and setters in my subclass were redundant, so when I removed them from my subclass and only kept the getters and setters in my superclass, then it was able to still retrieve the names and also keep the declaration as private. Thank you again for the help!
      – WoeIs
      Nov 20 at 20:01













      1














      You can't access instance variables from static methods,



      private String name="";
      private int length=0;


      These are instance variables, where as your method is declared as static



      public static String getName() {


      One key thing you should always remember while implementing inheritance.



      Do not use static word with variable declaration or use static word in method declaration.



      Inheritance only happens when we deal everything with instance variables and instance (non-static) methods. So you will have to change couple of things,



      Change this,



      public static String getName() {


      to,



      public String getName() {


      Similarly, you can't access instance variables with class name and for referring to the current object, you can use this. Hence change,



      public void setName(String name) {
      Ship.name = name;
      }


      to



      public void setName(String name) {
      this.name = name;
      }


      Also, in this constructor you have a problem,



      public ContainerShip(String string, int length, int teu) {
      super(getName(), length);
      this.teu = teu;
      }


      You aren't using string variable that is your first parameter. You need to change this super class from this,



      super(getName(), length);


      to,



      super(string, length);


      This way, whatever was passed to constructor, will get passed to constructor of super class.



      You should fix these problems first.



      Also, once you create an object using ContainerShip class, it will have access to getter/setter methods from parent class Ship as your sub class will inherit those methods which is called inheritance. Its a broad subject and as you proceed, you will know more such constructs. Let me know if you further have any query regarding inheritance.



      As you have declared your instance variables in super class as private, your instance variables aren't visible directly to sub classes, and only available through public getter/setter, but in case you want to directly access them, you can declare them as protected and can directly access them. Inheritance is the reason you can access variables from super class in sub class provided they are not declared as private. Private means only accessible in its own class.






      share|improve this answer





















      • Thank you for your answer. I removed all static keywords and added the "string" argument to my superclass method. When I changed private to proteced in my declaration, it appears to now work. However, is there not any solution where I can declare the "name" variable as private? Do I need to declare it private for all my subclasses?
        – WoeIs
        Nov 20 at 19:55










      • No, no, you can declare instance variables as private but then they won't be directly accessible to sub classes and still you can make them available by public getter/setters.
        – Pushpesh Kumar Rajwanshi
        Nov 20 at 20:02
















      1














      You can't access instance variables from static methods,



      private String name="";
      private int length=0;


      These are instance variables, where as your method is declared as static



      public static String getName() {


      One key thing you should always remember while implementing inheritance.



      Do not use static word with variable declaration or use static word in method declaration.



      Inheritance only happens when we deal everything with instance variables and instance (non-static) methods. So you will have to change couple of things,



      Change this,



      public static String getName() {


      to,



      public String getName() {


      Similarly, you can't access instance variables with class name and for referring to the current object, you can use this. Hence change,



      public void setName(String name) {
      Ship.name = name;
      }


      to



      public void setName(String name) {
      this.name = name;
      }


      Also, in this constructor you have a problem,



      public ContainerShip(String string, int length, int teu) {
      super(getName(), length);
      this.teu = teu;
      }


      You aren't using string variable that is your first parameter. You need to change this super class from this,



      super(getName(), length);


      to,



      super(string, length);


      This way, whatever was passed to constructor, will get passed to constructor of super class.



      You should fix these problems first.



      Also, once you create an object using ContainerShip class, it will have access to getter/setter methods from parent class Ship as your sub class will inherit those methods which is called inheritance. Its a broad subject and as you proceed, you will know more such constructs. Let me know if you further have any query regarding inheritance.



      As you have declared your instance variables in super class as private, your instance variables aren't visible directly to sub classes, and only available through public getter/setter, but in case you want to directly access them, you can declare them as protected and can directly access them. Inheritance is the reason you can access variables from super class in sub class provided they are not declared as private. Private means only accessible in its own class.






      share|improve this answer





















      • Thank you for your answer. I removed all static keywords and added the "string" argument to my superclass method. When I changed private to proteced in my declaration, it appears to now work. However, is there not any solution where I can declare the "name" variable as private? Do I need to declare it private for all my subclasses?
        – WoeIs
        Nov 20 at 19:55










      • No, no, you can declare instance variables as private but then they won't be directly accessible to sub classes and still you can make them available by public getter/setters.
        – Pushpesh Kumar Rajwanshi
        Nov 20 at 20:02














      1












      1








      1






      You can't access instance variables from static methods,



      private String name="";
      private int length=0;


      These are instance variables, where as your method is declared as static



      public static String getName() {


      One key thing you should always remember while implementing inheritance.



      Do not use static word with variable declaration or use static word in method declaration.



      Inheritance only happens when we deal everything with instance variables and instance (non-static) methods. So you will have to change couple of things,



      Change this,



      public static String getName() {


      to,



      public String getName() {


      Similarly, you can't access instance variables with class name and for referring to the current object, you can use this. Hence change,



      public void setName(String name) {
      Ship.name = name;
      }


      to



      public void setName(String name) {
      this.name = name;
      }


      Also, in this constructor you have a problem,



      public ContainerShip(String string, int length, int teu) {
      super(getName(), length);
      this.teu = teu;
      }


      You aren't using string variable that is your first parameter. You need to change this super class from this,



      super(getName(), length);


      to,



      super(string, length);


      This way, whatever was passed to constructor, will get passed to constructor of super class.



      You should fix these problems first.



      Also, once you create an object using ContainerShip class, it will have access to getter/setter methods from parent class Ship as your sub class will inherit those methods which is called inheritance. Its a broad subject and as you proceed, you will know more such constructs. Let me know if you further have any query regarding inheritance.



      As you have declared your instance variables in super class as private, your instance variables aren't visible directly to sub classes, and only available through public getter/setter, but in case you want to directly access them, you can declare them as protected and can directly access them. Inheritance is the reason you can access variables from super class in sub class provided they are not declared as private. Private means only accessible in its own class.






      share|improve this answer












      You can't access instance variables from static methods,



      private String name="";
      private int length=0;


      These are instance variables, where as your method is declared as static



      public static String getName() {


      One key thing you should always remember while implementing inheritance.



      Do not use static word with variable declaration or use static word in method declaration.



      Inheritance only happens when we deal everything with instance variables and instance (non-static) methods. So you will have to change couple of things,



      Change this,



      public static String getName() {


      to,



      public String getName() {


      Similarly, you can't access instance variables with class name and for referring to the current object, you can use this. Hence change,



      public void setName(String name) {
      Ship.name = name;
      }


      to



      public void setName(String name) {
      this.name = name;
      }


      Also, in this constructor you have a problem,



      public ContainerShip(String string, int length, int teu) {
      super(getName(), length);
      this.teu = teu;
      }


      You aren't using string variable that is your first parameter. You need to change this super class from this,



      super(getName(), length);


      to,



      super(string, length);


      This way, whatever was passed to constructor, will get passed to constructor of super class.



      You should fix these problems first.



      Also, once you create an object using ContainerShip class, it will have access to getter/setter methods from parent class Ship as your sub class will inherit those methods which is called inheritance. Its a broad subject and as you proceed, you will know more such constructs. Let me know if you further have any query regarding inheritance.



      As you have declared your instance variables in super class as private, your instance variables aren't visible directly to sub classes, and only available through public getter/setter, but in case you want to directly access them, you can declare them as protected and can directly access them. Inheritance is the reason you can access variables from super class in sub class provided they are not declared as private. Private means only accessible in its own class.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 20 at 19:45









      Pushpesh Kumar Rajwanshi

      4,8141826




      4,8141826












      • Thank you for your answer. I removed all static keywords and added the "string" argument to my superclass method. When I changed private to proteced in my declaration, it appears to now work. However, is there not any solution where I can declare the "name" variable as private? Do I need to declare it private for all my subclasses?
        – WoeIs
        Nov 20 at 19:55










      • No, no, you can declare instance variables as private but then they won't be directly accessible to sub classes and still you can make them available by public getter/setters.
        – Pushpesh Kumar Rajwanshi
        Nov 20 at 20:02


















      • Thank you for your answer. I removed all static keywords and added the "string" argument to my superclass method. When I changed private to proteced in my declaration, it appears to now work. However, is there not any solution where I can declare the "name" variable as private? Do I need to declare it private for all my subclasses?
        – WoeIs
        Nov 20 at 19:55










      • No, no, you can declare instance variables as private but then they won't be directly accessible to sub classes and still you can make them available by public getter/setters.
        – Pushpesh Kumar Rajwanshi
        Nov 20 at 20:02
















      Thank you for your answer. I removed all static keywords and added the "string" argument to my superclass method. When I changed private to proteced in my declaration, it appears to now work. However, is there not any solution where I can declare the "name" variable as private? Do I need to declare it private for all my subclasses?
      – WoeIs
      Nov 20 at 19:55




      Thank you for your answer. I removed all static keywords and added the "string" argument to my superclass method. When I changed private to proteced in my declaration, it appears to now work. However, is there not any solution where I can declare the "name" variable as private? Do I need to declare it private for all my subclasses?
      – WoeIs
      Nov 20 at 19:55












      No, no, you can declare instance variables as private but then they won't be directly accessible to sub classes and still you can make them available by public getter/setters.
      – Pushpesh Kumar Rajwanshi
      Nov 20 at 20:02




      No, no, you can declare instance variables as private but then they won't be directly accessible to sub classes and still you can make them available by public getter/setters.
      – Pushpesh Kumar Rajwanshi
      Nov 20 at 20:02











      0














      You can use Interfaces to define Type and Abstract classes to define default behavior. For instance:



      interface Ship {
      String getName();
      void setName();
      }

      abstract class DefaultShip implements Ship {
      private int length;

      public DefaultShip(int length) {
      this.length = length;
      }

      public String toString() {
      return " Ship " + getName() + " l=" + this.length;
      }
      }

      public class ContainerShip extends DefaultShip {

      private int teu;
      private String name;

      public ContainerShip(String name, int length, int teu) {
      super(length);
      this.name = name;
      this.teu = teu;
      }

      public void getName(String name) {
      return this.name;
      }

      public void setName(String name) {
      this.name = name;
      }

      public String toString() {
      return super.toString() +" container carrier with " + this.teu + " TEU";
      }
      }





      share|improve this answer


























        0














        You can use Interfaces to define Type and Abstract classes to define default behavior. For instance:



        interface Ship {
        String getName();
        void setName();
        }

        abstract class DefaultShip implements Ship {
        private int length;

        public DefaultShip(int length) {
        this.length = length;
        }

        public String toString() {
        return " Ship " + getName() + " l=" + this.length;
        }
        }

        public class ContainerShip extends DefaultShip {

        private int teu;
        private String name;

        public ContainerShip(String name, int length, int teu) {
        super(length);
        this.name = name;
        this.teu = teu;
        }

        public void getName(String name) {
        return this.name;
        }

        public void setName(String name) {
        this.name = name;
        }

        public String toString() {
        return super.toString() +" container carrier with " + this.teu + " TEU";
        }
        }





        share|improve this answer
























          0












          0








          0






          You can use Interfaces to define Type and Abstract classes to define default behavior. For instance:



          interface Ship {
          String getName();
          void setName();
          }

          abstract class DefaultShip implements Ship {
          private int length;

          public DefaultShip(int length) {
          this.length = length;
          }

          public String toString() {
          return " Ship " + getName() + " l=" + this.length;
          }
          }

          public class ContainerShip extends DefaultShip {

          private int teu;
          private String name;

          public ContainerShip(String name, int length, int teu) {
          super(length);
          this.name = name;
          this.teu = teu;
          }

          public void getName(String name) {
          return this.name;
          }

          public void setName(String name) {
          this.name = name;
          }

          public String toString() {
          return super.toString() +" container carrier with " + this.teu + " TEU";
          }
          }





          share|improve this answer












          You can use Interfaces to define Type and Abstract classes to define default behavior. For instance:



          interface Ship {
          String getName();
          void setName();
          }

          abstract class DefaultShip implements Ship {
          private int length;

          public DefaultShip(int length) {
          this.length = length;
          }

          public String toString() {
          return " Ship " + getName() + " l=" + this.length;
          }
          }

          public class ContainerShip extends DefaultShip {

          private int teu;
          private String name;

          public ContainerShip(String name, int length, int teu) {
          super(length);
          this.name = name;
          this.teu = teu;
          }

          public void getName(String name) {
          return this.name;
          }

          public void setName(String name) {
          this.name = name;
          }

          public String toString() {
          return super.toString() +" container carrier with " + this.teu + " TEU";
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 19:49









          elbraulio

          4589




          4589























              0














              You dont need setters and getters in the sub-class for name and length values.
              Only if you want to override them.



              Ship superclass



              public class Ship {

              private String name;
              private int length;

              public Ship(String name, int length) {
              this.name = name;
              this.length = length;
              }

              public String getName() {
              return name;
              }

              public void setName(String name) {
              this.name = name;
              }

              public String toString() {
              return " Ship "+name+" l="+ length ;
              }

              }


              ContainerShip subclass



              public class ContainerShip extends Ship {

              private int teu;

              public ContainerShip(String string, int length, int teu) {
              super(string, length);
              this.teu = teu;
              }

              public String toString() {
              return super.toString()+" container carrier with "+teu+" TEU";
              }

              }


              Main method



              public static void main(String args) {

              ContainerShip cont = new ContainerShip("Name", 100, 4);
              System.out.println(cont.getName());
              cont.setName("new name");
              System.out.println(cont.getName());
              }


              OUTPUT:




              Name



              new name







              share|improve this answer


























                0














                You dont need setters and getters in the sub-class for name and length values.
                Only if you want to override them.



                Ship superclass



                public class Ship {

                private String name;
                private int length;

                public Ship(String name, int length) {
                this.name = name;
                this.length = length;
                }

                public String getName() {
                return name;
                }

                public void setName(String name) {
                this.name = name;
                }

                public String toString() {
                return " Ship "+name+" l="+ length ;
                }

                }


                ContainerShip subclass



                public class ContainerShip extends Ship {

                private int teu;

                public ContainerShip(String string, int length, int teu) {
                super(string, length);
                this.teu = teu;
                }

                public String toString() {
                return super.toString()+" container carrier with "+teu+" TEU";
                }

                }


                Main method



                public static void main(String args) {

                ContainerShip cont = new ContainerShip("Name", 100, 4);
                System.out.println(cont.getName());
                cont.setName("new name");
                System.out.println(cont.getName());
                }


                OUTPUT:




                Name



                new name







                share|improve this answer
























                  0












                  0








                  0






                  You dont need setters and getters in the sub-class for name and length values.
                  Only if you want to override them.



                  Ship superclass



                  public class Ship {

                  private String name;
                  private int length;

                  public Ship(String name, int length) {
                  this.name = name;
                  this.length = length;
                  }

                  public String getName() {
                  return name;
                  }

                  public void setName(String name) {
                  this.name = name;
                  }

                  public String toString() {
                  return " Ship "+name+" l="+ length ;
                  }

                  }


                  ContainerShip subclass



                  public class ContainerShip extends Ship {

                  private int teu;

                  public ContainerShip(String string, int length, int teu) {
                  super(string, length);
                  this.teu = teu;
                  }

                  public String toString() {
                  return super.toString()+" container carrier with "+teu+" TEU";
                  }

                  }


                  Main method



                  public static void main(String args) {

                  ContainerShip cont = new ContainerShip("Name", 100, 4);
                  System.out.println(cont.getName());
                  cont.setName("new name");
                  System.out.println(cont.getName());
                  }


                  OUTPUT:




                  Name



                  new name







                  share|improve this answer












                  You dont need setters and getters in the sub-class for name and length values.
                  Only if you want to override them.



                  Ship superclass



                  public class Ship {

                  private String name;
                  private int length;

                  public Ship(String name, int length) {
                  this.name = name;
                  this.length = length;
                  }

                  public String getName() {
                  return name;
                  }

                  public void setName(String name) {
                  this.name = name;
                  }

                  public String toString() {
                  return " Ship "+name+" l="+ length ;
                  }

                  }


                  ContainerShip subclass



                  public class ContainerShip extends Ship {

                  private int teu;

                  public ContainerShip(String string, int length, int teu) {
                  super(string, length);
                  this.teu = teu;
                  }

                  public String toString() {
                  return super.toString()+" container carrier with "+teu+" TEU";
                  }

                  }


                  Main method



                  public static void main(String args) {

                  ContainerShip cont = new ContainerShip("Name", 100, 4);
                  System.out.println(cont.getName());
                  cont.setName("new name");
                  System.out.println(cont.getName());
                  }


                  OUTPUT:




                  Name



                  new name








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 19:50









                  oskozach

                  6615




                  6615






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400123%2fim-having-problems-fixing-my-getters-and-setters-in-inheritance%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      TypeError: fit_transform() missing 1 required positional argument: 'X'