0
///Example code here
Superclass{ 
    method1(){ 
      print(do1);
     }
    method2(){}
  }
Subclass extends Superclass{
   ///override method1
 method1(){ 
      print(do2);
    }
 method3(){}
 }

I have a question for the polymorphism in java, which is when Superclass s =new Subclass(). the "s" object always invoke the method in the Superclass, but when the override method happened, the "s" will point back to the override methods.

Update: So, the question is who will create a heap address for the reference "s", if it can be compiled and running in the end. If the Superclass created it, why new Subclass() rather than new Superclass(). If the Subclass created it, why cannot use s.method3().

trincot
  • 317,000
  • 35
  • 244
  • 286
Field.D
  • 158
  • 1
  • 1
  • 9
  • 5
    It's really unclear what you're asking here. Could you provide a [mcve] which doesn't behave as you expect it to? (Aside from anything else, I would ignore any stack/heap distinctions - concentrate on "reference" vs "object" and "compile-time type" vs "execution-time type". Those are far more relevant to the discussion.) – Jon Skeet Sep 02 '16 at 06:16
  • @fabian, sure, s is declared as Superclass, but the heap address is created for the Subclass right? and the "s " is point to the HEAP – Field.D Sep 02 '16 at 06:19
  • @JonSkeet, this confused me because I always think new Object() actually create an NEW ADDRESS IN HEAP, so the reference should point to the real address no matter what – Field.D Sep 02 '16 at 06:23
  • Again, you're focusing on the stack vs heap without any clear explanation of what's confusing you. – Jon Skeet Sep 02 '16 at 06:24
  • It simply doesn't matter what `s` points to at runtime. The **compiler** won't allow you to call `method3` on a expression of type `Superclass`. This happens before stack/heap exist in a program using that code. You can access `method3` of `s` by casting, but if the object isn't really a `Subclass` subclass of `Subclass`, you'd get a `ClassCastException` at runntime: `((Subclass) s).method3()`. – fabian Sep 02 '16 at 06:35
  • @JonSkeet Simply saying, why method3() cannot be called in JVM based on the fact that the HEAP has created an address for Subclass – Field.D Sep 02 '16 at 06:36
  • What do you mean by "in the JVM"? You can't call it from source code due to the *compile-time* type of the variable. Yet again, this is not about stack/heap. Again, you should provide a [mcve] to clarify your question. – Jon Skeet Sep 02 '16 at 06:38
  • @fabian this is the confused thing, if the compiler defined the storage logic in stack/heap, how it stop this happen from the beginning. Suppose I debug this code till to the "s" created. Before i call s.method3(), everything should be fine, which means the "s" has already been existed in the stack with the related Heap address. So how to explain an existed Subclass address in the heap cannot call its own method3. – Field.D Sep 02 '16 at 06:52
  • Why are you *still* talking about the stack and the heap? I've explained several times that you should be focusing on the difference between the compile-time types and execution-time types involved. At compile-time, the type of `s` is `Superclass`, therefore the compiler won't let you call any methods which aren't declared in `Superclass` or one of its superclasses. But at execution time, the value of `s` will be a reference to an instance of `Subclass`. It's as simple as that. – Jon Skeet Sep 02 '16 at 07:13
  • You should probably take a look at "static binding" and "dynamic binding" http://stackoverflow.com/questions/19017258/static-vs-dynamic-binding-in-java – patrik Sep 02 '16 at 07:28

2 Answers2

0

s can call method3. You just have to cast it first. If they allowed s to call method3 as Superclass, it wouldn't make sense because not ALL Superclass can call method3.

Kelvin
  • 574
  • 3
  • 13
0

Dynamic binding of method will decide at run times , so first when you call method with s as its reference of super class compiler will always first check if superclass has that method or not. there are 2 case

1. If super class has the method JVM will go ahead and check in child class, If child class has overriding method then that method will be called , if child class doesn't have that method than normally parent class method will call.

2. If super class does not have that method , then compilation fails as compiler could not identified any method in class of reference variable

So in your case if super class doesn't have method3 then it will defiantly gives compilation error

Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32
  • Again, this "s" reference is created by Superclass, which is only a reference in STACK. The JVM will go ahead to the real address to check which is HEAP. But here the HEAP is Subclass, how JVM knows the Superclass's address in HEAP? I do not think the Superclass have a physical address at that time – Field.D Sep 02 '16 at 06:32
  • JVM will go ahead only if compilation succeeded and for this parent class must have that method , because compiler in unaware of what will going to happen at run time, and even we should not forget that child class always inherit properies of parent class so if child doesn't have method it will call parent method – Jekin Kalariya Sep 02 '16 at 06:36
  • 1
    @Field.D This have nothing at all to do with memory allocation. In most cases you do not even have to bother about memory allocation in Java since this is handled by the JVM. This behavior is intended and an important part of the language. – patrik Sep 02 '16 at 07:13
  • 1
    In point 1 you mean *overriding*. Any *overload* resolution is performed at compile-time. It's important to distinguish between overloading and overriding. – Jon Skeet Sep 02 '16 at 07:13
  • thax Jon for correcting , its common typo from my side :) – Jekin Kalariya Sep 02 '16 at 07:58