1 / 16

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). Encapsulation and inheritance.

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).

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

  4. What if private instance variables could be accessed in subclasses?

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

  6. Protected Let’s introduce another level of access called protected.

  7. 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).

  8. Protected access • is access between private and public • is very weak protection • It’s use is discouraged (use the following (viz., package access) instead).

  9. Package access Let’s introduce another level of access called package.

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

  11. 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.)

  12. 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. • Protected: Simply subclass it for access. • Package: Even if you are a subclass, you only have access if you are in the same package.

  13. Package access • More restricted than protected. • Packages are analogous to directories (folders). • If you control the directory, you control the package!

  14. 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 (5 separate classes in 5 separate files)

  15. Access Access is the same for (“everything”): • static variables • instance variables • static methods • ordinary methods

  16. Summary public private protected package

More Related