bleach64
bleach64

Reputation: 107

Implementing inheritance overriding the toString method

I have created a (Person,Student,Employee,Faculty and Staff)classes. Person has to subclasses Student and Employee. Employee has two subclasses Faculty and Staff. I have done all the codings an they are working fine except my driver class TestPerson program its giving compilation errors

Note: A test program that Creates a Person,Student,Employee,Faculty,Staff, and invokes their toString Method.

The errors of driver class TestPerson.java are below:-

 
error: constructor Student in class Student cannot be applied to given types;
error: no suitable constructor found for Employee(String,String,String,String)
error: constructor Faculty in class Faculty cannot be applied to given types;
error: no suitable constructor found for Staff(String,String,String,String)"

**I am just providing the codes of the driver class. If you need my other codings of the other classes, please state in the comment, and I will immediately post it.

Please see my codings below:-

public class TestPerson {

    public static void main(String[] args) {
        Person person = new Person("John Doe", "123 Somewhere", "415-555-1212", "[email protected]");
        Person student = new Student("Mary Jane", "555 School Street", "650-555-1212", "[email protected]", "junior");
        Person employee = new Employee("Tom Jones", "777 B Street", "40-88-889-999", "[email protected]");
        Person faculty = new Faculty("Jill Johnson", "999 Park Ave", "92-52-22-3-333", "[email protected]");
        Person staff = new Staff("Jack Box", "21 Jump Street", "707-21-2112", "[email protected]");

        System.out.println(person.toString() + "\n");
        System.out.println(student.toString() + "\n");
        System.out.println(employee.toString() + "\n");
        System.out.println(faculty.toString() + "\n");
        System.out.println(staff.toString() + "\n");
        }
}

//Person Class

public class Person {

    private String name;
    private String address;
    private String phone_number;
    private String email_address;

    public Person() {
    }

    public Person(String newName, String newAddress, String newPhone_number, String newEmail){
        name = newName;
        address = newAddress;
        phone_number = newPhone_number;
        email_address = newEmail;
    }

    public void setName(String newName){
        name = newName;
    }

    public String getName(){
        return name;
    }

    public void setAddress(String newAddress){
        address = newAddress;
    }

    public String getAddress(){
        return address;
    }

    public void setPhone(String newPhone_number){
        phone_number = newPhone_number;
    }

    public String getPhone(){
        return phone_number;
    }

    public void setEmail(String newEmail){
        email_address = newEmail;
    }

    public String getEmail(){
        return email_address;
    }

    public String toString(){
        return "Name :"+getName();
    }

}

//Student class

public class Student extends Person {

    public final String class_status;

    public Student(String name, String address, int phone, String email, String classStatus) {
    super(name, address, phone, email);
    class_status = classStatus;
    }
    public String toString(){
        return "Student Status: " + super.getName();
    }

}

//Employee Class

import java.util.Date;
public class Employee extends Person{

    private String office;
    private double salary;
    private Date hire;

    public Employee() {
    }

    public Employee(String name, String address, int phone, String email){
        super(name, address, phone, email);
    }

    public Employee(String office, double salary, Date hire){
        this.office = office;
        this.salary = salary;
        this.hire = hire;
    }

    public void setOffice(String office){
        this.office = office;
    }

    public String getOffice(){
        return this.office;
    }

    public void setSalary(double salary){
        this.salary = salary;
    }

    public double getSalary(){
        return this.salary;
    }

    public void setHire(Date hire){
        this.hire = hire;
    }

    public Date getHire(){
        return this.hire;
    }

    public String toString(){
        return "Office " + super.getName();
    }
}

//Faculty Class

public class Faculty extends Employee {
    private String officeHours;
    private int rank;

    public Faculty(String name, String address, int phone, String email) {
    super(name, address, phone, email);
    }

    public String toString(){
        return "Office " + super.getOffice();
    }
}

//Staff Class

public class Staff extends Employee {
    private String title;

    public Staff(String name, String address, int phone, String email) {
    super(name, address, phone, email);
    }

    public Staff(String title){
        this.title = title;
    }

    public void setTitle(String title){
        this.title = title;
    }
    public String getTitle(){
        return this.title;
    }

    public String toString(){
        return "Title :" + super.getName();
    }
}

Upvotes: 0

Views: 22201

Answers (4)

user3033679
user3033679

Reputation: 11

First Set the Phone Number datatype as integer in all the classes ..

main Function will be:

public class TestPerson {
    public static void main(String[] args) {
        Person person = new Person("John Doe", "123 Somewhere", "415-555-1212",
                                   "[email protected]");
        Person student = new Student("Mary Jane", "555 School Street", 
                                     650-555-1212, "[email protected]", "junior");
        Person employee = new Employee("Tom Jones", "777 B Street", 
                                       40-88-889-999,  "[email protected]");
        Person faculty = new Faculty("Jill Johnson", "999 Park Ave",
                                     92-52-22-3-333, "[email protected]");
        Person staff = new Staff("Jack Box", "21 Jump Street", 707-21-2112, 
                                 "[email protected]");

        System.out.println(person.toString() + "\n");
        System.out.println(student.toString() + "\n");
        System.out.println(employee.toString() + "\n");
        System.out.println(faculty.toString() + "\n");
        System.out.println(staff.toString() + "\n");
    }
}

Upvotes: 1

Philipp
Philipp

Reputation: 69663

The constructor of Person requires a String as third argument, but you are trying to pass int phone to the super-constructor in your sub-classes. That won't work because it's the wrong type.

By the way: you should always represent phone numbers with strings, not with integers.

  1. It doesn't make sense to add or subtract phone numbers.
  2. Integers don't allow leading zeros, which are used for area codes in some countries.
  3. Integers can't be larger than 2147483648. When you try to store very long telephone numbers, this seemingly arbitrary limit will cause very unexpected bugs.

Upvotes: 2

jdevelop
jdevelop

Reputation: 12296

It's perhaps not related to the question itself, but I think the design could be refined like this:

  • define abstract class Role with the name of role
  • define classes Student, Employee, Staff whatever inheriting Role
  • define class Person with common properties for all person type, names etc, and having property of type Role inside

then define toString on Person and in all Role implementation

in this way you will be able to extend or modify Persons independently from Roles, which makes the design more flexible

Upvotes: 2

Mike Thomsen
Mike Thomsen

Reputation: 37506

The reason you are getting those errors is that the constructors don't exist.

error: constructor Student in class Student cannot be applied to given types; error: no suitable constructor found for Employee(String,String,String,String)

That means you will not get the code to compile until you have this:

Student(String name, String addr, String phone, String email) {
    ....
}

Assuming you have set the properties in the constructor, toString would look like this:

public String toString() {
    return this.name + "\n" + this.addr + "\n" + this.phone + "\n" + this.email;

}

UPDATE

Your problem is that Student has only this constructor:

public Student(String name, String address, int phone, String email, String classStatus)

Student needs a constructor which takes only four strings as its parameters. Alternatively, you can make everything take the five parameters you specified.

Upvotes: 4

Related Questions