Wednesday, 10 April 2013

ACCESS SPECIFIERS/MODIFIERS IN JAVA



ACCESS SPECIFIERS/MODIFIERS IN JAVA


Access Specifier/Modifier in any language defines the boundary and scope for accessing the method, variable and class etc.

Java has defined four types of access specifiers. These are :
1.    Public
2.    Private
3.    Protected
4.    Default

If you do not define any access specifier/modifier than java will take “default” access specifier as the default for variables/methods/class.

SYNTAX For declaring a class with access specifier :
<access-specifier><class-keyword><class-name>

For e.g.
public class Demo

    public access specifier :
If you declare any method/function as the public access specifier than that variable/method can be used anywhere.

public Access Specifier Example :
   package abc;  
   
 public class AccessDemo  
 {  
   
      public void test()  
      {  
            System.out.println("Example of public access specifier");  
      }  
 }  

In any other package if we want to access the class "AccessDemo" then we only need to import the abc package as shown in the following code :

    package xyz;  
 import abc.AccessDemo;  
   
 public class AccessExample  
 {  
   
      public static void main(String[] args)  
      {  
           AccessDemo ad = new AccessDemo();  
           ad.test();  
      }  
 }  

     private access specifier :
If you declare any method/variable as private than it will only accessed in the same class which declare that method/variable as private. The private members cannot access in the outside world. If any class declared as private than that class will not be inherited. :

private Access Specifier Example :

    class AccessDemo   
 {  
       private int x = 56;  
   
      public void showDemo()   
      {  
           System.out.println("The Variable value is " + x);  
      }  
    
      private void testDemo()   
      {  
           System.out.println("It cannot be accessed in another class");  
      }  
 }  
    
   
 public class AccessExample   
 {  
    
      public static void main(String[] args)   
      {  
           AccessDemo ad = new AccessDemo();  
           ad.testDemo(); // Private method cannot be used  
           ad.x = 5; // Private variable cannot be used  
    
           ad.showDemo(); // run properly  
      }  
 }  
   

    default access specifier :
If you define any method/variable as default or not give any access specifier than the method or variable will be accessed in only that package in which they are defined.

They cannot be accessed outside the package.

default Access Specifier Example :

In one package we define the variable default as below code :

    package abc;  
   
 class AccessDemo   
 {  
      default int a = 4;  
 }  

Now in any other package if we want to change the value of a, it is not possible because the other package has not access permission for "a" variable.

 package xyz;  
 import abc.AccessDemo;  
   
   
 class AccessExample   
 {  
      public static void main(String[] args)  
      {  
           AccessDemo ad = new AccessDemo();  
           ad.a = 67; //It is not possible.  
      }  
 }  

    protected access specifier :
It has same properties as that of private access specifier but it can access the methods/variables in the child class of parent class in which they are declared as protected.

When we want to use the private members of parent class in the child class then we declare those variables as protected.

protected Access Specifier Example :

 class AccessDemo   
 {  
      protected int x = 34;  
    
       public void showDemo()   
      {  
            System.out.println("The variable value is " + x);  
       }  
 }  
    
 class ChildAccess extends AccessDemo  
 {  
       // child class which inherits  
       // the properties of AccessDemo class  
 }  
    
 public class AccessExample   
 {  
    
       public static void main(String[] args)   
      {  
            ChildAccess ca = new ChildAccess();  
    
            ca.showDemo(); // run properly  
            ca.x = 45; // run properly  
 }   
 }  

62 comments :

  1. Good effort! :)

    ReplyDelete
  2. Nicely presented. Easy to understand. Fine work.Thanks..

    ReplyDelete
  3. Hi I am new to java. Is it "default" keyword exist in java. As of my understanding upto my understanding we didn't have any keyword.

    If i am wrong please let me know. email: nagesh1988@gmail.com

    ReplyDelete
    Replies
    1. Yes, the default keyword is present in java. If you are not provide any access specifier for the variable inside a class then by default it will be "default".
      Actually, the default keyword is not present in c/c++ but it is in java.

      Delete
  4. how to display code like your blog place tall me...

    ReplyDelete
  5. is to important in java all access specifier..

    ReplyDelete
  6. package com.narendra.pk1;

    public class A {
    protected int i=10;

    }


    package com.narendra.pk2;
    import com.narendra.pk1.A;

    public class C extends A{

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    A a1=new A();
    System.out.println(a1.i); //error in this line
    }

    }

    can anyone tell me what is wrong with the above code

    ReplyDelete
    Replies
    1. Hi Narendra, this code only runs when you change the visibility of the variable i to 'public' or declare the class C in same package 'com.narendra.pk1'.
      check out the package visibility here :
      http://javalanguageprogramming.blogspot.in/p/blog-page_6489.html

      Delete
  7. http://java-answers.blogspot.in/2012/01/access-specifiers-in-java.html

    hi tarun refer this site it says protected variable can also be accessed by subclasses in other package

    ReplyDelete
  8. Means now it is sure naa that by default access modifier in java is "Default". and bro i wanna ask 1 thing that if no access level is there then by default "Default" access modifier will be there then what is the need of default keyword ???

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. I reallt happy to read your blog completely. Its really amazing blog. Thanks for sharing. keep sharing more blogs. want to learn Java Online Training.


    Core Java Online Training

    ReplyDelete
  11. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge. Keep update such kind of useful information. thank you...
    Selenium Training in Chennai | Software Testing Training in Chennai

    ReplyDelete
  12. this is really very nice and useful post about Java Access Modifiers .
    thank you so much for this nice post.

    ReplyDelete
  13. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.

    Digital marketing training in chennai

    ReplyDelete
  14. Webtrackker is one only IT company who will provide you best class training with real time working on marketing from last 4 to 8 Years Experience Employee. We make you like a strong technically sound employee with our best class training.
    WEBTRACKKER TECHNOLOGY (P) LTD.
    C - 67, sector- 63, Noida, India.
    F -1 Sector 3 (Near Sector 16 metro station) Noida, India.
    +91 - 8802820025
    0120-433-0760

    Rpa Training institute in noida
    Robotic Process Automation (RPA) has not just overhyped the noise, but it has also proved to intensify unique and high-end skills, bringing remarkable productivity, most importantly, huge customer satisfaction ensuring reduced cost. Robots are making human efforts simpler and precise. Top-notches are focusing more towards investing into People, Robots, and Technology and bag highly competent resources, clients, and opportunities.

    Blockchain training institute in Noida
    Blockchain training institute in Noida - with 100% placement support - 8802820025 - Blockchain training institute in Noida sector 63, 64, 65, 18. A blockchain is a digitized, decentralized, public ledger of all cryptocurrency transactions.

    java training institute in meerut

    java training institute in meerut- Java is an object-oriented computer programming language released by Sun Microsystems in 1995. Mobile phones to scientific supercomputers, most of the devices employ the use of Java applications. Java programming derives most of its syntax from C and C ++. It is one of the fastest, simplest and reliable platforms, and it is one of the most widely used languages of its simplified coding and dynamic function.


    Our services:
    Rpa training institute in noida
    Rpa training center in noida
    Rpa training courses in noida
    Rpa training in noida
    Company Address:
    Webtrackker Technology


    rpa training institute in noida

    Blockchain training institute in Noida

    ReplyDelete
  15. My developer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the expenses.
    fire and safety course in chennai

    ReplyDelete
  16. I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
    python training in velachery | python training institute in chennai



    ReplyDelete
  17. This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolites festivity to pity. I appreciated what you ok extremely here 
    Java training in Chennai | Java training in Bangalore

    Java online training | Java training in Pune

    ReplyDelete
  18. Pleasant Tips..Thanks for Sharing….We keep up hands on approach at work and in the workplace, keeping our business pragmatic, which recommends we can help you with your tree clearing and pruning in an invaluable and fit way.
    Data Science course in rajaji nagar | Data Science with Python course in chenni

    Data Science course in electronic city | Data Science course in USA

    Data science course in pune | Data science course in kalyan nagar

    ReplyDelete
  19. thank you for sharing useful post. Welookups provides tutorials like java tutorial, android, javascript, ajax, sql, python, php, c++ language etc. for beginners and professionals.
    welookups

    ReplyDelete
  20. Digital marketing course in Noida - Digital Marketing is a broad term that includes advertising, promoting, and strengthening the online presence of businesses through digital platforms. Some of the digital tactics that fall under the umbrella of 'Digital Marketing' are digital advertising, email marketing, content marketing, pay-per-click, online brochures, and more. Unlike other offline marketing efforts, digital marketing allows getting accurate results in real time. For example, if you put an advertisement in the newspaper, it is nearly impossible to estimate how many people flipped to that page and paid attention to that ad. Digital marketing training in Noida

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. SFDC Training path is supposed to assure which you study and enforce the thoughts of SFDC platform developer and bypass any required accreditation checks on your first attempt. Quality in elegance guidance will assist you spot how to make bigger salesforce highlights utilizing the state-of-the-art and automated abilities of SFDC code and visual pressure. The down to earth fingers-on mastering approach observed within the route will assure you land role prepared earlier than the cease of it. For best Salesforce Training in Noida aspirants needs to get training from authorised centre for ultimate solutions. Salesforce training in noida sector 63

    ReplyDelete
  23. Android training center in noida sector 15 Android is an open source and Linux-based Operating System Android is an open source and Linux-based Operating System for PDAs, for instance, mobile phones and tablet PCs. Android was made by the Open Handset Alliance, drove by Google, and distinctive associations. Android training in noida Webtrackker Technology offers a united approach to manage application headway for mobile phones which suggest engineers require create for Android, and their applications should have the ability to continue running on different contraptions controlled by Android.

    ReplyDelete
  24. Android training center in noida sector 3 The Android Open Source Project made a Compatibility Test Suite, or cts, that you can use to test an app's compatibility with the Apps working framework. Accessible free on the Web, the Webtrackker Technology best Android training institute and creates a report on each application's appropriateness for arrangement. What's more, Google arrangements to offer a Web-based service that will allow application developers to publish their CTS reports to a public database. And the last guarantees that a particular software application meets the standards required for sending on cell phones that run the Android working system.

    ReplyDelete
  25. Android training center in noida sector 16 Android Application for the Intel® Platform is the perfect introduction for software engineers and mobile application engineers. Through all around planned application tests, code samples and case studies, the book teaches application advancement in view of the Intel platform-including for cell phones, tablets and embedded devices covering performance tuning, debugging, and optimization.
    Today, the clients of Android will utilize the Smartphone for business purposes. Various apps have been developed, which suit almost every aspect of business. There have been persistent application developments that are adding new features to the Smartphone every passing day! The most important and useful business apps are those that help the business visionaries to oversee business cards and to survey the data printed on those.
    Android, MAC & Phone gap is the most popular operating system using in tablet iPhone & smarts phone. Because of the rising the request and high offering of Apps based mobile devices, demanding of smart phone in the market is being going up step by step. The most reason of requesting this is Android is user friendly environment means that simple to use as contrast with other working frameworks in savvy phone.
    The applications programming designers need to much consider a long series of screen sizes, hardware specifications and configurations due to strong competition in the mobile software and changes within each of the platforms. The development of mobile applications is always developing, both as far as wage and jobs created.

    ReplyDelete

  26. Android training center in noida sector 16 Android Application for the Intel® Platform is the perfect introduction for software engineers and mobile application engineers. Through all around planned application tests, code samples and case studies, the book teaches application advancement in view of the Intel platform-including for cell phones, tablets and embedded devices covering performance tuning, debugging, and optimization.
    Today, the clients of Android will utilize the Smartphone for business purposes. Various apps have been developed, which suit almost every aspect of business. There have been persistent application developments that are adding new features to the Smartphone every passing day! The most important and useful business apps are those that help the business visionaries to oversee business cards and to survey the data printed on those.
    Android, MAC & Phone gap is the most popular operating system using in tablet iPhone & smarts phone. Because of the rising the request and high offering of Apps based mobile devices, demanding of smart phone in the market is being going up step by step. The most reason of requesting this is Android is user friendly environment means that simple to use as contrast with other working frameworks in savvy phone.
    The applications programming designers need to much consider a long series of screen sizes, hardware specifications and configurations due to strong competition in the mobile software and changes within each of the platforms. The development of mobile applications is always developing, both as far as wage and jobs created. Android training center in noida sector 62

    ReplyDelete
  27. Android training center in noida sector 63 The history and sorts of it are overpowering to know. The code names of degrees from A to J beginning at now, for example, Aestro, Blender, Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb, Ice Cream Sandwitch, Jelly Bean, KitKat and Lollipop. We should regard the android history in an improvement.
    Initially, Andy Rubin set up Android Incorporation in Palo Alto, California, United States in October, 2003. In seventeenth August 2005, Google secured android Incorporation. Beginning now and into the not too far-removed, it is in the support of Google Incorporation. The key administrators of Android Incorporation are Andy Rubin, Rich Miner, Chris White and Nick Sears. Originally proposed for camera yet moved to cutting edge PDAs later in this way of low market for camera in a way. Android is the moniker of Andy Rubin given by partners by respectability of his affection to robots. In 2007, Google reports the progress of OS. In 2008, HTC moved the key android adaptable.
    It is the heart of android building that exists at the foundation of it's blueprint. Linux part is responsible for contraption drivers, control connection, memory association, gadget alliance and asset get to.2) Native Libraries On the most lifted motivation driving linux part, there are Native libraries, for example, WebKit, OpenGL, FreeType, SQLite, Media, C runtime library (libc) and so on.
    The WebKit library is in charge of program invigorate, SQLite is for database, FreeType for scholarly style bolster, Media for playing and recording sound and video group
    In this, there are center libraries and DVM (Dalvik Virtual Machine) which is tried and unsurprising with run android application. DVM looks like JVM at any rate it is streamlined for cell phones. It utilizes less memory and gives quick execution.

    ReplyDelete
  28. Android primarily based Smartphones have seized the attention of the neighborhood in addition to international marketplace. The addiction is in particular because of the applications this smarty smartphone comes loaded with. The patron demand for specific and thrilling cell packages is at peak; in reality this demand has obtained middle degree. As a end result, Android utility development has carved a wealthy and booming photograph for each humunoid developers and organizations. This platform has helped expand many particular apps for special classes like finance, leisure, media, sports, utilities, climate, news, navigation, banking, business and others. With the emergence of the precise concept of corporation mobility answers, even groups can harness the benefits of first-rate Android app improvement. They have the rights to effortlessly personalize the programs developed the usage of this platform. Initially, a few problems of safety breach were found. However, those threats have no longer been eliminated with using cutting-edge and complicated generation improvements. This guarantees that humanoid apps are much less vulnerable to threats and are secured in nature. Smartphones have turn out to be a not unusual tool to connect human beings from throughout the globe in many methods, thru chat, immediately and text messages, audio visual call, name conferencing and others. The Android platform is showing no signs and symptoms of slowing down. The quantity of apps produced and taken to marketplace each day is rising continuously.
    Android is an open source platform, which cuts the improvement value then and there. It is an cheap manner and most importantly does not restrict the improvement procedure with the license rate. Yes, Android does no longer require any license. If you are making plans for inter application integration, Android is exceptional suitable for it. It permits improvement of custom applications for on line shops.
    As the platform has so much advantage and booming constantly, many expert improvement corporations have commenced providing specialized in addition to customized development services. However, development of the first-rate Android app isn't always a funny story. It requires specialized capabilities and know-how in developing apps across one-of-a-kind verticals. An experienced development team can deal with the venture well. Look for a expert group or folks that assist you to create advanced apps.


    WEBTRACKKER TECHNOLOGY (P) LTD.
    B - 85, sector- 64, Noida, India.
    E-47 Sector 3, Noida, India.
    +91 - 8802820025
    0120-433-0760
    +91 - 8810252423
    012 - 04204716
    EMAIL: info@webtrackker.com

    ReplyDelete
  29. Android primarily based Smartphones have seized the attention of the neighborhood in addition to international marketplace. The addiction is in particular because of the applications this smarty smartphone comes loaded with. The patron demand for specific and thrilling cell packages is at peak; in reality this demand has obtained middle degree. As a end result, Android utility development has carved a wealthy and booming photograph for each humunoid developers and organizations. This platform has helped expand many particular apps for special classes like finance, leisure, media, sports, utilities, climate, news, navigation, banking, business and others. With the emergence of the precise concept of corporation mobility answers, even groups can harness the benefits of first-rate Android app improvement. They have the rights to effortlessly personalize the programs developed the usage of this platform. Initially, a few problems of safety breach were found. However, those threats have no longer been eliminated with using cutting-edge and complicated generation improvements. This guarantees that humanoid apps are much less vulnerable to threats and are secured in nature. Smartphones have turn out to be a not unusual tool to connect human beings from throughout the globe in many methods, thru chat, immediately and text messages, audio visual call, name conferencing and others. The Android platform is showing no signs and symptoms of slowing down. The quantity of apps produced and taken to marketplace each day is rising continuously.
    Android is an open source platform, which cuts the improvement value then and there. It is an cheap manner and most importantly does not restrict the improvement procedure with the license rate. Yes, Android does no longer require any license. If you are making plans for inter application integration, Android is exceptional suitable for it. It permits improvement of custom applications for on line shops.
    As the platform has so much advantage and booming constantly, many expert improvement corporations have commenced providing specialized in addition to customized development services. However, development of the first-rate Android app isn't always a funny story. It requires specialized capabilities and know-how in developing apps across one-of-a-kind verticals. An experienced development team can deal with the venture well. Look for a expert group or folks that assist you to create advanced apps.


    WEBTRACKKER TECHNOLOGY (P) LTD.
    B - 85, sector- 64, Noida, India.
    E-47 Sector 3, Noida, India.
    +91 - 8802820025
    0120-433-0760
    +91 - 8810252423
    012 - 04204716
    EMAIL: info@webtrackker.com

    ReplyDelete
  30. The Linux server is an open supply running system. This server regularly desired over different server operating systems due to their fame for security, fidelity and versatility. You can take examples of red hat server working structures are CentOS, Gentoo, and Ubuntu Server. Red Hat certification route lets you master the standards of Linux Administration. Many websites are there which promises the crimson hat schooling in delhi. Learning linux course will help to you red hat certification training, Red Hat, Linux set up, Linux operations, deploying the Kernel, managing boot system testing and solving. As a part of RHCSA direction, you could work on actual-world projects.
    Professions or new entrants in the area of Information Technologies (IT) or computer systems will discover crimson hat course as the fine profession option for them. SSDN gives Linux Training in Gurgaon for each novices and advanced professionals so that they can get Red hat Certification to increase in their careers. Linux Server is known to be one of the international's leading companies of open supply answers for modern facts centers. Red hat training offers real-international talent development that allows better presentation on the workplace.
    The red hat path certification has assisted professionals supply better deployment and immigrations in their respective businesses. The expert and incredibly professional Red Hat Linux trainer at SSDN train students with palms-on revel in so that they may be well-equipped to control the most challenging user troubles.


    WEBTRACKKER TECHNOLOGY (P) LTD.
    B - 85, sector- 64, Noida, India.
    E-47 Sector 3, Noida, India.
    +91 - 8802820025
    0120-433-0760
    +91 - 8810252423
    012 - 04204716
    EMAIL: info@webtrackker.com

    ReplyDelete
  31. Effective blog with a lot of information. Ijust Shared you the link below for ACTE .They really provide good level of training and Placement,I just Had Data science Classes in ACTE , Just Check This Link You can get it more information about the Data science course.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  32. Thank you for sharing such a nice and interesting blog with us regarding Java. I have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my dude circle.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  33. Thank you for sharing such a nice and interesting blog with us regarding Java. I have seen that all will say the same thing repeatedly.

    AWS training in Chennai

    AWS Online Training in Chennai

    AWS training in Bangalore

    AWS training in Hyderabad

    AWS training in Coimbatore

    AWS training

    ReplyDelete
  34. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.

    python training in chennai

    python course in chennai

    python online training in chennai

    python training in bangalore

    python training in hyderabad

    python online training

    python training

    python flask training

    python flask online training

    python training in coimbatore

    ReplyDelete
  35. No. Java development is not dead. But, learning only JSP and Servlet will not give you any edge. ... There are lot of new things are coming to Java, so its definitely not dead or will not die.
    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    https://www.acte.in/java-training-in-bangalore
    https://www.acte.in/java-training-in-hyderabad
    https://www.acte.in/java-training-in-coimbatore
    https://www.acte.in/java-training

    ReplyDelete
  36. It was really awesome post it was very informative to me and over all the content was very relevant to me and you did a great job really very informative article Azure Training in Chennai

    Azure Training in Bangalore

    Azure Training in Hyderabad

    Azure Training in Pune

    Azure Training | microsoft azure certification | Azure Online Training Course

    Azure Online Training

    ReplyDelete
  37. Thanks for sharing such an informative and useful Post.
    Java course in Pune

    ReplyDelete