Skip to content

Commit c0be719

Browse files
committed
Encapsulation added
Signed-off-by: Someshdiwan <[email protected]>
1 parent 38381d5 commit c0be719

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

Section14Interfaces/src/Callback Method in Java.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
Callback Method in Java:
22

3-
A callback method in Java is a method that is passed as an argument to another method, and it gets executed at a later time when a certain event occurs or a condition is met.
3+
A callback method in Java is a method that is passed as an argument to another method, and it gets
4+
executed at a later time when a certain event occurs or a condition is met.
45

56
How Callbacks Work in Java
67

7-
Since Java does not support passing functions directly (like JavaScript or Python), **callback methods are typically implemented using interfaces, anonymous classes, or lambda expressions.
8+
Since Java does not support passing functions directly (like JavaScript or Python), **callback methods are typically
9+
implemented using interfaces, anonymous classes, or lambda expressions.
810

911
---
1012

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
An interface in Java contributes to polymorphism by defining a contract for implementing classes.
2+
3+
It declares a set of abstract methods that must be implemented by any class that implements the interface.
4+
5+
This allows for polymorphic behavior, as different classes can provide their own implementations of the interface methods.
6+
7+
Objects of these implementing classes can be treated as instances of the interface type, enabling runtime polymorphism
8+
9+
and promoting loose coupling in the code.
10+
11+
This code shows an interface Animal with a method makeSound(). Two classes, Dog and Cat, implement this interface and provide their own implementations of makeSound(). The AnimalSounds class demonstrates how objects of Dog and Cat can be treated as Animal objects, exhibiting polymorphic behavior.
12+
13+
// Define an interface
14+
interface Animal {
15+
void makeSound();
16+
}
17+
18+
// Implement the interface with a Dog class
19+
class Dog implements Animal {
20+
@Override
21+
public void makeSound() {
22+
System.out.println("Woof!");
23+
}
24+
}
25+
26+
// Implement the interface with a Cat class
27+
class Cat implements Animal {
28+
@Override
29+
public void makeSound() {
30+
System.out.println("Meow!");
31+
}
32+
}
33+
34+
// Demonstrate polymorphism
35+
public class AnimalSounds {
36+
public static void main(String[] args) {
37+
Animal myDog = new Dog();
38+
Animal myCat = new Cat();
39+
40+
myDog.makeSound(); // Output: Woof!
41+
myCat.makeSound(); // Output: Meow!
42+
}
43+
}
44+
In this example, the Animal interface defines a contract that both Dog and Cat classes adhere to.
45+
46+
This allows us to treat instances of Dog and Cat as Animal objects, and when we call the makeSound() method,
47+
the appropriate implementation is executed based on the actual object type.
48+
49+
This is a clear demonstration of polymorphism in action.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
When using multiple catch blocks, it's important to order them from most specific to most general exceptions.
2+
If a more general exception is caught first, it will catch all of its subclasses, preventing more specific catch blocks
3+
from being reached.
4+
5+
This is why we typically see catch blocks for specific exceptions like NullPointerException before a catch block for the general Exception class.
6+
7+
The 'throws' clause in a method signature is used to declare that the method might throw certain checked exceptions.
8+
It doesn't actually throw or handle the exception, but rather informs the caller that they need to either catch these
9+
exceptions or declare them in their own method signature.
10+
11+
This is part of Java's checked exception mechanism, ensuring that potential exceptions are dealt with at compile-time.
12+
13+
To create a custom checked exception, you should extend the Exception class. Extending RuntimeException would create an
14+
unchecked exception. Extending Error is not appropriate for application-level exceptions, and Throwable is too general.
15+
16+
By extending Exception, you create a checked exception that must be either caught or declared in the method signature,
17+
enforcing proper exception handling at compile-time.
18+
19+
The main advantage of try-with-resources is that it automatically closes resources that implement the AutoCloseable
20+
interface. This feature, introduced in Java 7, simplifies resource management and helps prevent resource leaks.
21+
22+
It ensures that resources are closed in the correct order and handles exceptions that might occur during closing.
23+
24+
This leads to cleaner, more reliable code compared to manually closing resources in a finally block.
25+
26+
When an exception is not caught in a method, it is propagated up the call stack to the calling method.
27+
This process continues until the exception is either caught and handled or reaches the top of the call stack.
28+
29+
If it reaches the top without being caught, the default exception handler prints the stack trace and terminates the program.
30+
31+
This mechanism allows for centralized exception handling and gives flexibility in where exceptions are handled.
32+
33+
The addSuppressed() method is used to add a suppressed exception in Java. This method was introduced along with the
34+
try-with-resources statement in Java 7.
35+
36+
When an exception occurs in the try block and another exception occurs while closing resources in the implicit finally
37+
block, the latter exception is suppressed and added to the original exception.
38+
39+
This allows for preserving information about multiple exceptions that occurred during the execution of a block of code.
40+
41+
You can retrieve suppressed exceptions using the getSuppressed() method.

0 commit comments

Comments
 (0)