0

I'm currently learning JAVA and got confused on the access in inheritance. The case below:

public class Father{
    private int age;

    public void setAge(int a){
        age = a;
    }

    public void getAge(){
        System.out.println(age);
    }
} //End of Father

public class Son extends Father{

} //End of Son

public class Test{
    public static void main(String[] args) {
        Father F = new Father();
        Son S = new Son();

        F.setAge(40);
        F.getAge();

        S.setAge(20);
        S.getAge();

        //System.out.println(F.age);
    }
} //End of Test

In the case above, "age" is a private variable in Father. Although Son extends Father, this private variable "age" is not inherited. Which means there is no variable in Son.

But when I put them into Test, running result shows "40" and "20", as if there was an int variable in Son. How to explain this?

3
  • Good answers are already given below, but one design comment... logically it would seem better to rename your parent class to "Human" or "Person" rather than "Father". There's nothing in the Father class which is specific to "human who has offspring", so it would probably serve you well to name it something more generic. Commented Mar 17, 2014 at 21:19
  • Thanks guys, I just found another question stackoverflow.com/questions/4716040/… and that explained thoroughly. Commented Mar 17, 2014 at 21:21
  • Lesson learnt, many thanks :) @paulk23 Commented Mar 17, 2014 at 21:23

4 Answers 4

2

age is inherited. It's just that you can't access it directly from son.

If you want to access it directly in son then mark it as protected and not private, i.e. declare

 protected int age;

in the Father class.

(By the way, the normal thing to do would be to have getAge() return an int, and your calling function write the age to the console).

Sign up to request clarification or add additional context in comments.

Comments

0

setAge() and getAge() are the public methods and the instance variable is being accessed through those methods, which are directly inherited. The instance variable is not being accessed directly, i.e. by direct manipulation outside methods in the class Father.

Comments

0

Private variables are still there in subclasses; they are just not visible to the subclass. Since the getAge() method is public in the superclass, and thus in the subclass, you can still invoke it to cause the private variable to be printed.

2 Comments

lol, meant to add the example to my answer but we all jumped on this one :-)
Yeah, I don't think your edit makes any sense in the context of my answer. In the context of your answer, it would make sense.
0

Your setters and getters are public so they are exposed in to the sub-class Son. The code for the setter and getter is contained in the super-class Father which has access to the private field 'age' in that class definition.

If you were to change the setter and getter to private then the sub-class Son would not have access to age field in the super-class Father.

public class Father{
    // only this class may access the field directly
    private int age;

    // this class,sub-classes and package classes may set the age
    protected void setAge(int a){
        age = a;
    }

    // you can get the age from anywhere
    public void getAge(){
        System.out.println(age);
    }
} //End of Father

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.