4

I have 2 classes in JAVA:

Parent Class:

public class Parent {
    private int age;

    public void setAge(int age) {           
        this.age = age;
    }

    public int getAge(){
        return this.age;
    }       
}

Child Class:

public class Child extends Parent{      

    public static void main(String[] args){

        Parent p = new Parent();
        p.setAge(35);
        System.out.println("Parent   "+p.getAge());

        Child c = new Child();
        System.out.println("Child  " + c.getAge());         
    }
}

Output is:

  Parent   35
  Child     0

The private members are not inherited in JAVA When calling getAge() method on child class instance, why does it run successfully and even gives output as 0?

royki
  • 1,593
  • 3
  • 25
  • 45
tryingToLearn
  • 10,691
  • 12
  • 80
  • 114

9 Answers9

8

private member is not inherited, but public methods accessing it yes.

When you create an object of type Child all variables of the superclass are created also if not directly usables.

If there are public methods in the superclass you can access them from the subclass. And if those methods access the private variable values of the superclass you can indirectly via public methods access them.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • Thanks but since the method has now been inherited and it is being called from sub class instance, won't it just look for variables in it's own scope? – tryingToLearn Jul 24 '15 at 06:32
  • 2
    No, the body of the public method of the superclass can access private variables and also other private methods of the superclass. The implementation details are hided to the subclass that doesn't know how the data are collected. – Davide Lorenzo MARINO Jul 24 '15 at 06:36
3

Access modifiers as the name suggest can only affect the accessibility of variables/methods not inheritance, you cannot access age variable directly in your Child class as it is private but it doesn't mean that it is not present for child object.

So in your case the two public methods are accessible from both classes but inside your Child class you cannot use age directly unless you change its access modifier to protected or public

For more better understanding have a look Controlling Access to Members of a Class

Also you can check Do access modifiers prevent inheritance?

SSH
  • 1,609
  • 2
  • 22
  • 42
1

Inheritance : You are getting value from method, not from variable.

Default Value :

  • Class Type (Non-primitive, Object) : null
  • Primitive Type (int, float, etc) : 0

Please refer Java docs about inheritance : https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Vishal Zanzrukia
  • 4,902
  • 4
  • 38
  • 82
1

For the Parent class there is no constractor , therefore JAVA makes a defualt constractor.

When Child extends Parent he actually first go the his parent constactor.

You can build in Parent class this constructor:

public Parent(){
   this.age = 15;
}

and you will see that the child age will be 15 when you print it.

Udi
  • 598
  • 8
  • 19
0

You are accessing getAge() method which is public and is legal. The implementation of getAge() is accessing it's private members, not you.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

One of the most fundamental aspects of a programming language is how it initializes data. For Java, this is defined explicitly in the language specification. For fields and array components, when items are created, they are automatically set to the following default values by the system:

numbers: 0 or 0.0
booleans: false
object references: null 

This means that explicitly setting fields to 0, false, or null (as the case may be) is unnecessary and redundant. Since this language feature was included in order to, in part, reduce repetitive coding, it's a good idea to take full advantage of it. Insisting that fields should be explicitly initialized to 0, false, or null is an idiom which is likely inappropriate to the Java programming language.

Furthermore, setting a field explicitly to 0, false, or null may even cause the same operation to be performed twice (depending on your compiler).

André Schild
  • 4,592
  • 5
  • 28
  • 42
0
public class Child extends Parent{      

    public static void main(String[] args){

        Parent p = new Parent();
        p.setAge(35);
        System.out.println("Parent   "+p.getAge());


        Child c = new Child();
        System.out.println("Child  " + c.age);      //can't access private 
        System.out.println("Child  " + c.name);     //can access public variable
        System.out.println("Child  " + c.getAge()); //can access public method
        System.out.println("Child  " + c.getName());//can,t access private method 

    }
}

class Parent {
    private int age;
    public String name;

    public void setAge(int age){

        this.age = age;
    }

    private String getName(){
        return name;
    }

    public int getAge(){
        return this.age;
    }

}
  • Private variable can't be access outside the class. But we can make it accessible through getter and setter. Here OOP concept of Encapsulation comes in.
  • public methods, variables defined in the Parent class can be accessible in the child class. Here OOP concept of Inheritance comes in.
Bruce
  • 8,609
  • 8
  • 54
  • 83
0

Adding a few cents of mine. Child class cannot access the private variable age which part of Parent class. But age state is still part of the Child object. You can access using get and the set method. To illustrate it further, you can invoke setAge() on the child object to set proper age of child.

public class Child extends Parent {
    public static void main(String[] args) {
        Parent p = new Parent();
        p.setAge(35);
        System.out.println("Parent "+p.getAge());

        Child c = new Child();
        c.setAge(20);
        System.out.println("Child " + c.getAge());
    }
}

Output:

Parent 35
Child 20
Ganesh Kumar
  • 3,220
  • 1
  • 19
  • 27
0

Private member not inherited right , but we can access it by the public method . So in your case private member is access by public method . And the reason of value is zero , because the value of variable age by parent class object not child class object . so when you access value of age variable by child class object , it print the default value of variable age .