Using access modifiers

Information about Using access modifiers

Published on July 7, 2014

Author: nantha98

Source: authorstream.com

Content

PowerPoint Presentation: 1 Access Modifiers by Nantha Kumar Subramaniam PhD CITP Access Modifiers: Access Modifiers What is access modifier? Access modifiers (also known as access levels or access specifiers ) determine whether a class can use or invoke a particular data fields (variables) and methods from other classes Access Modifiers : 3 Access Modifiers TWO reasons of using access modifiers in Java programs: First , when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes that your own class can use; Second , when you write a class, you need to decide what access level every member variable and every method in your class should have. Access Modifiers: Access Modifiers Access modifiers in Java: Access modifiers can be used for classes, data fields (variables) and methods public private protected Default Access Modifiers : Access Modifiers Public class , method , data field ( variable ) – can be declared as public It can be accessed from anywhere (from other classes) Example: public int a; public void setData(){..} 5 data field/variable method Access Modifiers : Access Modifiers Private class , method , data field ( variable ) – declared as private It cannot be accessed by other classes Example: private int a; private int getAge(){..} 6 data field/variable method Access Modifiers : Access Modifiers Protected class , method , data field ( variable ) – declared as protected It can be accessed by the subclasses (derived classes) Example: protected int a; protected void setName(){..} 7 data field/variable method Access Modifiers : Access Modifiers Default class , method , data field ( variable ) – not declared explicitly using public, private or protected These are referred as default Being default , it can be accessed from anywhere within its package Example: int a; void setID(){..} 8 data field/variable method Example: using public and private access modifiers: Example: using public and private access modifiers class Person { public String name; public String address; private double bankBalance; }   9 class Client { public static void main(String args ){ Person p = new Person; p .name =“John”; p .address =“55 New Street”; p .bankBalance =6300.00; }  } PowerPoint Presentation: Thank you 10

Related presentations


Other presentations created by nantha98

Object and classes
09. 07. 2014
0 views

Object and classes

09. 07. 2014
0 views

"while" loops

switch statements
09. 07. 2014
0 views

switch statements

GUI-based Applets
07. 07. 2014
0 views

GUI-based Applets

Java Operators
29. 07. 2014
0 views

Java Operators

Data Types and Variables
29. 07. 2014
0 views

Data Types and Variables