4
public abstract class Human{
    public String name;
    public int number

    public void getInfo(){
        Name = JOptionPane.showInputDialog("Please enter your name: ");
        money = Double.parseDouble(JOptionPane.showInputDialog("Please enter amount of money .00: "));
    }

    public void displayInfo(){
        JOptionPane.showMessageDialog(null,"Name: "+name+"\n"+
                                           "Number: "+number);
    }
}

public class Student extends Human {

}

public class Teacher extends Human{

}

public class Janitor extends Human{

{

I need help if calling the methods getInfo() and displayInfo() in all 3 classes below. I have tried:

public class Student extends Human{
    public Student(){
          getInfo();
          displayInfo();
    }

it works, but it generates a warning saying "problematic call in constructor" I guess it is not the best way to do it.

I also tried:

@Override
public void getInfo() {
    
}

but if I leave it empty nothing happens. Basically I am trying to call the method in the abstract class in a simple way without needing to type it up in every class.

peterh
  • 11,875
  • 18
  • 85
  • 108
TEZZ
  • 179
  • 1
  • 15
  • the error is not about the methods in abstract class, although using `this.methodName` is better, you have another issue in the constructor – Nikos M. Dec 11 '15 at 11:54

3 Answers3

3

As already mentioned, you shouldn't call overridable methods in constructors, because if another class overrides this method and invokes the constructor of the superclass, it may try to use values that are not initialized yet, since the overriden method will be invoked. Example:

public class Superclass {
  protected int id;
  protected void foo() {
    System.out.println("Foo in superclass");
  }

  public Superclass() {
    foo();
  }
}

public class Subclass extends Superclass {
  public Subclass(int id) {
    super();
    this.id = id;
  }

  @Override
  protected void foo() {
    System.out.println("Id is " + id);
  }
}

This will print the unitialized value of id, since you first call the constructor of the superclass which invokes the foo method of the subclass.

You can fix this by making your methods final if this suits your case.

Ivaylo Toskov
  • 3,911
  • 3
  • 32
  • 48
  • or call `super( )` AFTER `this.id=id;` – Nikos M. Dec 11 '15 at 12:20
  • 1
    This won't compile. Call to `super()` must be the first statement in the constructor of the subclass. – Ivaylo Toskov Dec 11 '15 at 12:22
  • hmm, then i'll need to check it again, which java version? – Nikos M. Dec 11 '15 at 12:24
  • It works like that in all java versions. Check http://stackoverflow.com/questions/1168345/why-does-this-and-super-have-to-be-the-first-statement-in-a-constructor. – Ivaylo Toskov Dec 11 '15 at 12:25
  • Thanks, the 'final' did it for me it saves me from re-typing all the methods into the other 3 class. If I try to initialize them in each class it would defeat the purpose of making it more efficient since it requires the same code in each class. – TEZZ Dec 11 '15 at 12:26
  • yes ok here https://docs.oracle.com/javase/tutorial/java/IandI/super.html, although i'm not if calling the dotted `super.Class` is possible and it seems no such requirement is needed – Nikos M. Dec 11 '15 at 12:26
1

You get the warning because it's a good practice not to call overridables in the constructor; since these overridables could try to access member variables that are not initialized yet (== null) .

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
0

You shouldn't call overridable functions inside a constructor. check this link

Community
  • 1
  • 1
sameera sy
  • 1,708
  • 13
  • 19