Join 150,221 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,164 people online right now. Registration is fast and FREE... Join Now!
I need some help with two classes im trying to inherit. Ive got class Person which is the base class and class student which needs to be inherited from person. Im at early stages on the student class but im having problems with super().
Heres my classes :
CODE
public class Person { protected String name; protected String gender; protected Date dateOfBirth; protected String address; protected String natInsceNo; protected String phoneNo; protected int indexNumber; protected static int counter;
Hi, I got three classes with toString method Person, Student and PartTime
PartTime is inherited from Student and Student is inherited from Person which is the base class.
Now im trying to inherit the toString methods in Student from Person and in PartTime from Student.
Heres my classes
CODE
public class Person { protected String name; protected String gender; protected Date dateOfBirth; protected String address; protected String natInsceNo; protected String phoneNo; protected int indexNumber; protected static int counter;
public void setEmployer(String emp) { employer = emp; }
public void setWorkAddress (String wAddr) { workAddress = wAddr;
}
public void setWorkPhone (String wPhone) { workPhoneNo = wPhone;
} public String toString {
} }
Do i need an extra line of code in toString method to inherit the other methods drom the other classes or just define the variables in the method ex return "Name" + name etc...... ?
// Person constructors... public Person(Date dateOfBirth, String nme, String addr,String ins); public Person(String nme, char sex, Date dob);
//Student extends Person
public Student(Date dateEnrolled, String crs, int yr){ super(); // constructor does not exist in Person. //... }
public Student (String nme,char sex, Date dob,String insceNumber,Date enrolled ) { super(nme); // constructor does not exist in Person. super(sex); // constructor does not exist in Person. // never mind, only on super call per constructor // }
// PartTime extends Student public PartTime() { super(); // constructor does not exist in Student. //... }
public PartTime(String nme, char sex, Date dob, String ins, Date enrolled) { super(nme, sex, dob, ins, enrolled); // this one should actually work! //... }
I think what's being missed here is the intent of the parent contructor. You want all that data!
What you're constructors say is, basically, is for Person the two most important bits of info are (String name, Date dateOfBirth). So, we'll keep those.
Here are some options:
CODE
// Person constructors... public Person(String name, Date dateOfBirth) { this.name = name; this.dateOfBirth = dateOfBirth; counter++; } public Person(String name, Date dateOfBirth, char gender) { this(name, dateOfBirth); this.gender = gender; } public Person(String name, Date dateOfBirth, String address, String natInsceNo) { this(name, dateOfBirth); this.address = address; this.natInsceNo = natInsceNo; }
public Student(String name, Date dateOfBirth, Date dateEnrolled, String course, int year){ super(name, dateOfBirth); this.dateEnrolled = dateEnrolled; this.course = course; this.year = year; }
public Student (String name, Date dateOfBirth, String natInsceNo, Date dateEnrolled, String course, int year) { this(name, dateOfBirth, dateEnrolled course, year); this.natInsceNo = natInsceNo; }
Hi, I just tried running the program by using run java application in the tools toolbar, but I get the error "Exception in thread "main" java.lang.NoSuchMethodError: main." I'm new to java and trying to learn it before I take a course. How do you get the program to run? Can anyone help?