klog60
asked on
Subclasses and Inheritance
Hello,
this is a class Person that holds data such as name, id etc. on a person.
I need to develop a subclass of this called Student that inherits from Person,but also includes the data - credits(count of how many credits student has accumulated), and av_mark(average mark).
I also need accessor methods to return the data items credits and av_mark,but I think they are similar to the accessor methods in the Person class.Please help me out, I know nothing about inheritance or superclasses !
class Person
{
protected Name name;
protected char sex;
protected String id;
protected Person father;
protected Person mother;
private static String tab = "";
public Person (Name pname, char psex)
{
name = pname;
sex = psex;
}
public Person (Name pname, char psex, String pid)
{
name = pname;
sex = psex;
id = pid;
}
public Name getname ()
{
return name;
}
public char getsex ()
{
return sex;
}
public String getid ()
{
return id;
}
public void setid (String sid)
{
id=sid;
}
public void setMother (Person p)
{
mother = p;
}
public void setFather (Person p)
{
father = p;
}
public String toString ()
{
String s = new String (name + " (" + sex + ")");
if (id !=null) s = s + " id: " + id;
s += "\n";
if(mother!= null)
{
tab += " ";
s += tab + "mother: " + mother;
tab = tab.substring(2); // remove indent
}
if (father!= null)
{
tab += " ";
s += tab + "father: " + father;
tab = tab.substring(2); // remove indent
}
return s;
}
}
this is a class Person that holds data such as name, id etc. on a person.
I need to develop a subclass of this called Student that inherits from Person,but also includes the data - credits(count of how many credits student has accumulated), and av_mark(average mark).
I also need accessor methods to return the data items credits and av_mark,but I think they are similar to the accessor methods in the Person class.Please help me out, I know nothing about inheritance or superclasses !
class Person
{
protected Name name;
protected char sex;
protected String id;
protected Person father;
protected Person mother;
private static String tab = "";
public Person (Name pname, char psex)
{
name = pname;
sex = psex;
}
public Person (Name pname, char psex, String pid)
{
name = pname;
sex = psex;
id = pid;
}
public Name getname ()
{
return name;
}
public char getsex ()
{
return sex;
}
public String getid ()
{
return id;
}
public void setid (String sid)
{
id=sid;
}
public void setMother (Person p)
{
mother = p;
}
public void setFather (Person p)
{
father = p;
}
public String toString ()
{
String s = new String (name + " (" + sex + ")");
if (id !=null) s = s + " id: " + id;
s += "\n";
if(mother!= null)
{
tab += " ";
s += tab + "mother: " + mother;
tab = tab.substring(2); // remove indent
}
if (father!= null)
{
tab += " ";
s += tab + "father: " + father;
tab = tab.substring(2); // remove indent
}
return s;
}
}
homework?
Yes, all you have to do is sublass first:
1) class Student extends Person {//extends means sublass
Now all data variables except the private one will be available in this class(no need to rewrite). Also, all those methods will be available. So, to add the new data members and accessor methods just go:
2) protected int credits;
protected int av_mark;
// I assume you want ints - any data type is allowed
Then the accessor methods, much the same pattern as before:
3)
public int getCredits() {
return credits;
}
public void setCredits(int x) {
credits=x;
}
// And so on for other data like av_mark
1) class Student extends Person {//extends means sublass
Now all data variables except the private one will be available in this class(no need to rewrite). Also, all those methods will be available. So, to add the new data members and accessor methods just go:
2) protected int credits;
protected int av_mark;
// I assume you want ints - any data type is allowed
Then the accessor methods, much the same pattern as before:
3)
public int getCredits() {
return credits;
}
public void setCredits(int x) {
credits=x;
}
// And so on for other data like av_mark
ASKER
Ah yes, so I don't have to retype everything in the subclass.But i also need a constructor method to hold the data items, and I think its public Student(int sCredits, int sAv_mark) {credits = sCredits;
av_mark = sAv_mark;.....},
but I need to call the parent class'
constructor method but I'm not sure how.does it have something to do with super. ?
av_mark = sAv_mark;.....},
but I need to call the parent class'
constructor method but I'm not sure how.does it have something to do with super. ?
>does it have something to do with super
yes, simply call
super(<name>, <sex>) or
super(<name>, <sex>, <id>)
don't forget to add parameters for the above in your constructor(s)
yes, simply call
super(<name>, <sex>) or
super(<name>, <sex>, <id>)
don't forget to add parameters for the above in your constructor(s)
ASKER
right, so I do
public Student ( Name name, char sex, int sCredits, double sAv_mark)
{
super(name, sex);
credits = sCredits;
av_mark = sAv_mark;
}
but now the toString method in the parent class wont return all the details - i'm using a test class
class TryStudent
{
public static void main(String args[])
{ Name name1 = new Name("Desie", "Greer");
Student des = new Student(name1, 'M', 60, 40);
System.out.println(des.toS tring());
}
}
it only returns Desie Greer (M)
I dont need to return id or mother or father, but i do need to return the new data items credits and av_mark.but how can I extend the toString method to encompass these?
Do I make a new method that incorporates the toString method? or can I just add the relevant code to the toString in the parent class for the new data items, and make the variables credits and av_mark public in the subclass?
as you can see I'm still grasping the fundamentals
public Student ( Name name, char sex, int sCredits, double sAv_mark)
{
super(name, sex);
credits = sCredits;
av_mark = sAv_mark;
}
but now the toString method in the parent class wont return all the details - i'm using a test class
class TryStudent
{
public static void main(String args[])
{ Name name1 = new Name("Desie", "Greer");
Student des = new Student(name1, 'M', 60, 40);
System.out.println(des.toS
}
}
it only returns Desie Greer (M)
I dont need to return id or mother or father, but i do need to return the new data items credits and av_mark.but how can I extend the toString method to encompass these?
Do I make a new method that incorporates the toString method? or can I just add the relevant code to the toString in the parent class for the new data items, and make the variables credits and av_mark public in the subclass?
as you can see I'm still grasping the fundamentals
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
buiochas le dia, thanks alot bobbit! its tough when you dont have any formal teaching!