Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling overloaded inherited methods using super class reference

I do not understand this Java behavior. I have two classes:

class C1 {
    public void m1(double num) {
        System.out.println("Inside C1.m1(): " + num);
    }
}

class C2 extends C1 {
    public void m1(int num) {
        System.out.println("Inside C2.m1(): " + num);
    }
}

And it is my main:

public class Main {

    public static void main(String[] args) {
        C1 c = new C2();
        c.m1(10);
    }
}

And the result was:

Inside C1.m1(): 10.0

When I expected:

Inside C2.m1(): 10

Also when I try to complete the code syntax, I found this:

Enter image description here

Where is the other m1 of C2 class?

I also check the bytecode of my Main.class and I saw this:

Compiled from "Main.java"
public class com.company.Main {
  public com.company.Main();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class com/company/C2
       3: dup
       4: invokespecial #3                  // Method com/company/C2."<init>":()V
       7: astore_1
       8: aload_1
       9: ldc2_w        #4                  // double 10.0d
      12: invokevirtual #6                  // Method com/company/C1.m1:(D)V
      15: return
}

The bytecode tell me that it will invoke the C1.m1 (D)V (line 12).

Why the method of C1? I am trying to understand this behavior.

like image 846
Robert Avatar asked Apr 25 '15 18:04

Robert


People also ask

Can sub class overload the methods of super class?

Note: In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass instance methods—they are new methods, unique to the subclass.

Can we achieve method overloading in inheritance?

Yes of course, overloading in inheritance class is possible in Java. Java compiler detect that add method has multiple implementations. so according to the parameter java compiler will determines which method has to be executed. class Parent { public void add(int a) { System.

Can we overload a function of superclass in derived class?

You can override or overload a function in the derived class. If you have another function with the same name as the inherited function but different parameter lists it is called overloading functions. Both of these can exist.

Can we use method overloading in inheritance in C#?

In C#, just like in C++, there is no overload resolution between class Base and class Derived. Also, there is no overloading across scopes and derived class scopes are not an exception to this general rule.


1 Answers

Your two methods named m1 do not have the same signature; the one in the superclass takes a double, and the one in the subclass takes an int. This means that the compiler will select the method signature to call based on the compile-time type of your variable, which is C1, and will call m1(double). Since at runtime the class C2 doesn't have an overriding version of m1(double), the version from C1 is invoked.

The rule is that method signatures are computed at compile time based on compile-time types; method calls are dispatched at runtime based on matching signatures.

like image 129
chrylis -cautiouslyoptimistic- Avatar answered Sep 26 '22 22:09

chrylis -cautiouslyoptimistic-