- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Are the private variables and private methods of a parent class inherited by the child class in Java?
No, a child class can’t inherit private members of the parent class, it can inherit only protected, default, and public members of it. If you try it gives you a compile time error as: −
Example
class Super{ private int data = 30; public void display(){ System.out.println("Hello this is the method of the super class"); } } public class Sub extends Super{ public void greet(){ System.out.println("Hello this is the method of the sub class"); } public static void main(String args[]){ Sub obj = new Sub(); System.out.println(obj.data); } }
On executing this example it will give an compiletime error as shown below −
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The field Super.data is not visible at Sub.main(Sub.java:13)
- Related Articles
- How to access the private methods of a class from outside of the class in Java?
- Can private methods of a class be accessed from outside of a class in Java?
- Why static methods of parent class gets hidden in child class in java?
- Private and final methods in Java Programming
- Private Methods in Java 9 Interfaces
- Can we declare the variables of a Java interface private and protected?
- What are the advantages of private methods in an interface in Java 9?
- Private Variables in C#
- Private Variables in Python
- Can we override private methods in Java
- Can I overload private methods in Java?
- Private Methods in C#
- What all is inherited from parent class in C++?
- Private and final methods in C#
- What is the scope of a private member variable of a class in C#?

Advertisements