| Author |
How does an object find a method
|
Oceana Wickramasinghe
Ranch Hand
Joined: Mar 02, 2011
Posts: 77
|
|
hi guys, i have two questions
1.We can call a method in the same class using an in instance of that class
but if i call work() method on 'b' reference i should get a compiler error. why cant JVM find work() method inside class A using an instance of class B. but if i extend class A with class B my object gains the ability to call work() method. im really curious how it works, so i want a detailed explanation as to what goes inside JVM . which brings me to my next question
2.Does each class get its own stack and heap? if the answer is yes, what happens when you inherit one class with another, do the two stack/heap become one? Is that how my object(1st question) gain the ability to call work() method.
hope you guys help me,thanks in advance.
|
 |
Joseph Mokenela
Ranch Hand
Joined: Jan 18, 2011
Posts: 58
|
|
When you say , you are actually inheriting the methods of class A, so in that case you are not going to get an error when calling method work() on b reference.In your case, the two classes are not related hence you are going to get a compiler error.In other words, the code in class A is not visible from b reference
I hope that answers your question.
Cheers!
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10032
|
|
Oceana Wickramasinghe wrote:why cant JVM find work() method inside class A using an instance of class B.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12950
|
|
1. Classes A and B don't have anything to do with each other in your code example. If you try to call a method defined in class A on an instance of class B, then that is ofcourse not going to work. When class B extends class A, then class B inherits all the (non-private) methods of class A, so then you can use those methods in an instance of class B. See Inheritance in the Java Tutorials for a more detailed explanation of what inheritance means and how it works.
2. No, each class does not get its own stack and heap. Each thread has its own stack, and there's only one heap that is shared by all threads. Stack and heap have nothing to do with classes and inheritance or finding methods.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Oceana Wickramasinghe
Ranch Hand
Joined: Mar 02, 2011
Posts: 77
|
|
|
Thank you all for answering my question, but im not sure if i got the answer i was expecting, someone told me that an object finding a method has something to do with virtual method table, which i dont fully understand. i just wanna know how an object receives this information" hey you're only allowed to call methods in the same class that you belong to". when an object calls a method how does it know what class the method is in. hope someone can help me now.
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
This wikipedia article contains some/most of the information you are looking for.
http://en.wikipedia.org/wiki/Virtual_method_table
Hunter
|
"If the facts don't fit the theory, get new facts" --Albert Einstein
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32827
|
|
You would appear to have two questions there.
When the compiler goes through the code, it checks that the method requested is available in that class. If it is not available in that class, then it checks the superclass, and keeps going until it finds java.lang.Object. If the method hasn't been found by then, there is an error.
When the JVM executes an instance method, it finds the Class<T> Object from the instance being used at the moment. Then it looks in that Class<T> object for the requested method, and if it doesn't find it, it can find the superclass Class<T> object because all .class files have an "extends" declaration. The first method found is executed, so you get polymorphism.
|
 |
 |
|
|
subject: How does an object find a method
|
|
|