Q1
•Public - When a class, method, or variable is declared as Public, it can be accessed from anywhere, by any other class. This means that there are no restrictions on its accessibility.
•Private - private class member is declared as Private, we can only be accessed within the same class. It is not accessible outside the class, including subclasses and other classes within the same package.
•Protected - In class, class member is declared as Protected, we can accessed by classes within the same package, as well as by subclasses (even if they are in different packages). This allows for inheritance and access within a certain scope.
•Default – Default means we no access modifier is used, then the Default access modifier is applied. It means that the class, method, or variable is accessible only within the same package. It is not accessible outside the package, even to subclasses.
Significant of Access Modifiers:
• Public Access modifiers are provides the widest accessibility and it provides access from anywhere,
• Private access modifiers are most restrictive accessibility. And limits the access within the same class.
• Protect access modifiers are use within the same package and to subclass, even in different packages.
• Default access modifiers are use within the same package only.
Q2
Exceptions:
•Exceptions are conditions that occur at runtime and disrupt the normal flow of the program. They are typically caused by user input errors, invalid operations, or other exceptional conditions that can be reasonably anticipated and handled by the program.
•Exceptions are divided into two categories: Checked exceptions and Unchecked exceptions.
•Checked exceptions: These are exceptions that must be either caught (handled) or declared in the method signature using the throws clause. Examples -IOException, SQLException.
•Unchecked exceptions: These are exceptions that do not need to be explicitly caught or declared. They usually represent programming errors or conditions that are beyond the control of the programmer. Examples - NullPointerException, ArrayIndexOutofBoundsException.
Errors:
•Errors, on the other hand, are exceptional conditions that occur at runtime and are typically beyond the control of the programmer. They represent serious problems that usually cannot be handled by the program, and attempting to recover from them might lead to unpredictable behaviour.
•Errors are not meant to be caught or handled by regular application code. Instead, they are usually used to indicate severe runtime problems that require intervention at a higher level, such as by system administrators or developers.
•Examples of errors include out of MemoryError, StackOverfloweError, and AssertionError.
1.Checked Exceptions:
•Checked exceptions are the exceptions that are checked at compile-time.
•These exceptions extend the Exception class but not the Runtime Exception class.
•When a method throws a checked exception, the caller must handle it either by catching it using a try-catch block or declaring it in the method signature using the throws keyword.
•Examples of checked exceptions include IOException, ClassNotFoundException, and SQLException.
2.Unchecked Exceptions:
•Unchecked exceptions, also known as runtime exceptions, are not checked at compile-time.
•These exceptions extend the Runtime Exception class or its subclasses.
•Unchecked exceptions do not need to be declared in the method signature or caught explicitly, although they can be if necessary.
•Unchecked exceptions usually occur due to programming errors such as dividing by zero (Arithmetic Exception), accessing an invalid array index
(ArrayIndexOutofBoundsException), or attempting to call a method on a null object (NullPointerException).
for-Q4, Q5, Q6, Q7, Q8.
https://github.com/Subashk129/Task11.
Top comments (0)