Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.

Similar presentations


Presentation on theme: "JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member."— Presentation transcript:

1 JAVA ACCESS MODIFIERS

2 Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member variables - Its methods and constructors The access modifiers are: - Public - Protected - Private

3 Private Least generous modifier. Top-level classes (not inner classes) may not be declared private. A private variable or method may only be used by an instance of that class that declares the variable or method.

4 Default Is the name of the access of classes, variables and methods if you don’t specify an access modifier. A class’s data and methods may be default, as well as the class itself. A class’s default features are accessible to any class in the same package as the class in questions. Default is not a Java keyword, it is simply a name that is given to the access level that results from not specifying an access modifier.

5 Protected More accessible than Default A protected feature of a class is available to all classes in the some package, just like default Features are also provided to subclasses that are in different packages

6 Public The most generous access modifier. A public class allows it’s public variables or public methods may be used in any Java program without restriction. An application declares its main() method to be public so that main() method can be invoked from any Java runtime environment.

7 Overriding Private  Default  Protected  Public Private may override private, default, protected, public Default may override default, protected, public Protected may override protected, public Public may override public

8 Other modifiers - Final Applies to classes, methods and variables The meaning of final varies in context A final class can not be subclassed class SubMath extends java.lang.Math{ } // illegal Math is final A final variable can not be modified (const in C++) final float pi = 3.14; // no one may change pi A final method can not be overridden final float circleArea(float r) { return pi * r * r; }

9 Other modifiers - Abstract Applies to classes, methods An abstract class can not be instantiated abstract class Electionics{ } Electonics e= new Electronics(); // illegal A abstract method contains a signature (no body) Abstract void powerUp(); If a class has an abstract method, the method can not be called on any object of that classes type, therefore the compiler insists that any class with an abstract methods must be an abstract class

10 Other modifiers - Static Applies to variables and methods A static variable means there is only 1 instance of the variable regardless of the number of instances of the classes objects. A static method may not access instance variables implicitly can but can access static (class) variables


Download ppt "JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member."

Similar presentations


Ads by Google