1 / 11

Encapsulation

Encapsulation. Encapsulation and inheritance. Private instance variables and methods in a base class cannot be directly accessed (by name) in a derived class. They can be indirectly accessed (via accessors and mutators in the base class).

careyh
Download Presentation

Encapsulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Encapsulation

  2. Encapsulation and inheritance • Private instance variables and methods in a base class cannot be directly accessed (by name) in a derived class. • They can be indirectly accessed (via accessors and mutators in the base class). • It’s exactly as if they don’t exist. (They can actually be defined and redefined in the derived class.)

  3. What if private instance variables could be accessed in subclasses? Problems: • Subversion: Then to change them, all we would have to do is to make a subclass of the base class and then go ahead and change them. • Errors: The more common problem is that the accessors and mutators guard against unintentional errors.

  4. Protected access Protected (rather than public or private) access allows: • Access by name inside its own class definition. • Access by name inside any class derived from it. • Access by name in the definition of any class in the same package (even if the class is not derived from it).

  5. Protected access • Access between private and public • Very weak protection • Use is discouraged (use the following (viz., package access) instead)

  6. Package access • AKA default access or friendly access. • Can be accessed by name by anything in the package but nothing outside of the package.

  7. Package access • This is what you get when you don’t specify either public, private, or protected (hence the name default access). • (If you don’t specify a package, you belong to the default package.)

  8. Package access • More restricted than protected. • Removes “Access by name inside any class derived from it.” if the derived class is NOT in the same package. • Packages are analogous to directories (folders). If you control the directory, you control the package.

  9. package somePackage; public class A { public int v1; protected int v2; int v3; //package access private int v4; } package somePackage; public class B { can access v1, v2, and v3. cannot access v4. } package somePackage; public class C extends A { can access v1, v2, and v3. cannot access v4. } //default package public class D extends A { can access v1 and v2. cannot access v3 and v4. } //default package public class E { can access v1. cannot access v2, v3, and v4. } Access example

  10. Access Access is the same for: • static variables • instance variables • static methods • ordinary methods

  11. Summary public private protected package

More Related