Results 1 to 2 of 2
  1. #1
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default subclass arguments to superclass variables

    SOLVED!

    Hi, I am making a chess piece like this:

    Piece wKing = new King("King", 'w');

    Piece is superclass to King. Piece has variables "String Name" and "char Color".

    Piece:
    Java Code:
    public String Name;
    public char Color;
    
    public Piece(String iName, char iColor)
        {
            Name = inName;
            Color = inColor;
        }
    King:
    Java Code:
    public class King extends Piece
    {
    public King(String iName, char iColor)
        {
            super(Name, Color);
        }
    }
    First, do I have to have the variables in Piece protected/public for this?
    Second, why do I get "cannot reference Name before supertype constructor has been called" on both when I have them as protected in the superclass?
    Last edited by Gatsu; 01-23-2013 at 04:49 PM.

  2. #2
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: subclass arguments to superclass variables

    Ok I solved this just now by changing my King constructor to this:

    Java Code:
    public King(String Name, char Color)
        {
            super(Name, Color);
        }
    using the same argument names as the names of the variables I want to assign them to.

Similar Threads

  1. Replies: 7
    Last Post: 11-04-2012, 11:04 PM
  2. Override a superclass's methods with a subclass
    By zach&kody in forum New To Java
    Replies: 7
    Last Post: 05-24-2011, 02:50 PM
  3. Using superclass fields in subclass method
    By lonegreyride in forum New To Java
    Replies: 12
    Last Post: 11-17-2010, 01:21 PM
  4. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 07:29 AM
  5. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 08:55 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
  •