Can we access private members of the parent class ? i.e Are private members of the class visible in the derived class ?
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers

 Q1. Can we access private members of the parent class ? i.e Are private members of the class visible in the derived class ?Core Java
Ans. No, If both Parent and Derived are outer classes.

public class Vehicle {
private static String manufacturingDate = "2016";
}

public class Car extends Vehicle{
public static void main(String args[]){
System.out.println(manufacturingDate); // error - The field Vehicle.manufacturingDate is not visible
}
}

Yes, If derived is the inner class of Parent.

public class Vehicle {
private static String manufacturingDate = "2016";
static public class Car extends Vehicle{
public static void main(String args[]){
System.out.println(manufacturingDate); // no problem
}
}
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     private members   private methods   private variables   inheritance  object oriented programming (oops)  oops concepts   members visibility   inner classes  nexted classes      basic        frequent


Related Questions

  Difference between Public, Private, Default and Protected ?
  Which access specifiers can be used with top level class ? a. public or default b. public or private c. public or protected d. protected or default
  Enums cannot be declared ..
 What is the difference between these two method declarations ?

private static void method(String[] arg)

and

private static void method(String... arg)
  Can we declare interface methods as private ?
  How can we create objects if we make the constructor private ?
  Why are Getter and Setter better than directly changing and retrieving fields ?

or

What is the significance of variable hiding in Java ?

or

Why it's preferred to keep variables private and allow their access through getter and setter methods ?
  enums are intrinsically ..
  Can we instantiate the object of derived class if parent constructor is protected ?
  Can we declare an abstract method private ?



Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: