0

I've been reading book algorithms on Java by Robert Lafore. And there is an problem - Object sorting. So there are three classes which make hierarchy. In book it is fine working but in Eclipse I have problem "Implicit super constructor Person() is undefined. Must explicitly invoke another constructor". What shall I do with extentions classes, or what shall I change to make this code run? Please help. Thank you.

class Person {
    private String lastName;
    private String firstName;
    private int age;

    public Person(String last, String first, int a) {
        lastName = last;
        firstName = first;
        age = a;
    } 

    public void displayPerson() {
        System.out.print("Last name: " + lastName);
        System.out.print(". First name: " + firstName);
        System.out.println(". Age: " + age);
    }

    public String getLast() {
        return lastName;
    }
}

Next class extends person.

public class ArrayInObj extends Person  {

    private Person a[];
    private int nElems;

    public ArrayInObj(int max) {        // Here is the problem
        a = new Person[max];
        nElems = 0;
    }

    public void insert(String last, String first, int age) {
        a[nElems] = new Person(last, first, age);
    }

    public void display() {
        for (int i = 0; i < nElems; i++) {
            a[i].displayPerson();
        }
        System.out.println(" ");
    }

    public void insertionSort( ) {
        int in, out;

        for (out = 1; out < nElems; out++) {
            Person temp = a[out];
            in = out;

            while (in > 0 && a[in-1].getLast().compareTo(temp.getLast()) > 0) {
                a[in] = a[in -1];
                --in;
            }
            a[in] = temp;
        }
    }
}

And the main class with main function.

public class ObjectSort extends ArrayInObj {

    public ObjectSort(int max) {
        super(max);
    }

    public static void main(String[] args) {

        int maxSize = 10;
        ArrayInObj arr = new ArrayInObj(maxSize);

        arr.insert("Evans", "Patty", 24);
        arr.insert("Smith", "Lorainne", 37);
        arr.insert("Yee", "Tom", 43);
        arr.insert("Adams", "Henry", 63);
        arr.insert("Hashimoto", "Sato", 21);

        System.out.println("Before sorting: ");
        arr.display();

        arr.insertionSort();

        System.out.println("After sorting: ");
        arr.display();

    }

}
Turtality
  • 37
  • 5
  • 1
    define no-arg constructor in Person call like `public Person(){}` I would question is ArrayObj really a person? – SMA Jul 11 '16 at 13:37
  • 6
    You have a class ``ArrayInObj`` that is used as a container for multiple instances of ``Person``. As such, it should not extend ``Person``. Remove the inheritance (by removing ``extends Person``) and you dont have to worry about that error anymore. – f1sh Jul 11 '16 at 13:38
  • @f1sh, +1. The question is a typical sample of inheritance misuse. Probably it's enough to use `List` here. If author needs a tree of persons (each person needs to have list of related persons), it should be done in a different manner by implementing type agnostic generic data structure like `MyTree`, then a type specific tree of persons could be created: `new MyTree()`. – Vladimir Vagaytsev Jul 11 '16 at 13:43
  • Is this an example in a book (I highly doubt that)? ArrayInObj is certainly not a Person. Why does it inherit from Person? – Nuri Tasdemir Jul 11 '16 at 13:43
  • You have transcribed the code incorrectly. There is no inheritance among these three classes in the Lafore book. Recommend closing as a typo. – Erick G. Hagstrom Jul 11 '16 at 14:29
  • Yes in book there are no inheritance. Sorry that I made this thread. Thank you all guys! – Turtality Jul 11 '16 at 15:59

4 Answers4

5

The technical reason why you are getting this error is that the only constructor of ArrayInObj implicitly calls the constructor of its superclass as follows:

public ArrayInObj(int max) {
    /* this gets implicitly added and fails to compile */
    super();
    a = new Person[max];
    nElems = 0;
}

To fix the compile error, you can either explicitly call the superclass's existing constructor or define a zero-argument constructor for Person.

Reading your code I get the impression that the correct solution would be to remove the extends Person from ArrayInObj's definition. Ask yourself: Is a ArrayInObj a Person? No, it seems to be a container for an array of Person.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
1

ArrayInObj is extending Person, and in the constructor for ArrayInObj, it is implicitly calling the Person constructor super() (by not explicitly calling a different super()). But, the Person class does not have a zero-argument constructor. Either explicitly call a Person constructor that is defined, super(a,b,c), or do not have ArrayInObj extends Person.

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
Isaac Hanson
  • 1,088
  • 9
  • 14
0

There are three possibilities:

  1. you add an constructor Person() to your Person class
  2. you call the existing constructor with super(last, first, a) as first call in your ArrayInObj classes constructor
  3. Is your ArrayInObj-class realy a Person? Does not look like it is. Remove extends Person
ageh
  • 91
  • 10
0

As others have said, you need to provide a default constructor in your Person class, so that the ArrayInObj class may use it.

You will also benefit from reading about constructors in the documentation https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html, or regarding default constructors this site Java default constructor.

Community
  • 1
  • 1
Sumiya
  • 317
  • 4
  • 5