Thursday, April 25, 2013

Class access modifiers

There are a lot of access modifiers for declaring a class, a method or a variable and most of them are always asked during the initial interviews as they give the interviewer a first opinion about the level of the candidate.

There are four access levels: public, private, default and protected.
public: All other classes, regardless of the package they belong to, can access the member.
private: can be accessed by code only in the same class.
default: can be accessed by any element in the same package.
protected: It's the same as the default but it can also be accessed by the subclasses.

There are some more modifiers (nonaccess)
  •  Classes can also be modified with final, abstract, or strictfp
  • A class cannot be both final and abstract
  • A final class cannot be subclassed.
  • An abstract class cannot be instantiated.

No comments:

Post a Comment