Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Covariant returns

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below mentioned source is from Whizlabs SCJP 6.0 preparation kit.

The output is
Inside SubCovariantTest
true
5


Now, can anyone help in figuring out why line 2 is printing true?
As getObject() method of SubCovariantTest class is getting invoked and return type from that method is an object of type B, in my opinion line 2 should print false.
I know the compiler knows Java better than me. But, I need to know the reason behind such results.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, a is an A reference variable pointing to an object of type B. The method called is the one in B that overrides the one in A, which returns a B object. But B objects pass the Is-A test for A (a B object is also an A object,) so the instanceof test gives true.

Also, the instance field x is resolved at compile time (there is no overriding for fields) to the field x defined in class A, since a is a reference of type A.

Does that help?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For one thing, when you declare a variable of type A, it's 100% instanceof A, isn't it ? Why would it be false ? When you instanciate an Animal to be a Dog (Animal dog = new Dog();), the "dog" variable is both an Animal and a Dog.

What do you think the following would print ?
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Ruben. It definitely helped me in understanding the logic.
Actually, I forgot the fact that during compile time, overriding will not come into scenario!!
Thanks for the reply.
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Christophe
Actually, I always tend to assume that during runtime, instanceof operator checks with the actual object to which the instance variable is referring to. In the examples you have provided,
I think the output should be -
false
true
true // I had a doubt earlier here, but now it got cleared after your explanation
false

Thanks.
 
Bartender
Posts: 4108
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajshekhar Paul wrote:
As getObject() method of SubCovariantTest class is getting invoked and return type from that method is an object of type B, in my opinion line 2 should print false.



You are right that the actual object is of type B but the object B is a A (it passes the IS-A test since B extends A). So output is true.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajshekhar Paul wrote:Yes Ruben. It definitely helped me in understanding the logic.
Actually, I forgot the fact that during compile time, overriding will not come into scenario!!
Thanks for the reply.


You are right Rajshekhar. And overriding only affects instance methods. No static methods or variables (either static or instance) are overridden.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think the output should be -
false
true
true // I had a doubt earlier here, but now it got cleared after your explanation
false


No. You have to check again about what "instanceof" does. Read this tutorial. Look at the output for "obj2" in the "The Type Comparison Operator instanceof" section.
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Christophe
Thanks for that link. It really helped a lot in understanding the proper functionality of the instanceof operator.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My test:



result is:

true
true
true
true
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Jia
Your test is absolutely correct.

 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic