How does method invoking takes place?

78 Views Asked by At

I'm new to Java Programming and learning polymorphism.

__EDIT__

As per the answers I received from everyone,I have code:

Here I'm typecasting my Derived object (obj) to Base type and then calling method().

public class Tester {
    public static void main(String[] args) {
        Base obj=(Base)new Derived();
        obj.method();
    }   
}

class Base{
    public void method(){
        System.out.println("In Base");
    }
}

class Derived extends Base{
    public void method(){
        System.out.println("In Derived");
    }
}

Output I'm getting is: "In Derived".

So after typecasting my object should become of type Base referenced by Base type. But it's not happening? Why?

Does typecast work on child->parent conversion or it has no effect here?

3

There are 3 best solutions below

2
On BEST ANSWER

Base obj=new Derived();

In the above statement, the reference part points to type Base. This is how the compiler identifies which class to consider. But your code will create an error. Let me explain the structure of the above statement before explaining why will it show an error.

Structure of the above statement:

  1. Declaration-The reference part is Base obj.
  2. Instantiation: The new keyword is a Java operator that creates the object/allocates space in the memory.

  3. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

Since, Derived is a sub-class of Base, you are allowed to call the constructor in the Derived class. This is how inheritance and polymorphism works.

Okay, Now let us go back to the error part. The obj.method() in your code is looking for a function method() in Base class but the method(int a) function in Base class requires an argument of type integer to be passed. So for the code to work, the calling statement has to be something like obj.method(5).This statement works because the calling statement is actually passing 5 to the function.

There is an easy fix for your code: Derived obj=new Derived();

Have you noticed?

  • I have relaced the reference to type Derived.

Why does that work?

  • Because there is method() function in your Derived class which doesn't require an integer argument.

There is one more amazing fact about inheritance in Java:

Everything possessed by a super-class is also possessed by the sub-class but the reverse is not true. And yes, the sub-class has the right to redefine any method/function it has inherited from super-class.

The above statement means the following code will work:

Derived obj=new Derived();
obj.method(5);

You must be wondering-How come this code works even though method() in Derived requires no argument. In fact, Derived has no method(int a).

Well, the answer to this is the amazing fact I have mentioned above.

Yes, method(int a) also belongs to Derived since it's a sub-class of Base.

But How does the code mentioned below work?

Derived obj=new Derived();
obj.method(5);

Simple, the JVM looks for the method(int a) in class Derived and it finds the function since Derived has inherited the function from Base class. Remember this too, the sub-class also has a privilege to over-ride a method in super class. This means that you can add method(int a) function in class Derived which over-rides the original method inherited from Base.

How inheritance works?

  • When you call obj.method(5) in the above code, the JVM first looks for any over-ridden method of the same type in Derived. If it does not find any over-ridden method, it moves up in the inheritance hierarchy chain to the super class and looks for the same method. But the reverse is not the true.
0
On

when there are same method names in different classes , the compiler comes to know by: 1-if you are passing any argument , then the type of argument which you are passing will tell the compiler which method to be called 2-if there are two classes , then make the object of that class for which you want to call the method

1
On

how does compiler comes to know which method is to be called & too from which class?

The compiler searches for the method in the class (in this case Base) of the object (in this case obj). If the method is not found in that class, then it looks for the method in the super class (in this case Object). If still not found, it flags an error.


Is it that compiler checks the reference type class & if method to be invoked is not present in reference type class it gives error??

Yes. But, as said before, If the method is not found in that class, then it looks for the method in the super class (in this case Object). If still not found, it flags an error.


Your obj.method() will fail, because Base does not have a method with the signature method() (it has method(int))


If your obj was of type Derived, then both these case will work:

  • obj.method(); // will call this method from the Derived class
  • obj.method(1); // will call this method from the Base class