In Java API it says
"A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass."
Can you please give me an example of this?
Accessing Private Members In Inheritance
Moderator: admin
-
- Posts: 27
- Joined: Thu Feb 07, 2013 9:46 pm
- Contact:
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Accessing Private Members In Inheritance
Where did you read that? Please post a link so that the context can be understood.
If you like our products and services, please help us by posting your review here.
-
- Posts: 27
- Joined: Thu Feb 07, 2013 9:46 pm
- Contact:
Re: Accessing Private Members In Inheritance
Hi,
The link is
http://docs.oracle.com/javase/tutorial/ ... asses.html
Please check "Private Members in a Superclass" section
The link is
http://docs.oracle.com/javase/tutorial/ ... asses.html
Please check "Private Members in a Superclass" section
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Accessing Private Members In Inheritance
Code: Select all
package exercise;
class Outer {
private int x = 10;
class Inner{
//lets the subclass of Outer have indirect access to Outer's private member x
public void changeX(int val){
x = val;
}
}
public void printX(){
System.out.println(x);
}
}
class NewOuter extends Outer{
public void accessPrivate(){
Inner inner = new Inner();
//change super class's private member x
inner.changeX(20);
printX();
}
}
public class PrivateTest {
public static void main(String[] args) {
NewOuter o = new NewOuter();
o.accessPrivate();
}
}
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 129 guests