Task-11

Access Modifiers in Java:

  • public: Accessible from anywhere. Classes, methods, and variables marked as public can be accessed by any other class.

  • protected: Accessible within the same package or subclasses in different packages. Protected members are not accessible outside the package unless they are inherited by a subclass.

  • default (no modifier): Accessible within the same package only. If no access modifier is specified, the default access modifier is applied.

  • private: Accessible only within the same class. Private members are not accessible outside the class in which they are declared.

Difference Between Exception and Error:

  • Exception: Represents an abnormal condition in the program flow that can be handled using try-catch blocks. Examples include NullPointerException, ArrayIndexOutOfBoundsException, etc.

  • Error: Represents serious problems that are typically not recoverable by the program, such as OutOfMemoryError, StackOverflowError, etc. Errors are usually caused by the environment in which the application is running and are not meant to be caught or handled by the application.

Difference Between Checked and Unchecked Exceptions:

  • Checked Exceptions: These are checked at compile time. The compiler forces the developer to handle or declare checked exceptions using throws keyword in the method signature.
    Examples include IOException, SQLException, etc.

  • Unchecked Exceptions: These are not checked at compile time. Unchecked exceptions extend RuntimeException and its subclasses. They do not need to be declared using throws keyword and can be handled optionally.

    Examples include NullPointerException, ArrayIndexOutOfBoundsException, etc.