1

So, I was experimenting with some basic encapsulation in Java when I found that the way the members and functions are accessed from inside the extended class is different. I mean to say this:

public class A {
  int i = 10;
  public void print() {
    System.out.println(" inside A ");
  }
}

public class B extends A{
  int i = 20;
  public void print() {
    System.out.println(" in B");
  }

  public static void main(String args[]) {
    A a = new B();
    a.print();
    System.out.println(a.i);
  }
}

In the above case, for an object of the current class (the one that extends something super), method of the current class is invoked while member of the super class is accessed. So, what's the reason for this? Or, more generically, what's exactly happening when I say

A a = new B()

I mean, what's happening at the memory level?

Anoop Dixith
  • 633
  • 2
  • 8
  • 20
  • This is how inheritance is supposed to work: subclasses can override the behavior of superclass methods, and it doesn't matter how you store a reference to an object of a given type, it'll still have the behavior of the actual type it was created as. – Louis Wasserman Jan 28 '15 at 00:04
  • Parent hold the child instance. http://stackoverflow.com/questions/12159601/why-do-we-assign-a-parent-reference-to-the-child-object-in-java – MG. Jan 28 '15 at 00:05

3 Answers3

1

It's because all non-static, non-final and non-private methods are virtual by default in Java.

From Wikipedia Virtual Function:

In object-oriented programming, when a derived class inherits from a base class, an object of the derived class may be referred to via a pointer or reference of the base class type instead of the derived class type. If there are base class methods overridden by the derived class, the method actually called by such a reference or pointer can be bound either 'early' (by the compiler), according to the declared type of the pointer or reference, or 'late' (i.e. by the runtime system of the language), according to the actual type of the object referred to.

Virtual functions are resolved 'late'. If the function in question is 'virtual' in the base class, the most-derived class's implementation of the function is called according to the actual type of the object referred to, regardless of the declared type of the pointer or reference. If it is not 'virtual', the method is resolved 'early' and the function called is selected according to the declared type of the pointer or reference.

Virtual functions allow a program to call methods that don't necessarily even exist at the moment the code is compiled.

ZqBany
  • 201
  • 1
  • 5
1

What happens in such cases like A a = new B(); is called upcasting. So reference variable 'a' is referring to Child class B.

In Java we use this to best take advantage of inheritance at run time. Because you can have different Children and based on the need pass a reference of any of them so we make decision which one to be called on runtime. Please take a look at these examples. http://www.javatpoint.com/runtime-polymorphism-in-java

Hamed Moghaddam
  • 564
  • 3
  • 8
0

This is one of the mostly important thinks when we talk about object-oriented programming (oop).

In your case:

A a = new B();

in memory, the variable "a" it's B. But you declared as A. So, without any casting, you can only access A methods and fields using "a". You can however, cast it to B again.

B b = (B) a;

Other think,

public void print() {

in B class it's actually "overriding" the method with same name as A. Why would you do that? Well, this way you can reuse existing code in a lot of different ways!

xkothe
  • 633
  • 1
  • 5
  • 18