1

I thought of this forum while looking for clarification of a statement in java literature. The statement is;

"When you call an objects method, Java looks for that method definition in the object's class. If it doesn't find one, it passes the method call up the class hierarchy until it finds a method definition."

My confusion originates from trying to understand the purposeful reasoning for coding differently when coding for a class then when coding for an object.

I think the coding differences are due to class(s) are physically located in a different part of memory then where the object is placed, when the program is loading into memory.

The class is loaded into the "Data" section, while the object is loaded into the stack or heap.

Getting back to the statement in the Java literature.

If I am trying to call an object's method, the instantiated class, the objects blueprint, that method is in the stack or heap within its object.

So why would Java look for the objects' method, in different parts of the data section?

  • See [The difference between Classes, Objects, and Instances](https://stackoverflow.com/questions/1215881/the-difference-between-classes-objects-and-instances). – Jesper Aug 28 '19 at 11:12
  • 1
    you don't "code for an object" - all Java code is classes, and objects are _instances_ of those classes. – Alnitak Aug 28 '19 at 11:12
  • 1
    That quote isn’t about memory but about inheritance, if a class doesn’t contain the method called the super class will be checked and so on. – Joakim Danielson Aug 28 '19 at 11:14
  • Terms you want to research: A) polymorphism and maybe B) vtable (that is the mechanism that is used to actually determine which method to call) – GhostCat Aug 28 '19 at 11:16

2 Answers2

2

When you call an objects method, Java looks for that method definition in the object's class. If it doesn't find one, it passes the method call up the class hierarchy until it finds a method definition

Lets understand its meaning by use of an example.

Consider the class

class BestFriend { 
    String name; 
    int age; 

    BestFriend (String name, int age) 
    { 
        this.name = name; 
        this.age = age; 
    } 
public String toString() 
    { 
        return name + " " + age + " "; 
    } 
public static void main(String[] args) 
    { 
        BestFriend friend = new BestFriend ("Gulpreet Kaur", 21); 
        System.out.println(friend ); 
        System.out.println(friend.toString()); 
    } 
} 
  • Here object I created is "friend", when I called the object method toString(), it looks for that method definition in BestFriend class.Since I have implemented toString() method it will call that method.
  • If I would not have implemented the toString method in my BestFriend class, it passes the method call to Object class, Every class in java is child of Object class either directly or indirectly. Object class contains toString() method.
  • Hence it passes the method call up the class hierarchy until it finds a method definition
Shubham Chopra
  • 1,678
  • 17
  • 30
  • 5
    Hint: when writing example code, follow Java naming conventions. It should be "BestFriend", not Best_Friend. _ only goes into SOME_CONSTANT. – GhostCat Aug 28 '19 at 11:15
0

First of all, forget about the "Data" section, stack or heap for this discussion; where things like the class definition are loaded into memory and where objects are stored is not really relevant to understand the concepts of how classes and objects work. These things are implementation details of the Java virtual machine that are not relevant to understanding the main concepts.

A class is a blueprint for making objects - it describes what objects of a specific type look like (what member variables such objects have and what methods you can call on such objects).

The sentence you quoted explains where Java will look when you try to call a method on an object. It will first look in the object's class - for example if you have a String object (which is an object that's an instance of class String), and you call a method on it, Java is first going to see if it can find that method in class String itself.

If it cannot find it there, it's going to look into the superclass of class String - which happens to be class java.lang.Object. If the method you're trying to call exists there, then Java calls that method.

The key concepts that are being explained here are inheritance and polymorphism - concepts from object oriented programming.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Jasper, thank you so much. The knowledge of knowing I am coding for class on for an object helps a great deal. While waiting for help on this posting, I was trying another example from the book of that quote. It makes it much easier with respect to narrowing my focus on inheritance and polymorphism, instead of whether its an object or not. – user5610278 Aug 29 '19 at 12:26