-1

I have a class that extends another class and I am trying to have it get idnum from it but every time I compile I get the error

Student.java:11: error: cannot find symbol
      this.idnum = idnum;
          ^
  symbol: variable idnum

This is my class codes for compile testing, I can get name to be called but not idnum is there something wrong with my code?

public class Student extends Person {  
   private int credits;
   private double gradePoints;

   public Student(String name, String idnum, int credits, double gradePoints){
      this.name = name;
      this.idnum = idnum;
      this.credits = credits;
      this.gradePoints = gradePoints;
      }

   public void setName(String name) {
      this.name = name;
      }
   public void setId(String idnum){
      this.idnum = idnum;
      }
   public void setCredits(int credits){
      this.credits = credits;
      }
   public void setGradePoints(double gradePoints){
      this.gradePoints = gradePoints;
      }

   public String getName(){
      return name;
      }
   public String getId(){
      return idnum;
      }
   public int getCredits(){
      return credits;
      }
   public double getGradePoints(){
      return gradePoints;
      }
}

Person class

public class Person {     
   private String name;
   private String idnum;

   public Person(String name,String idnum){
        this.name = name;
      this.idnum = idnum;
        }

   public void setId(String idnum){
      this.idnum = idnum;      
      }

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

   public String getName() {
      return name;
      }

     @Override public String toString()
      {
      return name + " " + idnum;
      }
 }
Robert
  • 47
  • 1
  • 1
  • 10
  • Read this: http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean – Stephen C Sep 21 '14 at 02:19

5 Answers5

1

Just call the superclass's constructor to initialize the superclass private fields. Private members aren't accessible to the outside world, even for it's subclasses.

public Student(String name, String idnum, int credits, double gradePoints) {
      super(name, idnum);
      this.credits = credits;
      this.gradePoints = gradePoints;
}
plalx
  • 42,889
  • 6
  • 74
  • 90
  • tried it this way and get error Student.java:10: error: no suitable constructor found for Person(String,String) super(name, idnum); ^ constructor Person.Person(String,int) is not applicable (actual argument String cannot be converted to int by method invocation conversion) constructor Person.Person() is not applicable – Robert Sep 20 '14 at 02:28
  • 1
    @Robert if that's the error you're getting, then you're not showing us the code you're compiling. The given code has `idnum` as a `String`, but your error suggests it's just an `int`. – Drew McGowen Sep 20 '14 at 02:29
  • @DrewMcGowen Thank you very much, I realized my mistake and it was that I was changing my Person class to what I needed but did not save and overwrite the file that was still wrong inside my project so it was reading the wrong one – Robert Sep 20 '14 at 02:55
0

You can't access the Person class private fields. Either use it's getter or change the field to protected or public.

ndrone
  • 3,524
  • 2
  • 23
  • 37
0

idnum (and name) is a private field of Person, meaning that no other classes (including subclasses) can access the field. Use the protected modifier so that subclasses (such as Student) can access the field.

August
  • 12,410
  • 3
  • 35
  • 51
0

Because idnum is declared private in class Person, it can't be accessed outside the class - that includes deriving classes. To fix this, use the Person constructor from within Student:

public Student(String name, String idnum, int credits, double gradePoints)
{
    super(name, idnum);
    this.credits = credits;
    this.gradePoints = gradePoints;
}

Also, do note that the getters and setters for name and idnum don't need to be redefined in Student because they're exactly the same as the ones in Person.

Drew McGowen
  • 11,471
  • 1
  • 31
  • 57
0

It's good to know solution but you should understand the concepts.

In java we have four Access Specifiers

1. public
2. private
3. protected
4. default(no specifier)

public specifiers :

Public Specifiers achieves the highest level of accessibility. Classes, methods, and fields declared as public can be accessed from any class in the Java program, whether these classes are in the same package or in another package.

Example :

public class Demo {  // public class  
    public x, y, size;   // public instance variables  
}  

private specifiers :

Private Specifiers achieves the lowest level of accessibility.private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses. So, the private access specifier is opposite to the public access specifier. Using Private Specifier we can achieve encapsulation and hide data from the outside world.

Example :

public class Demo {   // public class  
    private double x, y;   // private (encapsulated) instance variables  

    public set(int x, int y) {  // setting values of private fields  
        this.x = x;  
        this.y = y;  
    }  

    public get() {  // setting values of private fields  
        return Point(x, y);  
    }  
}  

protected specifiers :

Methods and fields declared as protected can only be accessed by the subclasses in other package or any class within the package of the protected members' class. The protected access specifier cannot be applied to class and interfaces.

Example:

public class Person {     
   protected String name;
   protected String idnum;
}

default(no specifier):

When you don't set access specifier for the element, it will follow the default accessibility level. There is no default specifier keyword. Classes, variables, and methods can be default accessed.Using default specifier we can access class, method, or field which belongs to same package,but not from outside this package.

Example:

class Demo  
{  
    int i; (Default)  
}  
Ankush
  • 675
  • 2
  • 13
  • 29