Why does calling a method from base class calls the child method?

Sam

I'm a student, learning Java. I know, protected means access from children or the same package. Here we inherit and override a protected method. And after such an action, whenever the base class wants to call its own method it calls the new overridden one from the subclass. I've been debugging this for a while and marked the execution order with comments. But I can't understand why doesn't it call the base method when I clearly call that from inside the base class constructor?

public class Solution {

    public static void main(String[] args) {
        new B(); // first
    }

    public static class A {

        public A() {
            initialize(); // third
        }

        protected void initialize() {
            System.out.println("class A"); // we never go here
        }
    }

    public static class B extends A {

        public B() {
            super(); // second
            initialize(); // fifth
        }

        protected void initialize() {
            System.out.println("class B"); // fourth, sixth
        }
    }
}

That's a task from one website, so basically the solution is to change access modifier of the initialize method from protected to private. But I still fail to understand why is the problem happening.

Sam

As Dakoda answered, the root cause is polymorphism. That means we may create child objects, but refer to them as their parent type and when we call the methods of the parent layer we actually refer to the child's methods.

In my case, I create a child object (marked //first) B, which has its own body of the initialize method. One nuance of the inheritance is that it doesn't include constructors, so I can call the parent's constructor (marked //second). Inside the parent's constructor, I call the initialize method - that is the polymorphism because I call the method of the child from its parent abstraction layer.

Here is the answer to the question - this happens, because we only allocated memory for a B instance, that means, we took A as our base and started to extend it (while we can overwrite anything inside). The only two things we did are:

  1. We created a constructor (it wasn't included in the base, as mentioned above)
  2. We overwrote the initialize method code. The code for this method that is inside the base is now lost for this object.

This concept of polymorphism is designed that way and there is no way for us to access the base method unless we specifically create an object that is either A itself or a child that doesn't overwrite this method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

calling child class method from base class C#

From Dev

Calling an extention method of a base class from child class

From Dev

Base class calls method from derived class?

From Dev

Calling a Method from "base.base" class?

From Java

Why does calling a method in my derived class call the base class method?

From Dev

Java - Calling method from child of abstract class

From Dev

c++: Calling a template method of a base class from a template child class though multilevel inheritance

From Dev

Calling the base class method from Code Quotations

From Dev

Calling an overridden method from the base class in Ada

From Dev

Why does calling a method not require an import of the class?

From Dev

Calling method of child class from object of parent class in C++

From Dev

Why does the child class does not inherit the method from parent class in python in this example?

From Dev

Why isn't my derived class method calling the overriden method from base class despite using the super keyword?

From Dev

Java Inheritance: Why calling a method at the parent constructor level, calls the overridden child method?

From Dev

Calling a method from a class

From Dev

Why does Python.NET use the base method instead of the method from a derived class?

From Dev

Calling method of template base class

From Dev

Calling derived class method from base class pointer

From Dev

Calling derived class method from base class destructor

From Dev

Calling derived class method from base class destructor

From Dev

Virtual Method in a Base class to use a Static variable from a child class

From Dev

Accessing a get method in base class from a child class not returning value

From Dev

Can somehow a method from base class return child class?

From Dev

Calling a private base method from a derived class in C#

From Dev

Calling virtual method of base template from derived variadic template class

From Dev

How to obtain the derived class type from base when calling a method

From Dev

which method does child class calls if it extends two classes with same method name

From Dev

Function calls parent class method instead of child?

From Dev

Calling method of the base class (not the immediate parent class)

Related Related

  1. 1

    calling child class method from base class C#

  2. 2

    Calling an extention method of a base class from child class

  3. 3

    Base class calls method from derived class?

  4. 4

    Calling a Method from "base.base" class?

  5. 5

    Why does calling a method in my derived class call the base class method?

  6. 6

    Java - Calling method from child of abstract class

  7. 7

    c++: Calling a template method of a base class from a template child class though multilevel inheritance

  8. 8

    Calling the base class method from Code Quotations

  9. 9

    Calling an overridden method from the base class in Ada

  10. 10

    Why does calling a method not require an import of the class?

  11. 11

    Calling method of child class from object of parent class in C++

  12. 12

    Why does the child class does not inherit the method from parent class in python in this example?

  13. 13

    Why isn't my derived class method calling the overriden method from base class despite using the super keyword?

  14. 14

    Java Inheritance: Why calling a method at the parent constructor level, calls the overridden child method?

  15. 15

    Calling a method from a class

  16. 16

    Why does Python.NET use the base method instead of the method from a derived class?

  17. 17

    Calling method of template base class

  18. 18

    Calling derived class method from base class pointer

  19. 19

    Calling derived class method from base class destructor

  20. 20

    Calling derived class method from base class destructor

  21. 21

    Virtual Method in a Base class to use a Static variable from a child class

  22. 22

    Accessing a get method in base class from a child class not returning value

  23. 23

    Can somehow a method from base class return child class?

  24. 24

    Calling a private base method from a derived class in C#

  25. 25

    Calling virtual method of base template from derived variadic template class

  26. 26

    How to obtain the derived class type from base when calling a method

  27. 27

    which method does child class calls if it extends two classes with same method name

  28. 28

    Function calls parent class method instead of child?

  29. 29

    Calling method of the base class (not the immediate parent class)

HotTag

Archive