Michael Shokery
Michael Shokery

Reputation: 83

inheritance constructor with no arg

I am trying to create a no argument constructor that will take inputs from the user (via the Scanner class) for all of the instance variables, but it is inherited from another class.

How do I do this?

Here is what I did:

 public class Manager extends Super_Employee{
     String  manager ;

     /* String GetManger  (){
         return manager ;
     }*/

     Manager (){
        Scanner manager = new Scanner(System.in);
     }

     @Override
     String Print() {
         return ("the manager is : " +manager);
     }

here is the super class:

 public  class Super_Employee extends abstract_Employee {

     String Fname ;
     String Lname;
     String address;
     int number;
     Super_Employee(String Fname,String Lname, String address, int number)
    {
        System.out.println("Constructing an Employee");
        this.Fname = Fname ;
        this.Lname = Lname;
        this.address = address;
        this.number = number;
    }  


 @Override
    String Print (){
    return ("the employee name is : "+Fname+ " "+ Lname+ " ,the Employee address is  : "
            + address + " ,the employee Phone # : "+number);   
} 
}

Upvotes: 3

Views: 1907

Answers (4)

Bruce Martin
Bruce Martin

Reputation: 10543

Some Possible options:

1) use a static builder

public static Manager newManager() {

 .....

return new Manager(.....);

2) create no-args super constructor:

Super_Employee() {
   ...
}}

3) Could use a "Second class" - rather ugly and I do no advise

public Manager() {
  this(new MyTemp());
}

public Manager(MyTemp temp) {
  super(temp.fname, temp.lname,....);
}

Upvotes: 2

DaoWen
DaoWen

Reputation: 33019

Based on your errors it sounds like your super class takes 3 arguments, but you're trying to use the default constructor since you don't have the values for those three values yet. In this case I would recommend using a factory method to construct your object.

Example:

public class Manager extends Super_Employee{

    /** Factory method, reading constructor arguments from System.in */
    public static Manager make() {
        Scanner s = new Scanner(System.in);
        new Manager(s.next(), s.next(), s.next(), s.nextInt());
    }

    public Manager(String fName, String lName, String address, int number) {
        super(fName, lName, address, number);
    }

     // . . .

Using the factory method Manager.make() to construct your object allows you to read in all the values you need from System.in before calling the super constructor, which wouldn't be possible from within the Manager constructor.

Upvotes: 2

davidbuzatto
davidbuzatto

Reputation: 9424

You MUST provide a default constructor in your subclass when in it you need to define a default constructor that will not call a non default constructor in the superclass. Confusing? Take a look in the example.

public class Foo {
    public Foo( String x ) {
    }
}

public class Bar extends Foo {
    public Bar() {
        super(); // this is called implicitly even if this line is not provided! compilation error here
    } 
}

In the example above, the subclass construtor will call super(), the default constructor of the subclass, so you must provide a defualt construtor in the superclass or write your subclass this way:

public class Bar extends Foo {
    public Bar() {
        super( "hadoken!" ); // now it works, since it calls a constructor that exists
    } 
}

Now, if you don't provide any constructor in the Foo class, it will have a defaul constructor (provided by the compiler).

Upvotes: 2

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

Add a no-arg constructor in Super_Employee

Super_Employee() { }

Since, you have

Super_Employee(String Fname,String Lname, String address, int number) {
    //...

the compiler doesn't provide a default constructor (which is a no-arg constructor provided by the constructor itself).

Upvotes: 2

Related Questions