Objective:
After completing this exercise, you will be able to:
- Concept of Inheritance
- Implement a super class and sub classes in Java
- Understand the working of super class and sub class constructors
Activity:
Task 1:
Consider the following hierarchy as it exists in a university:
- There are two types of persons in the university i.e. Student and Faculty
- Every Person has some basic information that is common to all persons i.e. the first_name and last_name stored as private attributes and age which is a protected attribute.
- A student can in turn be either an Undergraduate or a Graduate student, every student has a cgpa.
- An undergraduate student has a fyp_name as his private attribute.
- A graduate student has a thesis topic as his private attribute.
- A faculty member has private attributes about the number of courses he is currently teaching, i.e. his course_count and a three digit telephone extension number.
Implement the entire hierarchy of the Class i.e. define all the classes along with their attributes and their inheritance.
Task 2:
Add appropriate constructors to all the classes created in Exercise 2. For example the constructor for the Person class should take three inputs (for first_name, last_name and age). The student constructor should take four inputs, three for its parent class (i.e. Person) and one float value to be assigned to the cgpa attribute.
Task 3:
Add getters and setters function headings and implementation (resp) for all attributes in all the classes that you have defined.
Solution:
class Person {
private String firstName;
private String lastName;
protected int age;
public Person(String fname, String lname, int age) {
System.out.println("Person() called");
this.firstName = fname;
this.lastName = lname;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Student extends Person {
protected float cgpa;
public Student(String fname, String lname, int age, float c_gpa) {
super(fname, lname, age);
System.out.println("Student() called");
this.cgpa = c_gpa;
}
public float getCGPA() {
return cgpa;
}
public void setCGPA(float cgpa) {
this.cgpa = cgpa;
}
}
class Undergraduate extends Student {
private String fypName;
public Undergraduate(String fname, String lname, int age, float c_gpa, String fyp_n) {
super(fname, lname, age, c_gpa);
System.out.println("Undergraduate() called");
this.fypName = fyp_n;
}
public String getFYPName() {
return fypName;
}
public void setFYPName(String fypName) {
this.fypName = fypName;
}
}
class Graduate extends Student {
private String thesisTopic;
public Graduate(String fname, String lname, int age, float c_gpa, String thesistopic) {
super(fname, lname, age, c_gpa);
System.out.println("Graduate() called");
this.thesisTopic = thesistopic;
}
public String getThesisTopic() {
return thesisTopic;
}
public void setThesisTopic(String thesisTopic) {
this.thesisTopic = thesisTopic;
}
}
class Faculty extends Person {
private int courseCount;
private String extensionNumber;
public Faculty(String fname, String lname, int age, int courseCount, String extensionNumber) {
super(fname, lname, age);
System.out.println("Faculty() called");
this.courseCount = courseCount;
this.extensionNumber = extensionNumber;
}
public int getCourseCount() {
return courseCount;
}
public void setCourseCount(int courseCount) {
this.courseCount = courseCount;
}
public String getExtensionNumber() {
return extensionNumber;
}
public void setExtensionNumber(String extensionNumber) {
this.extensionNumber = extensionNumber;
}
}
public class Main {
public static void main(String[] args) {
Undergraduate ted = new Undergraduate("Ted", "Thompson", 22, 3.91f, "Some FYP Name");
Faculty richard = new Faculty("Richard", "Karp", 45, 2, "420");
System.out.println("Undergraduate: " + ted.getFirstName() + " " + ted.getLastName() + " (" + ted.getAge() + " years old, CGPA: " + ted.getCGPA() + ", FYP Name: " + ted.getFYPName() + ")");
System.out.println("Faculty: " + richard.getFirstName() + " " + richard.getLastName() + " (" + richard.getAge() + " years old, Teaching " + richard.getCourseCount() + " courses this semester, Extension Number: " + richard.getExtensionNumber() + ")");
}
}