Java: how constructors and methods behave with inheritance

Java seems inconsistent in how class constructors and methods deal with inheritance.

Case 1 - Methods:

public class HelloWorld { public static void main(String[] args) { Bee b = new Bee(); b.foo(); } } class Insect { public void foo() { this.bar(); System.out.println("Insect foo"); } public void bar() { System.out.println("Insect bar"); } } class Bee extends Insect { @Override public void foo() { super.foo(); System.out.println("Bee foo"); } @Override public void bar() { System.out.println("Bee bar"); } } 

The above code outputs the following:

Bee bar

Insect foo

Bee foo

Note that a call to the this.bar () method in the Insect foo () method does return and calls the Bee bar () method (instead of calling the Insect bar () method).

Case 2 - Constructors:

 public class HelloWorld { public static void main(String[] args) { Bee i = new Bee(1); } } class Insect { public Insect(int size) { this(size, 123); System.out.println("Constructor: Insect size"); } public Insect(int size, int height) { System.out.println("Constructor: Insect size, height"); } } class Bee extends Insect { public Bee(int size) { super(size); System.out.println("Constructor: Bee size"); } public Bee(int size, int height) { super(size, height); System.out.println("Constructor: Bee size, height"); } } 

The above outputs the following.

Constructor: insect size, height

Constructor: insect size

Constructor: bee size

Note the call to "this (size, 123);" in the Insect constructor goes to the second Insect constructor instead of the bee 2nd constructor.

So, in the end, method calls return to the subclass, and constructor calls remain in the superclass. Can someone explain why?

+5
source share
1 answer

Besides the correct information in the comments about methods that are not similar to constructors, you actually simplify things too much.

You see, when you write constructors, you have to decide if you are going to use the constructor of the "same class" (when using this() ) - or if you want to call the constructor of the superclass (using super() ). There is no intermediate position - you should be frank about it!

Keep in mind: the responsibility of the constructor is to initialize the object of the corresponding class. It can make a huge difference if you call this(whatever) or super(whatever) inside your constructors.

The compiler cannot know what you intend to do. Therefore, you have to be explicit about which particular constructor you want to call.

And besides - polymorphism simply does not make sense here, like that. You absolutely want the initialization of the object to take place as you expect. Because mistakenly initialized objects will probably cause problems later.

Your idea of โ€‹โ€‹making constructors polymorphic basically means that at runtime you decide which constructor should call. But you already know that you are in class X, and that you need to initialize a new object X. There is nothing here that could be obtained from resolving polymorphism. On the contrary, it would allow very strange and strange problems that would be purely runtime!

+2
source

Source: https://habr.com/ru/post/1268119/


All Articles