scotho3
scotho3

Reputation: 123

Inheritance and Constructors in Java

So, I'm working on a homework assignment, and I'm having a hard time following some of the directions, I've pasted the assignment below:

Create a hierarchy of five classes, plus one class included as a variable inside:

  1. Person has four String variables: name, address, phone, email
  2. Student is a subclass to Person and has one additional int variable status which takes values of 1, 2, 3, or 4 representing freshman, sophomore, junior, senior
  3. MyDate has three int variables for year, month, and day
  4. Employee is a subclass to Person and has one String variable office, one int variable for salary, and one MyDate variable for dateHired
  5. Staff is a subclass to Employee and has one additional String variable for title
  6. Faculty is a subclass to Employee and has one additional String variable for rank which takes values of Professor, Associate Professor, Assistant Professor, Instructor, and Adjunct. The data for all six classes should be private.

As for methods, you can skip the normal setters and getters if you write a single constructor that has parameters for all data and override the toString( ) method. Constructors of subclasses should use the super class constructor. The toString( ) methods of subclasses should use the toString( ) method of their super class.

The part that throws me for a loop is the idea that a single constructor can be written that will cover all the necessary parameters for the setters and getters instead of writing them in each sub-class. Is this possible? And how so?

Upvotes: 0

Views: 615

Answers (4)

Varun Achar
Varun Achar

Reputation: 15109

You need to use the constructor of the superclass whilst creating the subclass. So it should be:

public class Staff extends Employee {    

    private String title;    

    public Staff(String name, String address, String phone, String email, int status, String title) {
        super(name, address, phone, email, status);
        this.title = title;
    }    
}

Use the super(/*params of super class*/) to invoke the constructor of the super class and instantiate the inherited attributes. Note that you can only call a superclass constructor as the first statement of a constructor. If you don't call a superclass constructor explicitly, a call to super() (the default constructor of the superclass) is inserted automatically by the Java compiler.

For calling the parent class's toString() use:

public String toString() {
    return super.toString() + " ,title : " this.title;    
}

Similarly write the constructors and toString() methods of all classes.

Upvotes: 3

Abubakkar
Abubakkar

Reputation: 15654

This is what you can do

Person(String name,String address,String phone,String email){
 //Person constructor
 this.name = name;
 this.address = address;
 this.phone = phone;
 this.email = email;
}

public String toString(){
//toString method
return "Name: "+name+" Address: "+address+" Phone: "+phone+" Email: "+email;
}

Upvotes: 0

Andreas Dolk
Andreas Dolk

Reputation: 114777

Instead of writing

 public class A {
   private int b;
   private int c;

   public void setB(int b) {this.b = b;}
   public int getB() {return b;}

   // same for c
}

you allowed to code

 public class A {
   private int b;
   private int c;

   public A(int b, int c) {
     this.b = b;
     this.c = c;
   }

   @Override
   public String toString() {
     return "[b = " + b + ", c = " + c + "]";
}

(The implementation of toString() is just an example, it just needs to print the states of all fields)

Upvotes: 1

Paul Bellora
Paul Bellora

Reputation: 55223

As for methods, you can skip the normal setters and getters if you write a single constructor that has parameters for all data and override the toString( ) method.

I think the directions mean that each class you write can have a single constructor that takes parameters for all of its data. Taking the MyDate constructor for example:

public MyDate(int year, int month, int day) {
    ...
}

And likewise override toString() to report all that information.

Upvotes: 1

Related Questions