Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Java Doubt:

what happens when we use
Java
Sample sample // this variable for Sample class

but referring to Sample1(another class).

Somebody please explain in flow..

What I have tried:

Java
Sample sample = new Sample1();


what is happing in this statement In java please tell at memory level.
Posted
Updated 17-Aug-16 22:57pm
v2
Comments
Richard MacCutchan 27-May-16 9:03am    
A new Sample1 object will be created and its constructor executed. What happens at memory level will depend on how the class has been defined.

Disclaimer: without seeing the class declarations, the following is just a wild guess.


That's called polymorphism, see, for instance: Polymorphism in Java - javatpoint[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-May-16 9:22am    
5ed, but not sure that the inquirer will get it all.
I want to say: there is no a wild guess here. There are only two possibilities: the line shown will compile or not. It it compiles, there is an assignment compatibility between classes, otherwise they are not assignment-compatible. Well, yes, depending in class members, it may or may not make practical sense. :-)

Perhaps it could be confusing how a single object can be related to polymorphism. In just one line of code, polymorphism is already there, but in potential.

—SA
CPallini 27-May-16 10:57am    
Thank you.
Maciej Los 27-May-16 9:57am    
A BIG 5!
CPallini 27-May-16 10:57am    
A BIG THANK YOU! :-)
Java programs have no direct access to memory so you should not concern yourself with memory questions. The memory implementaion may be specific to a Java VM implementation. However, I can outline a general process for you.

In order for this code to work, Sample1 class must be a descendant of Sample class. Each class is represented in memory by data (class member variables) and a vtable. Vtable is an array of pointers to virtual methods belonging to this class. For example, if Sample class is defined as follows:
Java
public class Sample {
  int var1;
  int var2;
  Sample() {}
  public void method1() {}
  public void method2() {}
}

it may be represented in memory like this:

vtable pointer<br />
var1<br />
var2


where vtable pointer points to this array of pointers to methods:

<br />
0: method1 from Sample class<br />
1: method2 from Sample class


If you have a descendant class Sample1 like this one:
Java
public class Sample1 extends Sample {
  int var3;
  Sample1() {}
  @Override
  public void method1() {}
  @Override
  public void method2() {}
}

it will be represented in memory like this:

vtable pointer<br />
var1<br />
var2<br />
var3


where vtable pointer points to this array of pointers to methods:]

0: method1 from Sample1 class<br />
1: method2 from Sample1 class


When you create a Sample1 class instance with new, it will create the above memory structure for Sample1 class. When you assign it to a variable of Sample type:
Java
Sample sample = new Sample1();

compiler will allow this because Sample1 class can be used as Sample class without problems. Indeed, a Sample1 class instance has the same variables (var1, var2) as the Sample class and at the same positions. As for Sample1 class vtable, it has the methods with the same signature and at the same positions as the Sample class. So you can safely work with Sample1 class through a Sample variable as it is compatible with it. I hope this explanation will help you.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-May-16 11:21am    
5ed. I would add that this is the case when sample has compile-time type Sample, which is different from the runtime type Sample1. Indeed, this is the basis of polymorphism.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900