Answer:
Here is the JAVA program:
public class StudentDerivationFromPerson {
public static void main (String [] args) { //start of main function
StudentData courseStudent = new StudentData(); //creates an object of StudentData class named courseStudent
courseStudent.setName("Smith"); //assign courseStudent's name with Smith using courseStudent object and by calling the mutator method setName
courseStudent.setAge(20); //assign courseStudent's age with 20 using courseStudent object and by calling the mutator method setAge
courseStudent.setID(9999); //assign courseStudent's ID with 9999 using courseStudent object and by calling the mutator method setID
courseStudent.printAll(); //calls printAll member method using courseStudent object. This will print the name and age
System.out.println(", ID: " + courseStudent.getID()); //println statement to print the ID by calling accessor method getID of StudentData class
return;}}
Step-by-step explanation:
Here is the complete program:
PersonData.java
public class PersonData {
private int ageYears;
private String lastName;
public void setName(String userName) {
lastName = userName;
return; }
public void setAge(int numYears) {
ageYears = numYears;
return; }
public void printAll() {
System.out.print("Name: " + lastName);
System.out.print(", Age: " + ageYears);
return; }}
StudentData.java
public class StudentData extends PersonData {
private int idNum;
public void setID(int studentId) {
idNum = studentId;
return; }
public int getID() {
return idNum; } }
StudentDerivationFromPerson.java
public class StudentDerivationFromPerson {
public static void main (String [] args) {
StudentData courseStudent = new StudentData();
courseStudent.setName("Smith");
courseStudent.setAge(20);
courseStudent.setID(9999);
courseStudent.printAll();
System.out.println(", ID: " + courseStudent.getID());
return;}}
Here the courseStudent is the object of class StudentData
new keyword is used to create and object of a class
This object is used to access the methods of class
Note that the StudentData class is the sub class of PersonData class
The object courseStudent first accesses the mutator method setName of class PersonData to set the name (lastName) as Smith.
Next it accesses the mutator method setAge of class PersonData to set the age (ageYears) to 20
Then courseStudent accesses the mutator method setID of class StudentData to set the ID (idNum) to 9999.
Then the method printAll() of PersonData class is called using courseStudent object. This method has two print statements i.e.
System.out.print("Name: " + lastName);
System.out.print(", Age: " + ageYears);
So these two statement are printed on the output screen. They display the name and age i.e. Smith and 20 with a comma between them.
Next we have to display the ID too so we use this statement:
System.out.println(", ID: " + courseStudent.getID());
The above print statement calls getID accessor method of StudentData class to get the ID and display it on the screen.
Hence the output of the above program is:
Name: Smith, Age: 20, ID: 9999