Java access modifiers

3,667 views

Published on

Published in: Technology, Education

Java access modifiers

  1. 1. Java Access Modifiers SCJP/OCJP exam objectives – 1.1,1.3,1.4 By, Srinivas Reddy.Swww.JAVA9S.com
  2. 2. Access Modifiers - Importance GoogleSearch int pageRank; int index; getSearchResults(String query){} calculatePageRankAndIndexPages (){}
  3. 3. Access Modifiers• public• protected• default• private
  4. 4. Access Modifiers - Importance• Access modifiers helps to implement the concept of encapsulation in OOPs.• Encapsulation helps to hide the data and behavior.• Access Modifiers affect the accessibility at two levels : • Class • Members(methods and instance variables)
  5. 5. Access Modifiers – at class level
  6. 6. public• When a class is marked as public, it is visible to all other classes which are inside and outside the package. – Visible to everyone.. package com.java9s.Engines; package com.java9s.cars; import com.java9s.Engines.Engine; public class Engine{ public int engine1000cc(){ public class Ford{ int rpmCount=…..; void moveFast(){ return rpmCount; Engine e = new Engine(); } int rpm = e.engine3000cc() public int engine3000cc(){ //move with speed related to int rpmCount=…..; //rpm return rpmCount; } } } }
  7. 7. public - class level• A class marked as public is be accessible to all classes in other packages, but should mention the import statement.• There is no need for an import statement when a class inside the same package uses the class marked as public
  8. 8. default – class level• Default access has no key word.• When there is no access modifier declared, the access is default level access.• The classes marked with default access are visible only to the classes inside the same package.
  9. 9. default – class levelpackage com.java9s.bank;class InterestRates{ When there is no double creditCardInterest(){ access modifier specified …….; - The access is default level } double homeLoanInterest(){ ……; } package com.java9s.bank;} public class customerBanking{ double calculateHomeLoan(){ InterestRates i = new InterestRates(); double interest = i.creditCardInterest(); //calculate the home loan of customer return x; } }
  10. 10. default – class levelpackage com.java9s.bank;class InterestRates{ double creditCardInterest(){ …….; } double homeLoadInterest(){ ……; } package com.java9s.Customerbank;} public class customerBanking{ double calculateHomeLoan(){ InterestRates i = new InterestRates(); The accessing class is not double interest = i.creditCardInterest(); in the same package. So, the class //calculate the home loan of customer With default will not be visible return x; } }
  11. 11. protected and private – class level***protected and private access modifiers are not applicable to the class level declaration***
  12. 12. Access modifiers – member level(methods and instance variables)
  13. 13. public – member level• A member with public access is visible to all the classes inside and outside package.• The class in which the member exists should be is visible to the accessing class.
  14. 14. public – member levelpackage com.java9s.Engines; package com.java9s.cars; import com.java9s.Engines.Engine;public class Engine{ public int engine1000cc(){ public class Ford{ int rpmCount=…..; void moveFast(){ return rpmCount; Engine e = new Engine(); } int rpm = e.engine3000cc() public int engine3000cc(){ //move with speed related to int rpmCount=…..; //rpm return rpmCount; } } }} •Check if the class itself is accessible •Check if the import statement is used to import the class(if accessed from other package) •Finally- check if the member is accessible
  15. 15. public – member levelpackage com.java9s.Engines; package com.java9s.cars; import com.java9s.Engines.Engine;class Engine{ public int engine1000cc(){ public class Ford{ int rpmCount=…..; void moveFast(){ return rpmCount; Engine e = new Engine(); } int rpm = e.engine3000cc() public int engine3000cc(){ //move with speed related to int rpmCount=…..; //rpm return rpmCount; } } }} The class engine is having default access and will not be visible to Ford
  16. 16. default – member level• A member marked with default access will be visible to the classes that are in the same package.
  17. 17. default – member levelpackage com.java9s.bank;class InterestRates{ double creditCardInterest(){ …….; } double homeLoadInterest(){ ……; } package com.java9s.bank;} public class customerBanking{ double calculateHomeLoan(){ InterestRates i = new InterestRates(); double interest = i.creditCardInterest(); //calculate the home loan of customer return x; } }
  18. 18. protected – member level• A member marked as protected is visible to all classes in the same package(Just like default).• protected members are also accessible to classes outside the package, but the accessing class should be a subclass of the member class.
  19. 19. protected- member level package com.java9s.cars; package com.java9s.cars; public class Car{ public class Benz{ protected int speed; int move(){ } Car c = new Car();package com.java9s.fastCars; return c.speed;public class Ferrari extends Car{ } int moveFast(){ return super.speed; } } int move(){ Note: The protected members can be Car c= new Car(); accessed only by the subclasses in other c.speed; packages and can invoke the members only through inheritance mode. } Not by creating an instance.}
  20. 20. private – member level• When a member is marked as private, it is only visible to other members inside the same class.• Other classes inside and outside the package will not be able to access the private members of a class.
  21. 21. private – member level public class Car{ private String keyCode; }public class Theif{ keyCode variable is not void steal(){ visible outside the class Car Car c = new Car(); c.keyCode; }}
  22. 22. protected and private – class level***protected and private access modifiers are not applicable to the class level declaration*** ???
  23. 23. Access Modifiers - summaryVisibility public protected default privateFrom the same class Yes Yes Yes YesFrom a class in same package Yes Yes Yes NoFrom a class from outside the Yes No No NopackageFrom a subclass outside the Yes Yes(Only No Nopackage through inheritance mode)From a subclass in the same Yes Yes Yes Nopackage
  24. 24. Access Modifiers – Local Variablespublic class Calculator{ public int addition(int a, int b){ int c = a+b; return c; C is a local variable and only lives till the method } Executes.}No access modifiers should be applied for Local variables
  25. 25. Thank you Follow me on to get more updates on latest video posts Subscribe on http://www.youtube.com/user/java9s Twitter : @java9s facebook: www.facebook.com/java9swww.JAVA9S.com

×