AnswerBun.com

Why does "this" refer to the parent class although a child class object calls the method?

I think I did not understand something related to Inheritance or the this statement.
If I execute a method of a parent class, which calls another function implemented in the child class,
this refers to the function of the parent class although the Object executing the parent’s class function is a child class object.

For example:

Parent Class Methods

public BookingConfirmation templateMethod(BigDecimal value) {
        try {
            Boolean authenticate = this.authenticate();
            if (!authenticate)
                // TODO Write own Exception for this Case
                throw new UnsupportedOperationException("Das Konto wurde nicht erfolgreich authentifiziert!");

            System.out.println(this.getClass().toString());

            this.bookTransaction(new BigDecimal("100"));
            BookingConfirmation bookingConfirmation = this.createBookingConfirmation();
            return bookingConfirmation;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

protected BookingConfirmation createBookingConfirmation() {
        throw new UnsupportedOperationException("Not implemented yet");
    }

Child Class Implementation:

protected BookingConfirmation createBookingConfirmation(BigDecimal value) {
        try {
            String title = "Überweisung von Konto " + this.id.toString() + " in der Höhe von " + value.toString() + " Euro";
            BookingConfirmation bookingConfirmation = new BookingConfirmation(title);

            return bookingConfirmation;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

Stack Overflow Asked by 1stNox on February 22, 2021

1 Answers

One Answer

The methods in parent and child have different signatures

  • In parent: createBookingConfirmation()
  • In child: createBookingConfirmation(BigDecimal)

Thus, the method is not overridden, but overloaded and as consequence

BookingConfirmation bookingConfirmation = this.createBookingConfirmation();

always calls the method in the parent class. A fix would be to give the parent class method a BigDecimal parameter.

If a method of a parent class is overridden by a method of the child class, the correct child-class method will be called at runtime. The underlying concept is known as dynamic dispatch.


To prevent these kinds of mishaps, we should always annotate methods that are meant to override methods of supertypes with @Override. The compiler will generate a compiler error if the annotated method does not override a method in at least one supertype.


A remark on the wording of the question: As was pointed out by JCWasmx86 in the comments, one cannot cast a method. What one can cast is a variable.

Correct answer by Turing85 on February 22, 2021

Add your own answers!

Related Questions

Copy each cell in a range to multi-cell range

2  Asked on November 29, 2021 by preston-richardson

   

How to make dynamic custom textarea with jquery?

3  Asked on November 29, 2021 by david-noya

     

private static field Java

3  Asked on November 29, 2021

         

unordered_map excess calls to hash function

4  Asked on November 29, 2021 by amir-kirsh

   

cant load static file in django

1  Asked on November 29, 2021

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.