0
class Parent {
    public Parent() {
        System.out.println("Parent Default..");
        System.out.println("Object type : " + this.getClass().getName());
        this.method();
    }

    private void method() {
        System.out.println("private method");
    }
}

class Child extends Parent {
    public Child() {
        System.out.println("Child Default..");
    }

    public static void main(String[] args) {
        new Child();
    }
}

When I run this code it prints the class name of "this" = Child but the "this" object is able to call the private method of parent class why?

Harshit Gupta
  • 719
  • 1
  • 9
  • 26

5 Answers5

2

when you extend the class the private methods will not be inherited .but the object of the child class contains object of the parent class so when the superclass constructor is called .you are able to call the superclass private method inside the super class

Richard Elite
  • 720
  • 5
  • 15
1

First of all, when calling new Child(), since there is not a declared non-argument constructor in Child class, it will simple call super() which is invoking the Parent constructor.

Then, when executing this.getClass().getName(), here this stands for a Child instance, this is why you get "Child" as the result. Remember, Object#getClass() returns the most specific class the object belongs to. see more from here.

About why this.method() works. First, because Child extends Parent, the Child instance is also a Parent instance. The java scope modifier controls where the methods or fields can be accessed. Taking Parent#method() as the example, the private modifier indicates that this method can only be accessed (invoked) inside the Parent class. And this is exactly how you code does. it invokes the method inside the constructor of Parent class, which compiles the rule. See more about java access control from here

glenlivet1986
  • 260
  • 3
  • 12
  • _"here this stands for a Child instance"_ instances aren't created, neight for child nor for parent, if you run Harshit Gupta's code. I am sure a "thing" like Child exists somewhere in memory( heap) but not the instance...check out `jvisualjvm` to verify that. If you do something like `Child ch = new Child()` then for sure you can see an `instance` being created but again not for Super class as we know. – Nirmal Jun 30 '15 at 06:14
0

private has nothing to do with the actual class of the object. A private member can be accessed by any code within the same top-level class. (A top-level class is one that's not nested, which is unrelated to inheritance)

method is defined in Parent, and the call this.method() is also in Parent, so it's allowed.

user253751
  • 57,427
  • 7
  • 48
  • 90
0

A parent instance is not created here, you can confirm this using jvisualjvm in the /bin folder of your jdk installation, the child instance is not created either. Parent constructor is still invoked.

output:

Parent Default..
Object type : com.packagename.Child
private method
Child Default..

Parent constructor can be invoked by child class. While in the constructor of parent, as Krishanthy Mohanachandran has noted above, a private method can be legally invoked.

Nirmal
  • 1,229
  • 1
  • 15
  • 31
-1

It is abvious, you can call any private members within the class but not outside the class.

In this case it is legal. In this program first the constrctor of the Parent will be called and you can call private method within the class.

RajSharma
  • 1,941
  • 3
  • 21
  • 34