• Post Reply Bookmark Topic Watch Topic
  • New Topic

methods are checked based on the reference, not the object. why?  RSS feed

 
Daniel Lefkowitz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the following basic code (assume that dog has a method called bark):



But why? Isn't o just a pointer to a memory address which has the dog object? If so, why can't the compiler see that the object has a method called bark? Or, to ask the question another way, why is Java designed to check the object reference to see if the method exists instead of the object itself?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The type of the variable is known at compile time but the object is not created and assigned until runtime. So the compiler can tell what methods are available to the reference type of the variable but not the object because it doesn't exist yet.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you had written this?No. assigning that Dog object to an Object reference will not work. What you are telling the compiler is that you might have any type of Object there, so you can only rely on its having the eleven methods in the Object class.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Lefkowitz wrote:But why? Isn't o just a pointer to a memory address which has the dog object? If so, why can't the compiler see that the object has a method called bark? Or, to ask the question another way, why is Java designed to check the object reference to see if the method exists instead of the object itself?

Because Java is a language with static type checking, not dynamic type checking, which means that the compiler at compile time wants to check if types are correct. As Steve already said, what kind of object variable 'o' refers to is in general not known at compile time. Let's make the example a little bit more elaborate:

There are other programming languages with dynamic type checking, such as Python or Ruby.

Static and dynamic type checking both have their pros and cons. The good thing about static type checking is that the compiler can check more things, so that you will discover errors in your program sooner. People say that it is easier and faster to write programs in dynamically checked languages. I prefer statically checked languages myself.
 
Daniel Lefkowitz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies. Very informative. I'm kinda new to programming and am making my way through Head First Java. Nice to know that there's a friendly place for me to get answers if I can't find one with google
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Lefkowitz wrote:Thanks for the replies. Very informative. I'm kinda new to programming and am making my way through Head First Java. Nice to know that there's a friendly place for me to get answers if I can't find one with google

You're welcome, and that's what we're here for. Also glad to hear that you're trying Google first.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!