Okay, so I have 4 different classes for my program, and I’m trying to add a new variable in one of my subclasses. I will show the code for all of the classes involved and then explain what exactly is wrong.
First Code is my Person Class:
class Person {
private String myName; // name of the person
private int myAge; // person's age
private String myGender; // 'M' for male, 'F' for female
// constructor
public Person(String name, int age, String gender) {
myName = name;
myAge = age;
myGender = gender;
}
public String getName() {
return myName;
}
public int getAge() {
return myAge;
}
public String getGender() {
return myGender;
}
public void setName(String name) {
myName = name;
}
public void setAge(int age) {
myAge = age;
}
public void setGender(String gender) {
myGender = gender;
}
public String toString() {
return myName + ", age: " + myAge + ", gender: " + myGender;
}
}
my next class is my Teacher Class:
public class Teacher extends Person {
private static int salary;
private static String subject;
public Teacher(String name, int age, String gender, String subject, int salary) {
super(name, age, gender);
//Constructor
salary = salary;
subject = subject;
public String toString(){
return super.toString() +", Subject: " + subject + " Salary: " + salary;
}
}
}
So the problem with my program is, I’m trying to add in the String Subject and the Int Salary. I’m getting an error that keeps saying “Syntax error on token “String”, @ expected”(Line 12 of the Teacher Class) and then another one right next to it saying:””Syntax error, insert “EnumBody” to complete BlockStatement”(line 12 of the Teacher Class).
So all I am trying to do is add the new values into my parent class so that when I go to add in a user I can enter their salary and subject and so on. Sorry if i’m not too clear. It might be in plain sight for some of you, if i’m not clear enough please let me know.
Any help is greatly appreciated!
Thanks,
Sully
I haven’t looked all through your code, but I believe this is the problem:
You’ve started overriding
toStringin the middle of the constructor. You need to close the constructor, then start thetoStringmethod:Ideally, use the
@Overrideannotation to tell the compiler to validate that you really are overriding a superclass method, too: