Diptopol Dam
Diptopol Dam

Reputation: 815

Creation of a reference of an object

I have a conceptual question regarding the reference of an object in java.

Here Num is an interface

public interface Num {
    void sum();
}

Num2 which implements Num

public class Num2 implements Num{
    @Override
    public void sum() {
        System.out.println(getRandom()+getRandom());
    }

    public int getRandom() {
        Random randomNumber = new Random();
        int number = randomNumber.nextInt(30);
        return number;
    }
}

And the main function

Num n = new Num2();
n.sum();

Here I know that n is a reference of object Num2 and n is an pointer pointing to the object Num2. Num2 contains both the method sum and getRandom .But when we try to access method through n reference we can get only sum method. My question is that how can a pointer know which method are contained in Num. How and which information are stored in the stack for the reference during initialization of the object.If I have any misconception correct me.

Upvotes: 2

Views: 92

Answers (6)

James Dunn
James Dunn

Reputation: 8264

In java, when Child extends Parent (or implements) and you write Parent object = new Child(), you have created a Parent reference to a Child object in memory.

Once your code is compiled, the JVM will deal with the object in memory and it will know that the reference variable object actually refers to an object of type Child in memory (in your case, that n is of type Num2).

But until then, you have to deal with the compiler. The compiler only cares about the type of the reference, which in this case is Parent (or in your case Num), and as such will only let you call methods off of it that are declared in the Parent (Num) class.

One way to get around this is to do a cast, like so:

((Num2) n).getRandom();

Be sure only to do this if you know for sure that n is (or will be) actually pointing to an Object of type Num2 in memory! Otherwise, you will get a ClassCastException.
Here you are telling the compiler, "Trust me. I know that this is a Num2, so treat it like one."

To sum up:

  • Num n = new Num2() declares a reference variable and creates an Object in memory
  • The variable is of type Num, and that's all the Compiler knows
  • The object created in memory is of type Num2, and the JVM will know this
  • To run the JVM you have to satisfy the Compiler
  • In cases like this, You can satisfy the Compiler by casting.

Upvotes: 0

AllTooSir
AllTooSir

Reputation: 49352

You are defining the variable n as of type Num interface and hence you can invoke only the methods declared in Num . This resolution I believe is done at the compile time itself. Compiler determines what fields or methods are accessible by using a reference variable based on its type.

But remember the runtime will invoke the method on the actual object type , i.e. the class implementing the interface.

A variable of a class type T can hold a null reference or a reference to an instance of class T or of any class that is a subclass of T.

Look at the below code :

interface A {
  void method1();
}
class B implements A {
  public void method1() {
  }
  public void methodB(){
  }
}
class C implements A {
  public void method1() {
  }
  public void methodC(){
  }
}
public class InterfaceTest {
   public void testMethod(A a){
       // this is safe because whatever may be the run time type of actual
       // object `a` is referring to , that object will always implement
       // method1.
      a.method1();
      // this cannot be allowed because compiler doesn't know 
      // what will be the actual run time object `a` will refer to
       // It may or may not be an object of B.
      a.methodB(); 
   }
}

Upvotes: 3

Aniket Thakur
Aniket Thakur

Reputation: 68905

My question is that how can a pointer know which method are contained in Num?

At compile time it will only check that the function or method called by the reference pointer is declared(not necessarily defined) in the class of reference pointer. At runtime entire inheritance tree is parsed in top down fashion and the correct function implementation is selected.

Also the reference pointer that you hace mentioned is in the stack while the actual object is in the heap. And the object has it's class information.Let me give an example -

    Animal animal = new Dog();
    System.out.println(animal.getClass());

will print class Dog and not class Animal.

Upvotes: 1

Abubakkar
Abubakkar

Reputation: 15644

I think the following goes behind it (correct me if I am wrong):

When you create a reference Num n, then somewhere in the memory this get created with its properties.

So there it must be defining the method and such things that can be accessed with this reference.

Now when you refer it to an object, the object is a separate entity in the memory. When you try to access using the reference, the compiler must be using the reference meta-data to determine which method can be invoked using that reference and so on.

Upvotes: 1

morgano
morgano

Reputation: 17422

The compiler (not in runtime) is in charge of validate that you treat your object like a Num rather than a Num2

Upvotes: 2

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

You can only access to the method of the type defined to the variable at compile time. Since your n variable is from Num type, you can only use the methods defined in the Num interface. Note that the behavior of these methods will be defined by the real object reference type, which in this case is Num2.

Upvotes: 1

Related Questions