Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming

Similar presentations


Presentation on theme: "Object Oriented Programming"— Presentation transcript:

1 Object Oriented Programming
Object Oriented Programming Access Modifiers Lecture 10

2 Access modifiers: Public Protected Private
Access modifiers: Public Protected Private there are four access controls (levels of access) but only three access modifiers. The fourth access control level (called default or package access) is what you get when you don’t use any of the three

3 every class, method, and instance variable you declare has an access control, whether you explicitly type one or not. Although all four access controls (which means all three modifiers) work for most method and variable declarations, a class can be declared with only public or default access; the other two access control levels don’t make sense for a class

4 CLASS ACCESS

5 Default Access A class with default access has no modifier preceding it in the declaration. In other words, it’s the access control you get when you don’t type a modifier in the class declaration. Think of default access as package-level access, because a class with default access can be seen only by classes within the same package. For example, if class A and class B are in different packages, and class A has default access, class B won’t be able to create an instance of class A, or even declare a variable or return type of class A.

6 Public Access A class declaration with the public keyword gives all classes from all packages access to the public class. In other words, all classes in the Java Universe (JU) have access to a public class.

7 Other (Nonaccess) Class Modifiers
Other (Nonaccess) Class Modifiers

8 Final Classes When used in a class declaration, the final keyword means the class can’t be subclassed. In other words, no other class can ever extend (inherit from) a final class, and any attempts to do so will give you a compiler error.

9 Method and Variable Declarations and Modifiers
Method and Variable Declarations and Modifiers

10 Member Access Whereas a class can use just two of the four access control levels (default or public), members can use all four: ■ public ■ protected ■ default ■ private

11 Public Members When a method or variable member is declared public, it means all other classes, regardless of the package they belong to, can access the member

12 Private Members Members marked private can’t be accessed by code in any class other than the class in which the private member was declared.

13 Protected and Default Members
The protected and default access control levels are almost identical, but with one critical difference. A default member maybe accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.

14


Download ppt "Object Oriented Programming"

Similar presentations


Ads by Google