• Post Reply Bookmark Topic Watch Topic
  • New Topic

What are private classes?  RSS feed

 
Kaush Kane
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are private classes? and is there any difference between inheriting a private base class and public base class??

Case 1
private class Base{
/*Here variables and methods of base class */
}

public class Child extends Base{
/*Here variables and methods of child class */
}





Case 2
public class Base{
/*Here variables and methods of base class */
}

public class Child extends Base{
/*Here variables and methods of child class */
}


In case 2 the Child class has access to the public and protected members of the Base class. Does this hold true even in Case1??
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because of access problems, you can't write a private class in a file of its own. You have to write it inside another class, when it is called an inner class.



You use private inner classes when you want to restrict access to the outer class only.
A common use is for ActionListeners. Example:-

If you want to extend a private class, your subclass would have to be in the body of the outer class, too. You can't have two public classes in the same file, so subclassing a private class would be difficult. Probably pointless too.

Did that help you, or confuse you more?

CR
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!