Posts

Showing posts with the label Java Interview Questions

Factory Design Pattern in Java: Definition, Structure, and Real-Life Examples

📘 Factory Design Pattern in Java The Factory Design Pattern is a creational design pattern that provides an object-creation mechanism without exposing the creation logic to the end user. Instead of creating objects directly using the new keyword, the factory class is responsible for object creation and returning the required object. This pattern helps achieve loose coupling and better maintainability. 🧠 Core Idea of Factory Design Pattern Create objects without exposing the instantiation logic to the client and refer to newly created objects using a common interface. 🧩 Three Main Logics in Factory Design Pattern 🔹 1. Implementation Logic Present in the subclasses Subclasses provide implementation of the interface methods 🔹 2. Object Creation Logic Present in the factory class The factory decides which object to create 🔹 3. Consumer / Utilization Logic The end user (client) accesses objects Objects are obtained only through the factory class 🛠️ Steps to Create Factory Design Pat...

Design Patterns in Java: JavaBean and Singleton Design Pattern Explained with Examples

📘 Design Pattern in Java A Design Pattern is a reusable solution to commonly occurring problems in software design and application development. Design patterns help developers write clean, maintainable, scalable, and efficient code. They do not provide code directly, but they provide best practices and standard approaches to solve design issues. 🔷 JavaBean Design Pattern A JavaBean Design Pattern is used to create simple, reusable, and encapsulated Java classes mainly for data transfer within an application. ✅ Rules of JavaBean Class A JavaBean class must follow these rules: The class must be declared as public It may implement the Serializable marker interface All instance variables must be private It must contain public getter (accessor) and setter (mutator) methods It must have a public or protected constructor It should not contain business logic (recommended) 🎯 Purpose of JavaBean Design Pattern Used for data transportation inside an application The object of a JavaBean class ...

Properties File in Java: Configuration, Reading Data, and Methods Explained

📘 Properties File in Java A Properties file is a configuration file used to store application settings—most commonly database credentials—in the form of key-value pairs. It helps separate configuration data from application logic, making the application easier to maintain and modify. 🔑 Format of a Properties File Properties db.url=jdbc:mysql://localhost:3306/testdb db.username=root db.password=admin 📦 Reading a Properties File in Java To read data from a properties file, Java provides the Properties class, which is available in the java.util package. Required Packages: java.util java.io 🧩 Steps to Read a Properties File Create an object of the Properties class Open the file using FileReader Load the key-value pairs into the Properties object Retrieve values using keys 🔧 Important Methods of Properties Class 🔹 load() Method Used to load key-value pairs from a file into the Properties object. void load(FileReader fr) throws IOException 📌 This method belongs to the Properties cla...

Static Import in Java: Definition, Syntax, Advantages, and Examples

📘 Static Import in Java Static Import is a feature introduced in JDK 1.5 that allows programmers to access static members (variables and methods) of a class, abstract class, or interface without using the class name. It helps make the code shorter, cleaner, and more readable, especially when static members are used frequently. 🔹 Why Use Static Import? Normally, static members are accessed using the class name. With static import, the class name is not required. ✔️ Benefits : Reduces code verbosity Improves readability Makes frequently used static members easy to access 🧩 Syntax of Static Import import static packageName.ClassName.staticMember; OR (to import all static members) import static packageName.ClassName.*; 🧠 What Can Be Imported Using Static Import? Static variables Static methods Static constants 📌 Applicable to: Class Abstract class Interface 🌍 Real-Life Example Think of static import like saving a contact number: Without static import → You dial the full number every...

Database Configuration in Java: Definition, Components, and Best Practices

📘 Database Configuration in Java Database Configuration refers to a set of credentials and settings used by an application to establish communication with a database server. These details allow a Java application to connect securely and perform database operations such as insert, update, delete, and retrieve data. 🔑 Components of Database Configuration A typical database configuration includes the following details: Database URL – Specifies the database location and type Port Number – Identifies the port on which the database server is running Username – Authorized user to access the database Password – Authentication credential for the user 📌 Note: Database credentials are unique for every database server and should be handled securely. 📂 Where Database Configuration Can Be Written Database configuration details can be stored in different places depending on the application design: Interface Properties file (.properties) web.xml file (for web applications) 🧩 Database Configurati...

Class Loading in Java: Explicit vs Implicit Class Loading with Examples

🔄 Class Loading in Java – Complete Beginner Guide What is Class Loading in Java? Class Loading is the process by which the Java Virtual Machine (JVM) loads a .class file from the hard disk into the JVM memory (Method Area / Class Area) during program execution. Without class loading, Java classes cannot be executed. Types of Class Loading in Java Class loading in Java is broadly classified into two types: Explicit Class Loading Implicit Class Loading 1️⃣ Explicit Class Loading What is Explicit Class Loading? Explicit class loading occurs when the JVM loads a class automatically because the programmer directly accesses any class member. When Does Explicit Class Loading Happen? Accessing a static variable Calling a static method Creating an object using constructor Example: Student s = new Student(); // Class loaded explicitly ✔ The JVM loads the Student.class file when the object is created. 2️⃣ Implicit Class Loading What is Implicit Class Loading? Implicit class loading is a process ...

JAR File in Java: Definition, Uses, Abstraction Concept, and How to Add JAR to Build Path

📦 JAR File in Java – Complete Guide for Beginners What is a JAR File in Java? A JAR file (Java ARchive) is a compressed file format used in Java to package multiple .class files, along with configuration files such as .xml and .properties files, into a single file. JAR files make Java applications easy to distribute, reuse, and deploy. Why is a JAR File Used? JAR files are mainly used to: Combine multiple Java class files into one file Reduce file size using compression Share libraries between different Java applications Maintain project structure and dependencies Support inter-application communication JAR File and Abstraction Concept A JAR file is a physical form of abstraction. 🔹 What is Abstraction? Abstraction is an object-oriented programming concept that focuses on: Hiding implementation details Exposing only required functionality 🔹 How JAR Represents Abstraction? Users can use the methods and classes from a JAR file The internal implementation (source code) remains hidden O...

Multiple Catch Blocks in Java: Rules, Order, and Best Practices

📌 Multiple Catch Blocks in Java (Professional & Beginner-Friendly Explanation) When a programmer uses a try block followed by multiple catch blocks, Java follows strict rules to avoid ambiguity while handling exceptions. 🔹 Key Rules of Multiple Catch Blocks 1️⃣ No Duplicate Catch Blocks Allowed Each catch block must handle a different exception type. ❌ Invalid Code (Duplicate Catch): try {     int a = 10 / 0; } catch (ArithmeticException e) {     System.out.println("Arithmetic Exception"); } catch (ArithmeticException e) { // ❌ Duplicate     System.out.println("Duplicate Exception"); } 📌 Reason:  The JVM will be confused about which catch block to execute, resulting in a compile-time error. 2️⃣ Superclass Catch Can Handle Subclass Exceptions A catch block with a superclass exception can handle all its subclass exceptions. 📌 Example: try {     int[] arr = new int[5];     arr[10] = 50; } catch (Exception e) {     Sys...