Kishore Kumar Korada
Kishore Kumar Korada

Reputation: 1264

Accessing private instances in child class

I've seen answers for questions related to mine on Stack Overflow, but I am still left with some ambiguity. A parent class method has access to its own private instance variables. If a child class inherits the class, what happens when the getA() method is called on an instance of the Child class? Does it return the a from the Parent class or the a from the Child class?

class Parent {
    private int a = 10;  
    public int getA() {
       return a;
    }
 }

class Child extends Parent {
    private int a = 22;              
 }

public class Test { 
   public static void main(String []args) throws Exception {        
       Child c = new Child(); 
       System.out.println(c.getA());
   }    
}

Upvotes: 2

Views: 834

Answers (5)

Fields cannot be overridden.

In your code, an instance of child (like the one referred to by c) has two different fields, which are both called a.

You cannot access Parent's private variables inside Child, full stop. That's the entire point of private. There is nothing* you can write inside Child to make the parent's a field equal 22.




* Technically, you could with reflection. Reflection doesn't count though, since its purpose is essentially to allow you to break things, and do things that are otherwise impossible.

Upvotes: 1

irfan shafi
irfan shafi

Reputation: 524

Stick to basics "private member variable will only be accessed within the class by its own member functions and fields cannot be inherited" so basically when you are accessing the private variable A in parent class , the method is supposed to access its own private member rather then the private field of any child class.

Upvotes: 1

Loki
Loki

Reputation: 4130

As the Method getA() is inherited, if you call this Method, you'll always invoke the Parent's Method.

The current Object will be treated as a Parent and not as a Child, and the Parent's a will be returned.

Even though you have your own variable a, this variable wont override the Parent's a. They are different from each other, have different addresses and different values.

If you want getA() to return Child's a, you need to override the Method to return your new variable.

class Child extends Parent {
    private int a = 22;

    @Override
    public int getA(){
        return a;
    }
 }

You could also "go crazy" and do stuff like the following:

class Child extends Parent {
    private int a = 22;       

    @Override
    public int getA(){
        int superA = super.getA();

        return a+superA;    
    }
 }

That way you could return the sum of Parent's and Child's a.

(Just an example)

Upvotes: 3

Shriram
Shriram

Reputation: 4411

Private variables are local to the class and in your code you are inheriting the properties of the parent class so you can access getA() and it will return the parent's attribute. And you cannot access child's variable unless you have public getter method for child attribute.

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521259

This is a duplicate of this SO post.

The variable a in subclass Child hides the a in the parent class Parent.

Upvotes: 2

Related Questions