- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why subclass doesn't inherit the private instance variables of superclass in Java?
When you declare the instance variables of a class private, you cannot access them in another class if you try to do so a compile-time error will be generated.
But, if you inherit a class that has private fields, including all other members of the class the private variables are also inherited and available for the subclass.
But, you cannot access them directly, if you do so a compile-time error will be generated.
Example
class Person{ private String name; public Person(String name){ this.name = name; } public void displayPerson() { System.out.println("Data of the Person class: "); System.out.println("Name: "+this.name); } } public class Student extends Person { public Student(String name){ super(name); } public void displayStudent() { System.out.println("Data of the Student class: "); System.out.println("Name: "+super.name); } public static void main(String[] args) { Student std = new Student("Krishna"); } }
Compile-time error
Student.java:17: error: name has private access in Person System.out.println("Name: "+super.name); ^ 1 error
To access the private members of the superclass you need to use setter and getter methods and call them using the subclass object.
Example
class Person{ private String name; public Person(String name){ this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void displayPerson() { System.out.println("Data of the Person class: "); System.out.println("Name: "+this.name); } } public class Student extends Person { public Student(String name){ super(name); } public void displayStudent() { System.out.println("Data of the Student class: "); } public static void main(String[] args) { Student std = new Student("Krishna"); System.out.println(std.getName()); } }
Output
Krishna
- Related Articles
- Referencing Subclass objects with Subclass vs Superclass reference
- Instance variables in Java
- What happens when a subclass object is assigned to a superclass object in Java?
- What are class variables, instance variables and local variables in Java?
- What is the difference between class variables and instance variables in Java?
- Are the private variables and private methods of a parent class inherited by the child class in Java?
- Private Variables in C#
- Private Variables in Python
- Can we declare the variables of a Java interface private and protected?
- Private Variables in Python Program
- Class with a constructor to initialize instance variables in Java
- Can we access the instance variables from a static method in Java?
- Why use instance initializer block in Java?
- What are the default values of instance variables whether primitive or reference type in Java?
- How many ways are there to initialize the instance variables of a class in java?

Advertisements