java_oops
java_oops
Using Synchronization
Synchronization is the process of allowing only one thread at a time to
complete the particular task. It means when multiple threads executing
simultaneously, and want to access the same resource at the same time,
then the problem of inconsistency will occur. so synchronization is used to
resolve inconsistency problem by allowing only one thread at a time.
Synchronization uses a synchronized keyword. Synchronized is the modifier
that creates a block of code known as a critical section.
class A {
Thread t = Thread.currentThread();
System.out.println(
}
// Class B extending thread class
A a = new A();
a.sum(10);
class Test {
B b = new B();
//'Thread A'
t1.setName("Thread A");
//'Thread B'
t2.setName("Thread B");
t1.start();
t2.start();
••
Process Synchronization is the coordination of execution of multiple processes in
a multi-process system to ensure that they access shared resources in a controlled
and predictable manner. It aims to resolve the problem of race conditions and
other synchronization issues in a concurrent system.
The main objective of process synchronization is to ensure that multiple processes
access shared resources without interfering with each other and to prevent the
possibility of inconsistent data due to concurrent access. To achieve this, various
synchronization techniques such as semaphores, monitors, and critical sections
are used.
Race Condition
When more than one process is executing the same code or accessing the
same memory or any shared variable in that condition there is a possibility
that the output or the value of the shared variable is wrong so for that all the
processes doing the race to say that my output is correct this condition known
as a race condition.
Critical Section Problem
A critical section is a code segment that can be accessed by only one process
at a time. The critical section contains shared variables that need to be
synchronized to maintain the consistency of data variables. So the critical
section problem means designing a way for cooperative processes to access
shared resources without creating data inconsistencies
Any solution to the critical section problem must satisfy three requirements:
• Mutual Exclusion: If a process is executing in its critical section,
then no other process is allowed to execute in the critical section.
• Progress: If no process is executing in the critical section and other
processes are waiting outside the critical section, then only those
processes that are not executing in their remainder section can
participate in deciding which will enter the critical section next, and
the selection can not be postponed indefinitely.
• Bounded Waiting: A bound must exist on the number of times that
other processes are allowed to enter their critical sections after a
process has made a request to enter its critical section and before
that request is granted.
Peterson’s Solution
Peterson’s Solution is a classical software-based solution to the critical
section problem. In Peterson’s solution, we have two shared variables:
• boolean flag[i]: Initialized to FALSE, initially no one is interested in
entering the critical section
• int turn: The process whose turn is to enter the critical section.
Peterson’s Solution preserves all three conditions:
• Mutual Exclusion is assured as only one process can access the
critical section at any time.
• Progress is also assured, as a process outside the critical section
does not block other processes from entering the critical section.
• Bounded Waiting is preserved as every process gets a fair chance.
••
String Interning is a method of storing only one copy of each distinct String
Value, which must be immutable. Applying String.intern() on a couple of strings
will ensure that all strings having the same contents share the same memory.
Ans : When you create a String using a String Literal, the object is created on the heap and
the reference to that object is stored in the String Constant Pool. Hereafter, any references
to that String literal will not create new object but the reference from the String Constant
Pool is returned. When you create a String object using a new keyword, the object is
created on heap but no references are stored in the String Constant Pool.
In your case: 4 objects shall be created on the heap but only the references of the first three
objects will be stored in the String Constant Pool. The object created using
the new keyword will not have its reference stored in the String Constant Pool
• String Literals:
• New Operator:
1. String Literals: When a string literal is created, Java checks the string pool.
2. Reusability: If the literal already exists in the pool, a reference to the existing string is
returned.
3. New Entry: If it doesn't exist, the string is added to the pool, and a reference to it is
returned.
4. Efficiency: This mechanism conserves memory by avoiding the creation of duplicate
string objects.
• == Operator:
java
Copy code
String s1 = new String("Hello");
String s2 = new String("Hello");
boolean result = (s1 == s2); // false
• equals Method:
java
Copy code
String s1 = new String("Hello");
String s2 = new String("Hello");
boolean result = s1.equals(s2); // true
A string is a sequence of characters. In Java, objects of String are immutable
which means a constant and cannot be changed once created. Initializing a
String is one of the important pillars required as a pre-requisite with deeper
understanding.
Comparison between String, StringBuilder, and StringBuffer
Thread
Thread Safe Not Thread Safe Thread Safe
Safety
Memory
High Efficient Less Efficient
Efficiency
Now, we will be justifying. Let us do consider the below code with three
concatenation functions with three different types of parameters, String,
StringBuffer, and StringBuilder. Let us clear out the understanding between
them via a single Java program below from which we will be drawing out
conclusions from the output generated, to figure out differences between
String vs StringBuilder vs StringBuffer in Java.
Example
Java
// Java program to demonstrate difference between
// String, StringBuilder and StringBuffer
// Main class
class GFG {
// Method 1
// Concatenates to String
public static void concat1(String s1)
{
s1 = s1 + "forgeeks";
}
// Method 2
// Concatenates to StringBuilder
public static void concat2(StringBuilder s2)
{
s2.append("forgeeks");
}
// Method 3
// Concatenates to StringBuffer
public static void concat3(StringBuffer s3)
{
s3.append("forgeeks");
}
// Method 4
// Main driver method
public static void main(String[] args)
{
// Custom input string
// String 1
String s1 = "Geeks";
// s1 is not changed
System.out.println("String: " + s1);
// String 1
StringBuilder s2 = new StringBuilder("Geeks");
// s2 is changed
System.out.println("StringBuilder: " + s2);
// String 3
StringBuffer s3 = new StringBuffer("Geeks");
// s3 is changed
System.out.println("StringBuffer: " + s3);
}
}
Output
String: Geeks
StringBuilder: Geeksforgeeks
StringBuffer: Geeksforgeeks
Output explanation:
Object:
The Object class in Java is the root class of the Java class hierarchy. Every class in Java is a
subclass of Object, either directly or indirectly. It provides basic methods that all Java objects
inherit and can use.
Key Points:
• Root Class: All classes in Java inherit from Object.
• Fundamental Methods: Provides essential methods like equals(), hashCode(),
toString(), clone(), finalize(), getClass(), wait(), notify(), and notifyAll().
• Inheritance: Allows Java to treat any class as an Object type, enabling polymorphism
and providing a common interface for all objects.
The Object class in Java provides several methods that are fundamental to all Java objects. Here
are the key methods:
1. equals(Object obj)
o Checks if two objects are equal.
2. hashCode()
o Returns a hash code value for the object.
3. toString()
o Returns a string representation of the object.
4. clone()
o Creates and returns a copy of the object.
5. finalize()
o Called by the garbage collector before the object is destroyed.
6. getClass()
o Returns the runtime class of the object.
7. wait()
o Causes the current thread to wait until another thread invokes notify() or
notifyAll() on the same object.
8. notify()
o Wakes up a single thread that is waiting on the object's monitor.
9. notifyAll()
o Wakes up all threads that are waiting on the object's monitor.
Yes, Java gives importance to primitive data types for several reasons:
1. Performance: Primitive data types are more efficient in terms of memory and processing
speed compared to their wrapper class counterparts.
2. Simplicity: They provide a straightforward way to represent basic values (e.g., int,
char, double), making them easy to use and understand.
3. Low-Level Operations: They are essential for low-level operations, such as arithmetic
calculations and bitwise operations.
However, Java also provides wrapper classes to allow primitives to be used in contexts that
require objects, ensuring flexibility and compatibility with Java's object-oriented features.
Q Is Java pass by value or pass by reference?
1. Primitive Data Types: When a primitive type is passed to a method, the actual value is
passed. Changes to the parameter inside the method do not affect the original variable.
o Example:
java
Copy code
void changeValue(int x) {
x = 10;
}
int a = 5;
changeValue(a);
// a remains 5
java
Copy code
void modifyObject(MyObject obj) {
obj.value = 10; // This changes the object's value
}
In summary, Java always passes arguments by value, meaning it passes a copy of the actual
value (for primitives) or a copy of the reference (for objects).
1. Q Types of oops
1. Encapsulation:
o Definition: Bundling the data (variables) and methods (functions) that operate on
the data into a single unit or class.
o Purpose: Restricts direct access to some of an object's components, which can
protect the integrity of the data.
o Example: Using private fields and public getter and setter methods.
2. Inheritance:
o Definition: Mechanism where one class (child/subclass) inherits the properties
and behavior of another class (parent/superclass).
o Purpose: Promotes code reusability and establishes a natural hierarchy.
o Example: class Dog extends Animal {}
3. Polymorphism:
o Definition: Ability of a single function or method to work in different ways based
on the object it is acting upon.
o Purpose: Enhances flexibility and integration of code.
o Types:
▪ Compile-Time (Method Overloading): Multiple methods with the same
name but different parameters.
▪ Run-Time (Method Overriding): Subclass provides a specific
implementation of a method already defined in its superclass.
o Example: animal.sound() can be different for Dog and Cat.
4. Abstraction:
o Definition: Hiding complex implementation details and showing only the
necessary features of an object.
o Purpose: Reduces complexity and increases efficiency by exposing only relevant
parts.
o Example: Using abstract classes and interfaces.
java
Copy code
abstract class Animal {
abstract void makeSound();
}
These concepts form the foundation of OOP and help in designing and building robust, reusable,
and maintainable software
1. Association:
o Definition: A general relationship between two classes where one class uses or
interacts with another.
o Types: Can be one-to-one, one-to-many, many-to-one, or many-to-many.
o Example: A teacher and a student. The teacher teaches students, and students
attend classes taught by teachers.
o Code Example:
java
Copy code
class Teacher {
private String name;
// constructor, getters, and setters
}
class Student {
private String name;
// constructor, getters, and setters
}
2. Aggregation:
o Definition: A special type of association representing a "whole-part" relationship
where the part can exist independently of the whole.
o Lifespan: The part (child) can exist independently of the whole (parent).
o Example: A department and a teacher. A teacher can belong to multiple
departments and still exist if the department is deleted.
o Code Example:
java
Copy code
class Department {
private String name;
private List<Teacher> teachers;
// constructor, getters, and setters
}
class Teacher {
private String name;
// constructor, getters, and setters
}
3. Composition:
o Definition: A stronger type of association representing a "whole-part"
relationship where the part cannot exist independently of the whole.
o Lifespan: The part (child) depends on the whole (parent) for its existence. If the
whole is destroyed, the part is also destroyed.
o Example: A house and a room. A room cannot exist without the house.
o Code Example:
java
Copy code
class House {
private List<Room> rooms;
// constructor, getters, and setters
}
class Room {
private String name;
// constructor, getters, and setters
}
Function Overloading
1. Definition:
o Multiple methods in the same class with the same name but different parameters
(type, number, or both).
2. Compile-Time:
o Resolved during compile time.
3. Usage:
o Provides multiple ways to perform a similar task with different inputs.
4. Example:
java
Copy code
class Example {
void display(int a) {
System.out.println("Argument: " + a);
}
void display(String b) {
System.out.println("Argument: " + b);
}
}
// Usage
Example obj = new Example();
obj.display(5); // Output: Argument: 5
obj.display("Hello"); // Output: Argument: Hello
Function Overriding
1. Definition:
o A subclass provides a specific implementation of a method that is already defined
in its superclass.
2. Run-Time:
o Resolved during runtime.
3. Usage:
o Achieves runtime polymorphism and allows a subclass to provide a specific
implementation for an inherited method.
4. Example:
java
Copy code
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
// Usage
Animal myDog = new Dog();
myDog.sound(); // Output: Dog barks
Key Differences:
• Method Signature:
o Overloading: Methods have the same name but different parameter lists.
o Overriding: Methods have the same name and parameter list in both superclass
and subclass.
• Scope:
o Overloading: Occurs within the same class.
o Overriding: Occurs in a subclass relative to a superclass.
• Purpose:
o Overloading: Provides different ways to perform similar tasks with different
inputs.
o Overriding: Provides a specific implementation of a method in the subclass that
is already defined in the superclass.
4o
1.
2.
3.
4.
5. Q Difference between Abstract class and Interface?
Abstract Class
1. Definition:
o A class that cannot be instantiated and may contain both abstract methods
(without implementation) and concrete methods (with implementation).
2. Methods:
o Can have both abstract and non-abstract methods.
o Can provide method implementations.
3. Variables:
o Can have instance variables.
o Can have constants (static final variables).
4. Inheritance:
o A class can extend only one abstract class (single inheritance).
5. Constructors:
o Can have constructors.
6. Access Modifiers:
o Can have any access modifiers (public, protected, private) for methods and fields.
7. Usage:
o Used when classes share a common base class but need to provide specific
implementations.
8. Example:
java
Copy code
abstract class Animal {
String name;
void eat() {
System.out.println("This animal eats");
}
}
Interface
1. Definition:
o A reference type in Java, similar to a class, that can contain only abstract methods
(until Java 8), default methods, static methods, and constants.
2. Methods:
o Before Java 8: Can have only abstract methods (implicitly public and abstract).
o From Java 8: Can have default and static methods with implementations.
3. Variables:
o Can have only constants (implicitly public, static, and final).
4. Inheritance:
o A class can implement multiple interfaces (multiple inheritance).
5. Constructors:
o Cannot have constructors.
6. Access Modifiers:
o Methods are implicitly public.
o Fields are implicitly public, static, and final.
7. Usage:
o Used to define a contract that implementing classes must adhere to, promoting
multiple inheritance and loose coupling.
8. Example:
java
Copy code
interface Animal {
void sound();
Key Differences:
• Instantiation:
o Abstract Class: Cannot be instantiated directly.
o Interface: Cannot be instantiated.
• Method Implementation:
o Abstract Class: Can have both abstract and non-abstract methods.
o Interface: Before Java 8, only abstract methods; from Java 8, can have default
and static methods with implementations.
• Multiple Inheritance:
o Abstract Class: A class can extend only one abstract class.
o Interface: A class can implement multiple interfaces.
• Fields:
o Abstract Class: Can have instance variables.
o Interface: Can only have constants.
• Use Case:
o Abstract Class: When classes share a common base with some shared
implementation.
o Interface: When classes need to adhere to a contract or protocol, promoting
multiple inheritance and loose coupling.
4o
No, private methods and static methods cannot be overridden in Java. Here’s why:
Private Methods
1. Private Methods:
o Definition: Methods that are declared private within a class and are not visible
outside of that class.
o Reason for Not Overriding: Private methods are not inherited by subclasses, so
they cannot be overridden.
o Example:
java
Copy code
class Parent {
private void display() {
System.out.println("Parent display");
}
}
In this example, display in Child does not override display in Parent. They
are two separate methods.
Static Methods
2. Static Methods:
o Definition: Methods that belong to the class rather than any specific instance of
the class. They can be called without creating an instance of the class.
o Reason for Not Overriding: Static methods are not associated with any instance
of the class, so they are not subject to polymorphism. Instead, they are hidden if
redefined in a subclass (a process known as method hiding).
o Example:
java
Copy code
class Parent {
static void display() {
System.out.println("Parent display");
}
}
In this example, display in Child hides display in Parent. The method called
is determined by the type of the reference, not the actual object.
Summary
• Private Methods: Cannot be overridden because they are not visible to subclasses.
• Static Methods: Cannot be overridden because they belong to the class itself and not to
instances of the class. They can be hidden by redefining them in a subclass, but this is not
the same as overriding.
Yes, the main() method can be overloaded in Java. However, the JVM (Java Virtual Machine)
looks for the specific signature public static void main(String[] args) to start the
application. Overloaded main() methods with different signatures will not be used as the entry
point of the application but can be called from the main(String[] args) method or elsewhere
in the code.
Example:
java
Copy code
public class MainExample {
Output:
sql
Copy code
Main method with String[] args
Main method with int argument: 5
Main method with String argument: Hello
Key Points:
• Standard Entry Point: The JVM always looks for public static void
main(String[] args) to start execution.
• Overloading: You can define multiple main() methods with different parameter lists.
• Usage: Overloaded main() methods can be called from the standard main(String[]
args) or from other parts of the program.
In summary, while you can overload the main() method, only the public static void
main(String[] args) method will be used as the entry point by the JVM. Other overloaded
main() methods can be used like any other static methods in your program.
Yes, an abstract class can have a main method in Java. Although you cannot instantiate an
abstract class directly, the main method can be used to execute code, including creating instances
of concrete subclasses.
Example:
java
Copy code
abstract class AbstractExample {
public static void main(String[] args) {
System.out.println("Main method in abstract class");
ConcreteExample concrete = new ConcreteExample();
concrete.display();
}
Output:
csharp
Copy code
Main method in abstract class
Display method in concrete class
Key Points:
• Main Method: The main method in an abstract class can be used as the entry point for
running the program.
• Concrete Subclass: You can create instances of concrete subclasses within the main
method or elsewhere in the abstract class.
• Purpose: The main method in an abstract class can be useful for providing a test harness,
utility methods, or demonstrating the use of the abstract class and its subclasses.
In summary, while you cannot instantiate an abstract class, having a main method in it is
perfectly valid and can be used to run the program, test functionality, and create instances of
concrete subclasses.
Serialisation
Serialization and deserialization are processes used to convert objects to and from a format that
can be easily stored or transmitted.
Serialization
Definition:
• The process of converting an object into a byte stream, which can then be easily saved to
a file, sent over a network, or stored in a database.
Purpose:
Example:
java
Copy code
import java.io.*;
// Serialization
try (FileOutputStream fileOut = new FileOutputStream("person.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
out.writeObject(person);
System.out.println("Serialized data is saved in person.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
}
Deserialization
Definition:
• The process of converting a byte stream back into a copy of the original object.
Purpose:
Example:
java
Copy code
import java.io.*;
// Deserialization
try (FileInputStream fileIn = new FileInputStream("person.ser");
ObjectInputStream in = new ObjectInputStream(fileIn)) {
person = (Person) in.readObject();
System.out.println("Deserialized Person:");
System.out.println("Name: " + person.name);
System.out.println("Age: " + person.age);
} catch (IOException | ClassNotFoundException i) {
i.printStackTrace();
}
}
}
Key Points:
In summary, serialization and deserialization are crucial for persisting and transferring object
states in Java, enabling objects to be saved and reconstructed across different contexts.
• The transient keyword in Java is used to indicate that a field should not be serialized.
Purpose:
Yes, it is possible to serialize a class even if its superclass is not serializable. However, the non-serializable
superclass must have a no-argument constructor that is accessible (typically public or protected). The
fields of the non-serializable superclass will not be serialized, but the fields of the serializable subclass
will be.
Uninitialized non-serializable, non-transient fields will still be tolerated during serialization and
deserialization. However, they will not be part of the serialized state, and their default values will
be assigned during deserialization.
• For primitive types, this means the default values (e.g., 0 for int, false for boolean).
• For object references, this means null.
Cloning
Marker Interface
Definition:
• A marker interface is an interface that does not contain any methods or constants. It is
used to "mark" or "tag" a class to indicate that it possesses some property or should be
treated in a certain way by the Java runtime or other components of the application.
Purpose:
• The primary purpose of a marker interface is to provide metadata about a class. The
presence of a marker interface can be checked at runtime to enable certain behavior.
Examples of Marker Interfaces in Java
1. java.io.Serializable:
o Indicates that a class can be serialized (i.e., converted into a byte stream).
2. java.lang.Cloneable:
o Indicates that a class allows for field-for-field copying (i.e., the clone method can
be called on instances of the class).
3. java.util.concurrent.locks.Lock:
o Indicates that a class supports a lock mechanism for thread synchronization.
• Shallow Copy: Copies the object and references to the same objects.
• Deep Copy: Copies the object and recursively copies all objects referenced by it.
The main differences between Errors and Exceptions in Java are primarily related to their
intended use, handling, and impact on the program's execution:
Errors
1. Definition:
o Errors in Java represent serious, unrecoverable problems that typically arise from
the environment or the JVM itself rather than from the application code.
2. Types:
o Examples include OutOfMemoryError, StackOverflowError,
VirtualMachineError, etc.
3. Handling:
o Errors should not be caught or handled by typical application-level error handling
mechanisms because they indicate serious problems that usually cannot be
resolved programmatically.
4. Impact:
o Errors typically lead to termination of the application or the current thread. They
are meant to signal severe problems that usually require intervention at the system
or environment level.
Exceptions
1. Definition:
o Exceptions in Java represent conditions that occur due to problems in the
application code or unexpected conditions during program execution.
2. Types:
o Examples include NullPointerException, FileNotFoundException,
NumberFormatException, etc. Exceptions are further categorized into checked
exceptions (those that must be declared or handled) and unchecked exceptions
(runtime exceptions).
3. Handling:
o Exceptions are intended to be caught and handled within the application's error
handling mechanism using try-catch blocks or by declaring them in method
signatures (for checked exceptions).
4. Impact:
o Exceptions, depending on their type and severity, can often be recovered from or
handled gracefully within the application logic. They do not necessarily lead to
the termination of the entire application but might affect specific operations or
flows within the program.
• Origin:
o Errors typically originate from the JVM or the environment and indicate serious
problems beyond the control of the application.
o Exceptions are generally caused by problems within the application code or
unexpected conditions during execution.
• Handling:
o Errors are not meant to be caught or handled programmatically in most cases and
often lead to termination of the application or thread.
o Exceptions are meant to be caught, handled, or declared, depending on whether
they are checked or unchecked, to allow the application to recover from
unexpected conditions.
• Impact:
o Errors signal severe, usually irrecoverable issues that typically require
intervention outside the application.
o Exceptions represent conditions that can often be anticipated and handled within
the application's normal flow of control.
• Checked Exceptions:
o Checked at compile-time.
o Must be handled using try-catch or declared in method signature.
o Used for recoverable conditions that can reasonably be anticipated.
• Unchecked Exceptions:
o Not checked at compile-time.
o Optional handling; not required to be caught or declared.
o Typically used for programming errors or conditions that indicate serious
problems with the application's logic.
Understanding these distinctions helps in designing effective exception handling strategies and
ensuring that exceptions are appropriately managed in Java applications, balancing between
recoverable errors and serious application flaws
Creating a custom exception in Java involves creating a new class that extends either Exception
(for checked exceptions) or RuntimeException (for unchecked exceptions). Here’s how you can
create a custom exception:
Key Points:
Custom exceptions allow you to define specific error conditions relevant to your application and
provide meaningful error messages to aid in debugging and handling exceptional cases.
A runtime exception in Java is a type of exception that occurs during the execution of a program,
typically due to logical errors, improper use of API, or other runtime conditions that violate the
application's normal flow. Unlike checked exceptions, which must be either caught or declared in
the method signature, runtime exceptions are unchecked and do not need to be explicitly handled
in the code.
1. Unchecked Exception:
o Runtime exceptions extend RuntimeException or its subclasses.
o They are not checked at compile-time, meaning the compiler does not enforce
handling or declaration using throws.
2. Typical Causes:
o Common causes include attempting to access an array element beyond its bounds
(ArrayIndexOutOfBoundsException), accessing a null reference
(NullPointerException), dividing by zero (ArithmeticException), etc.
3. Impact on Program:
o When a runtime exception occurs, it typically indicates a serious problem with the
application's logic or environment.
o They can cause the program to terminate if not handled, although they are not
intended to be caught in every possible scenario.
4. Handling:
o While runtime exceptions do not require explicit handling, it is good practice to
anticipate and handle them where possible to provide robust error handling and
prevent unexpected program termination.
o They often indicate bugs or flaws in the program that should be addressed during
development and testing phases.
try {
String str = null;
System.out.println(str.length()); // NullPointerException
} catch (NullPointerException e) {
System.out.println("Trying to invoke a method on a null object
reference.");
e.printStackTrace();
}
try {
int result = 10 / 0; // ArithmeticException: division by zero
} catch (ArithmeticException e) {
System.out.println("Attempted to divide by zero.");
e.printStackTrace();
}
}
}
Key Points:
Understanding runtime exceptions helps in effective debugging, error handling, and overall
robustness of Java applications, ensuring they can gracefully handle unexpected conditions
during execution.
The Java Virtual Machine (JVM) handles exceptions in Java through a series of steps that
involve identifying, propagating, and potentially handling exceptions within the runtime
environment. Here’s an overview of how JVM handles exceptions:
Handling Process:
1. Exception Occurrence:
o When an exceptional condition occurs during the execution of a Java program
(such as dividing by zero, accessing an array out of bounds, or attempting to
invoke a method on a null object reference), the JVM creates an object
representing that exception.
2. Exception Object Creation:
o The JVM creates an instance of a subclass of Throwable (either Exception or
Error).
o This instance contains information about the type of exception, the point where
the exception occurred (stack trace), and any other relevant details.
3. Exception Propagation:
o The JVM then searches for a handler for this exception. It checks the method
where the exception occurred for a try-catch block that can handle this
particular type of exception.
4. Finding Exception Handler:
o If a suitable catch block is found within the method, the JVM transfers control to
that block to handle the exception.
o If no appropriate catch block is found within the method, the JVM propagates the
exception up the call stack to the calling method.
5. Propagation Up the Call Stack:
o The JVM continues to propagate the exception up the call stack, searching for an
appropriate handler in each method.
o If the exception reaches the top of the call stack (i.e., the main method in a
standalone application or the entry point in a web application), and if no handler
is found, the JVM prints the stack trace to the console and terminates the program
(unless the exception is caught by a global handler).
6. Uncaught Exception Handling:
o For exceptions that are not caught and handled explicitly, the JVM typically prints
the stack trace to the console, providing detailed information about where the
exception occurred and the sequence of method calls leading to it.
Key Points:
By understanding how the JVM handles exceptions, developers can design robust exception
handling strategies in their Java applications, ensuring that critical errors are properly managed
to maintain application stability and reliability.
finalize()
1. Method:
o finalize() is a method defined in the Object class in Java. It's called by the
garbage collector before reclaiming the memory occupied by an object.
2. Usage:
o It's invoked automatically by the garbage collector, not explicitly by the
programmer.
o It can be overridden by a class to perform cleanup operations (like closing files or
releasing resources) before an object is garbage collected.
o It's not recommended to rely on finalize() for critical resource cleanup due to
unpredictable nature of when the garbage collector runs.
java
Copy code
class MyClass {
@Override
protected void finalize() throws Throwable {
// Cleanup code before garbage collection
// Close resources, etc.
super.finalize();
}
}
• final: Keyword used for constants, methods, and classes to indicate immutability, prevent
method overriding, or prevent subclassing.
• finalize(): Method in Object class for performing cleanup operations before object garbage
collection, invoked automatically by the JVM's garbage collector.
• finally: Block used in exception handling to ensure execution of cleanup code, whether an
exception is thrown or not, after a try block.
n Java, the superclass of all exceptions is the Throwable class. The Throwable class is at the top
of the exception class hierarchy, and both Exception and Error classes extend from Throwable.
1. Throwable Class:
o Throwableis the superclass of all exceptions and errors in the Java language.
oIt provides methods to get information about the exception (getMessage(),
printStackTrace(), etc.).
o Direct subclasses include Exception and Error.
2. Exception Class:
o Exception is a subclass of Throwable and represents conditions that a reasonable
application might want to catch.
o Examples include IOException, SQLException, NullPointerException, etc.
o Exceptions are further divided into checked exceptions (subclass of Exception)
and runtime exceptions (RuntimeException and its subclasses).
3. Error Class:
o Error is also a subclass of Throwable and represents serious problems that a
reasonable application should not try to catch.
o Examples include OutOfMemoryError, StackOverflowError,
VirtualMachineError, etc.
o Errors typically arise from problems outside the control of the application, such as
system resource exhaustion or JVM issues.
Example Usage:
java
Copy code
public class ThrowableExample {
public static void main(String[] args) {
try {
// Code that may throw an exception
int result = divideByZero();
} catch (ArithmeticException e) {
// Handle arithmetic exception
System.out.println("Arithmetic Exception occurred: " +
e.getMessage());
} catch (Throwable t) {
// Catching Throwable to handle all exceptions and errors
System.out.println("Throwable occurred: " + t.getMessage());
}
}
• Is throwable an interface?
No, Throwable is not an interface in Java; it is a class. Specifically, Throwable is the superclass
of all exceptions and errors in Java. It is located at the root of the exception hierarchy in Java.
Example Usage:
java
Copy code
public class ThrowableExample {
public static void main(String[] args) {
try {
// Code that may throw an exception
int result = divideByZero();
} catch (ArithmeticException e) {
// Handle arithmetic exception
System.out.println("Arithmetic Exception occurred: " +
e.getMessage());
} catch (Throwable t) {
// Catching Throwable to handle all exceptions and errors
System.out.println("Throwable occurred: " + t.getMessage());
}
}
In Java, the finally block associated with a try block is designed to execute under normal
circumstances, even when an exception is thrown. However, there are a few scenarios where the
finally block may not get executed:
1. System.exit():
o If the Java program calls System.exit() within the try block or catches a
SecurityException caused by System.exit(), the JVM terminates abruptly. In
this case, the finally block does not get a chance to execute.
java
Copy code
try {
// Code that may throw an exception
// Possibly calls System.exit(0);
} finally {
// This block will not execute if System.exit() is called
}
java
Copy code
try {
while (true) {
// Infinite loop or blocking condition
}
} finally {
// This block will not execute until the loop is terminated
}
3. Thread Termination:
o If a thread executing code within a try block is terminated abruptly (for example,
by calling Thread.stop()), the finally block associated with that try block
may not execute.
java
Copy code
try {
// Code that may throw an exception
Thread.currentThread().stop();
} finally {
// This block may not execute if the thread is terminated abruptly
}
In general, finally blocks are intended for cleanup and releasing resources, and they are crucial
in ensuring that important operations (like closing files or releasing locks) are performed
regardless of whether an exception occurs. However, developers should be aware of these
exceptional cases where the finally block may not execute as expected due to external or
abnormal conditions.
3.5
if i return 1 and -1 in try catch block will the finally execute
ChatGPT
Yes, the finally block will execute even if you return from within the try or catch blocks.
Here’s how it works:
java
Copy code
public class FinallyExample {
public static void main(String[] args) {
int result = divideNumbers(10, 0);
System.out.println("Result: " + result);
}
Key Points:
• Execution Guarantee: The finally block is designed to execute under normal and
exceptional circumstances, ensuring cleanup or finalization tasks are performed.
• Return Statement: Returning from within a try or catch block will cause the finally
block to execute before returning from the method.
• Exception Handling: Whether an exception is thrown or not, the finally block ensures
that its code is executed before control leaves the method.
No, in Java, a subclass cannot throw a checked exception that is broader (higher in the exception
hierarchy) than the checked exception thrown by its superclass. This rule ensures that subclasses do not
weaken the exception handling contract defined by their superclass. Here’s why:
Conclusion:
To maintain consistency and uphold the exception handling contract defined by the superclass,
Java enforces that a subclass cannot throw checked exceptions that are broader (higher in the
exception hierarchy) than those thrown by its superclass. This rule ensures type safety and helps
in maintaining robust exception handling in Java programs.
Can we throw an unchecked exception in child class if parent class doesn’t throw any exception?
Example Scenario:
Consider a superclass Parent without any exception declarations, and its subclass Child that
throws an unchecked exception:
java
Copy code
class Parent {
void method() {
// Method code that does not throw any checked exceptions
}
}
In this example:
• Parent class defines method() without any throws declaration, indicating it does not
throw any checked exceptions.
• Child class overrides method() and throws a RuntimeException, which is an
unchecked exception.
• Compile-time: The compiler allows Child to override method() and throw unchecked
exceptions without any issues related to method signature mismatch.
• Runtime: When method() is called on an instance of Child, and an unchecked
exception (RuntimeException in this case) is thrown, it does not need to be caught or
declared, as it is unchecked.
• Function:
• Placement:
Usage of Enum
Enums (short for Enumerations) in Java are used to define a collection of named constants,
which provide better type safety and readability compared to using primitive constants or strings
directly in code. Here are some key reasons why enums are used:
Garbage collection (GC) in Java is an automatic memory management process that handles the
reclamation of memory occupied by objects that are no longer in use. Here’s how garbage
collection works in Java:
Example Scenario:
java
Copy code
public class GarbageCollectionExample {
public static void main(String[] args) {
// Creating objects
MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();
// Nullifying references
obj1 = null;
obj2 = null;
• obj1 and obj2 become eligible for garbage collection once their references are set to
null.
• System.gc() is called to suggest to the JVM that it should perform garbage collection,
but it doesn't guarantee immediate execution.
Conclusion:
// Diamond Problem
import java.io.*;
// Interfaces Declared
interface Parent1 {
void fun();
}
// Interfaces Declared
interface Parent2 {
void fun();
System.out.println("fun function");
// Driver Class
class test1 {
// main function
t.fun();