Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Help with Polymorphism  RSS feed

 
Piash Chaudhuri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends ! This is my first post in JavaRanch ! I know VB6 but very new to Java (and OOP).

In the following code, I've created a super class (MySuper) that has a subclass (MySub1).
objSuper is reference of MySuper
objSub1 is a object of MySub1

Both the classes has a method x(). Naturally the method is over ridden in MySub. MySub also has another method y().

Now if I comment the objSuper.y(); line, the code compiles perfectly. But with that line I'm gettin a compile error "cannot find symbol method y()".

I'm reading Just Java 2 (6th Ed) by Pere van der Linden. In the summary of Chapter 8 he wrote "superclass = subclass // always valid"
But then why this code is not working ?
I'm really confused.
Please help Me. Thanks in advance !
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is simply that the reference of type MySuper doesn't know anything about method y(). When OO programmers talk about objects of type MySub being type MySuper (that is, MySub is-a MySuper) that relationship really only goes one way. References of type MySuper will only be able to use the methods declared in the MySuper interface, and not the ones in MySub. MySub references, on the other hand, know all the methods of the MySuper type by virtue of extending that class.
 
Kail Limas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty new to Java myself, so maybe somebody with more experience can explain why it works that way in more detail. However, while this: superclass = subclass, is possible, using a method of the subclass that isn't present in the superclass isn't. The superclass only takes on the functions etc. from the subclass it has itself.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that a simple way to state it is that access is controlled by the type of the reference variable NOT the type of the object assigned to it.

Meaning that, if you have a superclass reference variable currently pointing to a subclass object, then the refverence can ONLY SEE the things defined by the superclass "class".
 
Piash Chaudhuri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys !
I've tried same in C#, but it gives me same error ("Error: 'ConsoleApplication1.MySuper' does not contain a definition for 'y'").

However, if I cast the superclass reference to subclass, it works in both C# and Java.



Why it is working with casting ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because, by adding the cast, you are saying to the compiler 'I know objSuper is a reference to a MySuper object, but at this point in the program it will actually be referencing a MySub1 object, so it is okay to call the y() method'.

If at runtime however, objSuper is not pointing to a MySub1 object, you will get an exception thrown.
 
Piash Chaudhuri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the great explanatons !




Is there any 'rating' system here or 'mark resolved post' ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!