Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Modifiers (public, private, protected And friendly)

In Java, modifiers are keywords that are used to control the visibility, accessibility, and behavior of classes, methods, and variables. Modifiers can be broadly divided into two categories: access modifiers and non-access modifiers. This tutorial will cover the basics of both types of modifiers in Java, along with examples.

  • Access Modifiers:

Access modifiers control the visibility of classes, methods, and variables. There are four access modifiers in Java:

  • public: The class, method, or variable is accessible from any other class in the same package or other packages.
  • protected: The class, method, or variable is accessible within the same package and by subclasses in other packages.
  • private: The class, method, or variable is only accessible within the same class.
  • Default (no modifier): The class, method, or variable is accessible within the same package.

Example:

public class MyClass {
    public int publicVar;
    protected int protectedVar;
    private int privateVar;
    int defaultVar;
}
  • Non-Access Modifiers:

Non-access modifiers provide additional information or restrictions for classes, methods, and variables. Some common non-access modifiers are:

  • static: The class, method, or variable belongs to the class rather than to an instance of the class.
  • final: The class, method, or variable cannot be modified further (i.e., a final class cannot be subclassed, a final method cannot be overridden, and a final variable cannot be reassigned once it has been assigned a value).
  • abstract: The class or method is incomplete and cannot be instantiated or called directly. Subclasses must provide an implementation for abstract methods.
  • synchronized: The method can only be accessed by one thread at a time, which is useful when working with shared resources in a multi-threaded environment.
  • transient: The variable is not serialized when the object is serialized, and its value is not saved or restored during serialization.
  • volatile: The variable's value can be changed by multiple threads, and the value is always read from and written to the main memory instead of the thread's local cache.

Example:

public final class MyClass {
    public static int staticVar;
    public final int finalVar;
    public transient int transientVar;
    public volatile int volatileVar;

    public synchronized void synchronizedMethod() {
        // Method implementation
    }

    public static void staticMethod() {
        // Method implementation
    }
}

public abstract class MyAbstractClass {
    public abstract void abstractMethod();
}

This tutorial introduced the basics of Java modifiers, including access modifiers and non-access modifiers. Modifiers are an essential part of Java programming, as they control the visibility, accessibility, and behavior of your classes, methods, and variables. Understanding how to use modifiers effectively will help you create cleaner, more organized, and more secure code.

  1. Java access modifiers examples:

    • Examples of access modifiers:
    public class MyClass {
        public int publicField;
        private String privateField;
        protected void protectedMethod() {...}
    }
    
  2. Inheritance and access modifiers in Java:

    • Subclasses inherit accessible members from their superclass. The access level of overridden methods can be more permissive but not more restrictive.
    public class SuperClass {
        protected void display() {...}
    }
    
    public class SubClass extends SuperClass {
        @Override
        public void display() {...} // Valid
    }
    
  3. Java access modifiers and method overriding:

    • Overriding methods can have the same or more permissive access modifiers in subclasses.
    public class SuperClass {
        public void display() {...}
    }
    
    public class SubClass extends SuperClass {
        protected void display() {...} // Valid
    }
    
  4. Java package-private (default) modifier:

    • Package-private (default) access is the absence of an explicit access modifier. It means the member is accessible only within the same package.
    class PackagePrivateClass {
        // Package-private (default) class
    }