TurkuBasicOOPinJava / Week 5: Class hierarchies /03A. Adding features to child class
KaiquanMah's picture
setParentAttribute(value1) OR super(value1, ...)
cb12565 verified
raw
history blame
4.61 kB
The idea of inheritance is usually to ADD NEW FEATURES to SUBCLASSES,
i.e. to SPECIALISE the activities of classes.
Let's continue developing the 'Student' class by writing a constructor for it,
which will have a name and an email address as well as a number of credits as parameters.
In addition, write setting and observation methods for the number of credits:
class Student extends Person {
// new attribute, that person does not have
private int studypoints;
//CONSTRUCTOR
public Student(String name, String email, int studypoints) {
// Setting attributes from parents class
// by CALLING METHODS INHERITED from it
setName(name);
setEmail(email);
// child class's "own" attribute
this.studypoints = studypoints;
}
public int getStudypoints() {
return studypoints;
}
public void setStudypoints(int studypoints) {
this.studypoints = studypoints;
}
}
Now we can form an object from the Student class:
Student sam = new Student("Sam Student", "sam@example.com", 123);
System.out.println(sam.getStudypoints());
Program outputs:
123
Since 'Student' inherits the class 'Person', the features of the 'Person' class are also available.
The 'Student' class instance can therefore also be used to call the
name and email configuration and observation methods, for example:
Student sam = new Student("Sam Student", "sam@example.com", 123);
System.out.println(sam.getStudypoints());
System.out.println(sam.getName());
System.out.println(sam.getEmail());
Program outputs:
123
Sam Student
sam@example.com
Let's also implement the constructor and new setting and observation methods in the Teacher class:
class Teacher extends Person {
private int courses;
public Teacher(String name, String email, int courses) {
setName(name);
setEmail(email);
this.courses = courses;
}
public int getCourses() {
return courses;
}
public void setCourses(int courses) {
this.courses = courses;
}
}
Now the teacher and the pupil have not only common characteristics, but also their own:
Student sam = new Student("Sam Student", "sam@example.com", 123);
Teacher tina = new Teacher("Tina Teacher", "tina@example.com", 4);
// both have a name and an email
System.out.println(sam.getName());
System.out.println(sam.getEmail());
System.out.println(tina.getName());
System.out.println(tina.getEmail());
// only student has study points
System.out.println(sam.getStudypoints());
// only teacher has course amount
System.out.println(tina.getCourses());
Program outputs:
Sam Student
sam@example.com
Tina Teacher
tina@example.com
123
4
=================================
Calling 'parent class constructor'
In the previous examples, the values inherited from the parent class were set using the setting methods.
However, it would be easier to 'CALL the PARENT CLASS CONSTRUCTOR DIRECTLY, especially if there are MANY VALUES TO SET'.
This can be done with the 'super' keyword. It can be used to refer from a child class to a parent class.
Let's start by writing a constructor for the 'Person' class to set the values of the attributes:
// PARENT CLASS
class Person {
private String name;
private String email;
public Person (String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Then modify the Student class so that its constructor calls the parent class constructor:
class Student extends Person {
// new attribute that the person does not have
private int studypoints;
public Student(String name, String email, int studypoints) {
super(name, email);
this.studypoints = studypoints;
}
public int getStudypoints() {
return studypoints;
}
public void setStudypoints(int studypoints) {
this.studypoints = studypoints;
}
}
Let's make a similar change to the Teacher category:
class Teacher extends Person {
private int courses;
public Teacher(String name, String email, int courses) {
super(name, email);
this.courses = courses;
}
public int getCourses() {
return courses;
}
public void setCourses(int courses) {
this.courses = courses;
}
}