• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Polymorphism

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am getting very much confused in solving polymorphism questions as shown below...This is from Devaka's Practice Exam1 Q.No:29
Is there any easy way to solve this type of questions??? I am able to answer some questions related to this but not as complicated as this....

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Banu I think this is a simple one. There is no method call to confuse you. Just make a hierarchy in your mind. The hierarchy of this question will be like this

 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the trick that depends on interface.

Compiler says you can cast any class to any interface, it's not my work to deal with interface if programmer using explicit casting, JVM will see this matter.

Why?
As any class' subclasses can implement any interface or any number of interface except final class, as final class cannot be subclassed.

So compiler says I can deal with final class as final class can not produce childs, so I am sure final class reference variable will keep only final class Objects.

For non-final class compiler says I can not deal with them as non-final class reference variable could contain any object if object is of its subclasses.

And for null you can remember one thing, null is assignable to all of the class reference as it always come in last in the hierarchy tree of java. And Object comes always top in the hierarchy.

Any doubt than ask which line is confusing you ?
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiler says you can cast any class to any interface, it's not my work to deal with interface if programmer using explicit casting, JVM will see this matter.

Thanks Punith ,I dont know this point.....

Now I understood but one question ..


when the second line runs ..... b is of type C112 and it contains null . Am I Right??? can we say that : C112 b=null;

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it is of type B112 that is declared there, but C112 is its child type so compiler is ok with it.

Java considers null in lowest in its hierarchy.

They way you can assign any type of object to class Object reference like:



Same way you can assign null to any type of reference variables.


[EDIT : did not understand question exactly, so typed wrong answer.]
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Banu Chowdary wrote:
Now I understood but one question ..


when the second line runs ..... b is of type C112 and it contains null . Am I Right??? can we say that : C112 b=null;



No, b is not the type of C112, Its a type of B112. Remember, when you write something like this,



myClass is a reference (type) of the class which declares it, i.e MyClass.
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then what is the meaning of C112 here which is there in the explicit casting???

If this casting is not converting the reference type from B112 to C112 then next time if we are assigning the b to anything means we are assigning reference type B112 only ...Right??? hmmmmmmmmmmmmmmmmmmmm
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can think it as anonymous reference of C112.


it is like:
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I understood ......... Thank you Punith
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic