Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading method on extended class

Tags:

java

Simple question, strange result. I have two classes A and B:

public class A
{
    protected int num;

    public A(int n)
    {
        num = n;
    }

    public boolean f(A a)
    {
        return num == a.num * 2;
    }
}

public class B extends A
{
    public B(int n)
    {
        super(n);
    }

    public boolean f(B b)
    {
        return num == b.num;
    }
}

Why does y1.f(y2) call the f() method in A instead of in B?

A y1 = new B(10);
B y2 = new B(10);

System.out.println(y1.f(y2));

Is it not supposed to call f() in B as B is more specific than A?

like image 835
Jonathan Avatar asked Jun 30 '15 15:06

Jonathan


People also ask

Can we overload method in different class?

Yes it is overloading , This overloading is happening in case of the class ' C ' which is extending P and hence having two methods with the same nam e but different parameters leading to overloading of method hello() in Class C .

Can we overload method in sub and 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 use method overloading in inheritance?

In the inheritance hierarchy, superclass and subclass methods can be overridden and overloaded.


1 Answers

Why does y1.f(y2) calls the f() method in A instead of in B?

Because the compile-time type of y1 is A.

Overloading is performed at compile-time... the execution-time type of the object you call the method on is only relevant for overriding.

So the compiler is choosing the method f(A) as that's the only f method it's aware it can call on y1 (and it's checked that it's applicable given the argument list). That method isn't overridden in B, therefore at execution time, the implmenetation in A is called.

As a starker example, consider this code:

Object x = "foo";
int length = x.length();

This won't even compile, because Object doesn't contain a length() method. String does, but the compiler doesn't consider that, because the compile-time type of x is Object, not String - even though we can tell that at execution time, the value of x will be a reference to a String object.

like image 88
Jon Skeet Avatar answered Nov 15 '22 22:11

Jon Skeet