Modifiers

10 0 0
                                    

Four types of modifiers

1. public - any class can access your class

2. package private or no modifiers - a class that belongs to the same package can access your class.

3. protected - a class that belongs to the same package and a subclass from a different package can access your class.

4. private - accessible only within the class itself.


class - class has two modifiers, a public and a package private.

==================================

    Example:

    public class Example1{}     // public modifier

    class Example2{}    // package private or no modifier

====================================


members - when talking about members, these are the constructors, fields and methods inside the class. Members has four modifiers.

==============================

    Example:

    class Example3{

        private String name;

        public Example3(String name){

            this.name = name;

        }

        void myMethod1(){

            System.out.println("I am a package private member!");

        }

        protected myMethod2(){

            System.out.println("I am a protected member!");

        }

    }


================================


Java ProgrammingWhere stories live. Discover now