Problem regarding interface reference variable

I studied this with instance methods, at run time jvm uses the actual instance class, and with class methods, the compiler will only consider the declared type of the reference variable, not the actual class.

I learned this concept instance method while hiding.

And in my program I used an interface reference variable to save the class object and try to access the class instance method using this, but it causes an error. My program is as follows:

interface A { void show(); } class B implements A { public void show() { System.out.println("Interface Method"); } void info() { System.out.println("IN Info"); } } class interDemo { public static void main(String args[]) { A a; B b=new B(); a=b; a.show(); a.info(); } } 

Please help me figure this out ...

+4
source share
4 answers

The compiler tells you that type A does not have a method called info . This is true: the compiler does not know that at run time type A will actually be B , which has an info method. It would be unsafe to allow the a.info() call to actually compile and emit into bytecodes; there is nothing to say that A will always be of type B

This is called static typing . In Java and other languages ​​with a static type, you need to "distinguish" a variable to force the compiler to treat it as another type. In this case, you can write ((B) a).info() .

+7
source

sjr is correct. Here is another way to look at this:

You indicate that A can only show . This means that if you have a reference variable A , that’s all you can do.

This means that any class that wants to show can implement this interface. Clients who need a show object can use A without knowing or caring about whether the base class has other methods. This is a key aspect of the abstraction provided by object-oriented programming.

+1
source

You might want to see a lecture on YouTube. This covers your issue, and I hope this helps you.

In short: the static type of a is A. After assigning ba, the dynamic type of a is B. Thus, at this point, the static type of a is A and the dynamic type of a is B. The compiler does not follow dynamic types, it only checks static types. Therefore, it will not allow you to do anything other than a static type.

So, in your example, if you use a static type A link, you can only call methods from class A.

+1
source

super concept without confusion
=======================================

2 rules to know where the method will be executed (when a super-type reference is used to call methods)

NOTE: check the “create object” / “link assignment” statement to apply the rule

1 RULE: 1st check the method to be called. If it is static / overloaded / single - then it becomes static polymorphism / static (the compiler is looking for a reference type) --- hence it always runs from a reference type

2: The correct verification method: if it is overridden , then it becomes a dynamic polymorphism (jvm searches for the type of the object) --- therefore, it is always executed from the type of the object (i.e. the right to a new keyword)

eg:

 super s=new child(); s.play(); 

there are 2 cases:

1st : check play () is what I set (static / overloaded / single method) or dynamic (overridden)

2nd: if static it will be executed from super ie reference type leads to polymorphism of compilation time

if dynamic it will be executed from a child type ie, leads to dynamic polymorphism

0
source

Source: https://habr.com/ru/post/1334473/


All Articles