Yajash Jhamb
Yajash Jhamb

Reputation: 7

what's the use of java virtual method invokation?

Say I have the following code

public class A {
  public void callme() {
    System.out.println("Calling of class A function ");
  }
}

public class B extends A {
  public void callme() {
    System.out.println(" Calling of class B fuction ");
  }

  public void Hello() {
    System.out.println("hello guys");
  }
}

and a main() that does the following

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

    b = (B)a;
    a.callme();
    b.callme();
    a.Hello(); // show error : Hello is undefined for method A
  }
}

The book says "you get the behavior associated with the object to which the variable refers at runtime". Ok, I get behavior of B class when a method callme is called, but when I access the method Hello, it shows an error that Hello is undefined for method A. why is that?

Upvotes: 1

Views: 67

Answers (4)

Is the parent class aware of classes derived from it ?

Casting does not change the actual object type. Only the reference type gets changed.

I highly recommend you my upcasting and downcasting writing from the link

Upvotes: 0

Boola
Boola

Reputation: 358

Polymorphism doesn't work this way. Since A is parent of B, B can inherit methods of A (like son can inherit properties of father), but its not vice versa because A doesn't know which classes are inheriting it (A does not know who are its children.)

For Example, suppose there is one more class C:

public class C extends A {
public void callme(){

System.out.println(" Calling of class C fuction ");
}

public void Hello(){

System.out.println("hello guys, I am C");
}
}

Now, if you use

a.Hello();

how will a know which child class it should call since it does not methods of its child. only its own abstract method, which it knows child will implement for sure.

Upvotes: 1

Vikrant Kashyap
Vikrant Kashyap

Reputation: 6846

public class B extends A {
public void callme(){

System.out.println(" Calling of class B fuction ");
}

public void Hello(){ // hello method is only the part of class B not A.

System.out.println("hello guys");
}
}

In above class hello() method is a part of B only. it not override by the method of A.

now in your main method call

public static void main(String [] args){
A a= new B();  // object of b referred by (a) Reference Variable of A
B b= new B();  // object of b referred by (b) Reference Variable of B

b= (B)a;
a.callme();  //override takes place and method of B's Callme() called
b.callme();  //again override takes place here and method of B's Callme() called
a.Hello();// buttttttt
b.Hello(); // this will compile and executes fine.
}
}

Here you used the reference variable of class A which don't have any method name Hello(). So, Method Resolution will not take place(won't be able to find any method like Hello()).

but if you try to call b.Hello() using the reference variable of B then it will work fine to you.

Now suppose there is another class C which is a sub class of A and contains a Method Name Hello().

public class C extends A {

    public void Hello(){ // hello method is only the part of class B not A.

    System.out.println("hello guys");
    }
    }

In main a Statement like this

A a = new C();

and if you try to call a.Hello() then which Hello() method will call. Compiler will get confused.

So, This concept work only when you try to override the method of super class in sub Class.

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234665

a is of type A, even if it is referring to an instance of B.

A doesn't have the Hello method (even an abstract declaration). So your compiler emits the error. Adjusting your code slightly to

public abstract class A {
    public abstract void Hello();
    /*the rest as before*/

would be a fix. A is then assuming the characteristics of an interface.

Upvotes: 0

Related Questions