• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about reference variables  RSS feed

 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Ranchers:

I've enrolled into a Java class for this summer and have been reading the online book that comes with the class. The link to the book is posted at the bottom in case it's necessary to include it.

I have been reading about polymorphism, type conversion, and casting. I am confused as to when and why a programmer would choose a specific reference variable for an object. To make this question more clear, I'll give example code from the book:



In the main driver method, an instance of class B is instantiated and stored into a reference of the Object class. As a result, casting is required to call the m() method in the B class.

Could someone please explain why a programmer would use an Object reference variable instead of a reference variable from either class A or B... which, if I am not mistaken, would eliminate the need for casting to call the overridden m() methods?

It is also confusing because I read that the method called is chosen at runtime depending on which variable is stored in the reference variable, and not the reference variable itself. So I am not sure why a cast is required.

Code came from online book:
http://cnx.org/contents/fb64661c-5b3f-4ea8-97c6-e48df112438a@178.3:67/Object-Oriented_Programming_%28O
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are just trying to show you can. It's rare to actually need to.

One reason is if you don't write all the code yourself. Maybe someone gives you a list of Objects and you know they are of a certain type.

There's also an instanceof operator so you can check the type before casting. For example, you give me a list of objects. I want to do math if they happen to be numbers of just print them otherwise. I can use instanceof to check the type. if it is a Number, I can cast it and then use methods available on number.

ps - You aren't required to post a link to the book. It's helpful though in case someone has a question on the context.
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:They are just trying to show you can. It's rare to actually need to.

One reason is if you don't write all the code yourself. Maybe someone gives you a list of Objects and you know they are of a certain type.

There's also an instanceof operator so you can check the type before casting. For example, you give me a list of objects. I want to do math if they happen to be numbers of just print them otherwise. I can use instanceof to check the type. if it is a Number, I can cast it and then use methods available on number.

ps - You aren't required to post a link to the book. It's helpful though in case someone has a question on the context.


Mainly for illustrative purposes -- roger that!

How come casting is required at all if the method is chosen by the object stored in the reference variable? The casting is performed on the reference variable, if I understand it correctly, but the method is chosen by the object it points to.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Roberts wrote:How come casting is required at all if the method is chosen by the object stored in the reference variable? The casting is performed on the reference variable, if I understand it correctly, but the method is chosen by the object it points to.

The compiler is trying to protect you. It sees an Object and says "there is no m() method on Object". Casting is Java's way of informing the compiler that you really want to do this.

This works differently in some other languages. Read up on "duck typing" if your are curious. The gist is that the method will be called if it exists and blow up at runtime if it doesn't exist.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you gave does not match the code that's in the book's section that discusses casting. This is what's adding to your confusion.

First, when you extend Object, there's no need to explicitly write "extends Object"; when the extends clause is omitted, Object is implicitly the superclass. I suppose "extends Object" is there for clarity, which is fine but you should know that this is not standard practice.

Second, the book shows an empty class A, without a method m().

Third, the example in the book was to illustrate how casting works. To illustrate polymorphism, the code you gave makes more sense with the following main() method:


You are more likely to see usage similar to this in real-world code. The class A would be the "base" class that defines the general contract for behavior. The class B and other subclasses of A can then define variations to the general contract defined by A. The typical example you would find in tutorials would be Animal as the base class with a method speak(). Subclasses would be things like Dog and Cat. Dog.speak() would print "Arf" while Cat.speak() would print "Meow", for example. The polymorphic nature of the objects is exercised when you refer to Dog and Cat instances with an Animal variable reference.

Output of this program would be:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!