Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract Classes - Super Constructor (Java)

I have a really simple question:

Let's say I have an abstract class which represents a person in a bar.

public class Person {
    protected String firstName;
    protected String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

And I also have 2 classes that extend Person, let's say a class for bartenders and a class for customers.

In the class for customers, I also want an int representing his age as a field. In the bartender class, we don't.

Also, for the customer class, I want a method isAdult().

public class Bartender extends Person {
    public Bartender(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

public class Customer extends Person {
private int age;    

    public Customer(String firstName, String lastName, int age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
}

I now have 2 questions:

1) This doesn't work as I get the message "Implicit super constructor Passenger() is undefined. Must explicitly invoke another constructor". What does this exactly mean? 2) For the method isAdult(), I feel like the best way to do it is to implement it in the abstract class Person like this:

public abstract boolean isAdult();

And then implement it for Bartenders returning always true and for Customers checking their age.

Another way is implementing it from the class Person directly like this:

public boolean isAdult() {
    return (this instanceof Bartender || age > 18);
}

Would that work and which way would be better?

like image 451
pavlos163 Avatar asked Mar 13 '15 21:03

pavlos163


People also ask

Can you use super with abstract classes?

An abstract class can have constructors like the regular class. And, we can access the constructor of an abstract class from the subclass using the super keyword.

Can an abstract class have a constructor in Java?

Like any other classes in Java, abstract classes can have constructors even when they are only called from their concrete subclasses.

Can we use super in constructor in Java?

With super(parameter list) , the superclass constructor with a matching parameter list is called. Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.

Can abstract class have parameterized constructor in Java?

Yes, we can define a parameterized constructor in an abstract class.


1 Answers

Person has a defined parameter using constructor and no default parameterless constructor. Since a child class's super constructor must always be called in the child constructor, and since Person has no default constructor, you must explicitly call it in the child constructor:

public Bartender(String firstName, String lastName) {
    super(firstName, lastName);

    // don't do this:
    // this.firstName = firstName;
    // this.lastName = lastName;
}

and

public Customer(String firstName, String lastName, int age) {
    super(firstName, lastName);
    this.age = age;
}

Regarding isAdult(), you could just have this method in the Customer class and not have it in Bartender. Alternatively if the super must have this method, you could throw an exception if someone calls it on a Bartender, since it should not be called that way.

like image 188
Hovercraft Full Of Eels Avatar answered Sep 23 '22 23:09

Hovercraft Full Of Eels