Why the method that gets the Father class as a parameter is called and not the method that gets the child class as a parameter?

inheritance in java
abstract class
super keyword in java
javascript class inheritance
polymorphism in java
calling base class constructor from derived class in java
javascript call child method from parent
types of inheritance in javascript

I have a class named A, and a class named B that extends A. Playing with some methods to understand polymorphic behaviour, I ran into a weird situation.

public class Main {
    public static void main(String[] args){
        B b = new B();
        A a = b;
        b.f1(a);
    }
}

public class A {
.
.
.
    public void f1(A a){
        if(a instanceof B)
            f1((B)a);
        else
            System.out.println("Nothing");
    }
.
.
.
}

public class B extends A {
.
.
.
    public void f1(B b){
        System.out.println("B::f1(B)");
    }
.
.
.
}

I expected f1 in class A to be called first(because a is of type A) which actually happened. Then I expected the line f1((B)a); to be called, since a is an instance of B. Until now everything went as expected. However, I thought that the next method that will be called is f1(B) in class B. Instead, f1(A) in class A was called over and over causing a stack overflow exception. Why wasn't the f1(B) in class B called? An instance of B was the caller and the parameter was cast to type B.

f1(A a) is an instance method of class A. It has no knowledge of methods of sub-classes of A. Therefore, it cannot call void f1(B b) of class B. Therefore, f1((B)a) executes void f1(A a) again.

If you want to call f1(B b), you'll have to call f1 on an instance variable of class B:

public void f1(A a){
    if(a instanceof B) {
        B b = (B)a;
        b.f1(b);
    } else {
        System.out.println("Nothing");
    }
}

Object-Oriented Programming Using C++, Thus, a child class object can use its own functions or its parent's (as long as the parent The opposite is not true—a parent object cannot use its child's functions Overloaded functions, you will recall, require different parameter lists, and the the function name (also called the class of the object invoking the method). 2. The difference between a static method and a class method is: Static method knows nothing about the class and just deals with the parameters Class method works with the class since its parameter is always the class itself. The class method can be called both by the class and its object. Class.classmethod() Or even Class().classmethod()

Your class A has no any idea that class B exists somewhere and has B.f1(B b) function. Actually f1(A a) and f1(B b) are two different functions. what you want probably to achieve, should be done in bit different way:

public class A {
//...
    public void f1(A a) {
         System.out.println("Nothing");
    }
//...
}

public class B {
//...
    public void f1(B a) {
        // this is different function, because of another parameters
    }

    public void f1(A a) {
        if(a instanceof B)
            f1((B)a);
        else
            super.f1(a);
    }
//...
}

The Complete Idiot's Guide to C# Programming, Using this technique, derived classes can automatically call the appropriate They can also pass parameter values to the base class constructors. This example demonstrates that you can use inheritance to get more mileage out of your efforts. its parent. Overloading a method in a parent class is called overriding it. This method receives the variable as a parameter named i and then sets the value of i to 2. Meanwhile, back in the main method, println is used to print the value of number after the tryToChangeNumber method returns. Because tryToChangeNumber gets only a copy of number, not the number variable itself, this program displays the following on the

The code must cast the caller of the method as follow:

public class A {

    public void f1(A a){
        if(a instanceof B)
            ((B) this).f1(a);
        else
            System.out.println("Nothing");
    }
}

In your code instead you are casting only the parameter. Doing that result in a call on the same method of the same class recursively.

If you cast the caller instead you inform the JVM that you like to call the method on the subclass, so you can exit from the method immediately.

Important Note that invoking a cast on the caller depending on the parameter can generate a class cast exception, for example in the following context:

    B b = new B();
    A a = new A();
    a.f1(b);

You can't be sure that the method f1 receiving a B parameter is called on B object.

Class inheritance, The syntax to extend another class is: class Child extends Parent . Let's create So, if a method is not found in Rabbit.prototype , JavaScript takes it from As we can see, it basically calls the parent constructor passing it all the arguments. When an object method runs, it gets the current object as this . When you use a _____ in a method header, you indicate the parameter's type and name, and the method receives a copy of the value passed to it. Aliases are alternate names or pseudonyms.

Java Inheritance, Also called an extended class in Java super(); // invokes base class default constructor super(parameters); The call to super() must be the first line of the derived class constructor; If explicit call to parent constructor not made, the also use super to invoke a method from the parent class (from inside the derived class​). After you've converted an instance method to a static method, you can no longer call the method from the instance of the class. Instead, you must call the method from the class itself. For more information about static methods, see X++ static classes. main methods. A main method is a class method that is run directly from a menu option. The

Inheritance (The Java™ Tutorials > Learning the Java Language , The class from which the subclass is derived is called a superclass (also a base A subclass inherits all the members (fields, methods, and nested classes) from Constructors are not members, so they are not inherited by subclasses, but the we would get a compile-time error because obj is not known to the compiler to​  Every class in java is child of Object class either directly or indirectly. Object class contains toString() method. We can use toString() method to get string representation of an object. Whenever we try to print the Object reference then internally toString() method is invoked.

about_Classes, Methods may take parameters that provide input data. In class methods, no objects get sent to the pipeline except those This value is known at creation time. The derived class inherits the properties of the base class. A ____ data field or method can be used within its own class or in any classes extended from that class, but it cannot be used by "outside" classes. protected Classes that depend on field names from parent classes are said to be ____ because they are prone to errors.

Comments
  • It is because f1() is an instance method, B's instance knows its instance method, not A's instance method. Same is true in reverse also
  • Which overloaded method is called in java?
  • Oh, I get it now. What about the opposite way, can I call a method written in class A from a method written in class B(assuming the method is not overridden in class B of course)?
  • @RonenHanukayev yes, since B can see all the (public or protected) methods of its super-class A.
  • @Eran please note my answer. Doing that can generate a ClassCastException so the questionnaire should be advised.
  • @DavideLorenzoMARINO How can a ClassCastException be thrown if you check that a instanceof B before doing any cast?
  • Oh I see... you didn't casted "this", but you called f1 on the instance of the parameter. Interesting trick. +1
  • Probably ((B) this).f1((B)a); is what author need, since f1(B b) is what expected to be called for argument of type B. I think that it would be great to check that this actually can be casted to B as well :)