Posts

Showing posts with the label Core Java

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...

Arrays in Java: Definition, Types, Syntax, and Real-Life Examples

📌 Array in Java (Professional & Beginner-Friendly Explanation) 🔹 What is an Array? An array in Java is an object that is used to store multiple values of the same (homogeneous) data type under a single variable name. Arrays store data using index positions. The size of an array is fixed once it is created. Array indexing starts from 0 and ends at size – 1. When an array is created in Java, an internal class is generated by the JVM. This internal class contains a final variable called length, which stores the size of the array. 🔹 Key Characteristics of Arrays Stores homogeneous data only Index-based data access Fixed size (cannot grow or shrink) Faster data access compared to collections 🔹 Syntax to Create an Array in Java 1️⃣ Using new keyword dataType[] arrayName = new dataType[size]; Example: int[] numbers = new int[5]; 2️⃣ Using Array Literal dataType[] arrayName = {value1, value2, value3}; Example: int[] numbers = {10, 20, 30, 40}; 🔹 Types of Arrays in Java 1️⃣ Primitive D...

Scanner Class in Java: Complete Guide to User Input with Examples

Scanner Class. Scanner Class is present inside the java.util package. It is used for the take the input from user. Scanner Class method. nextByte();read byte data. nextShort();read short data. nextInt();read integer data. nextLong();read long data. nextFloat();read float data. nextDouble();read double data. nextBoolean();read boolean data. next();read string and character data. nextLine();read string data.

Java Collection Framework Explained – Complete Guide with Examples

Java Collection Framework – Complete Beginner Guide 📘 What is the Collection Framework? The Java Collection Framework is a set of classes and interfaces provided by Java to store, manage, and process groups of objects efficiently. 👉 It is part of the java.util package. 📌 Key Points The Collection Framework stores objects only, not primitive data types Primitive data types are stored using Autoboxing              Example: int → Integer, double → Double Collection size is dynamic, controlled by the JVM Provides ready-made methods for searching, sorting, inserting, and deleting data 📘 Real-Life Example 📚 Library System A library stores many books Each book is an object Java collections store these book objects efficiently 📘 Main Interfaces in Collection Framework Collection  ├── List  ├── Set  └── Queue Map (separate hierarchy) Interfaces: Collection List Set Queue Map 📘 Advantages of Collection Framework ✔ Stores homogeneous and het...

Packages in Java: Definition, Types, and Examples

Package in Java A package in Java is a collection of related classes, interfaces, and abstract classes that are grouped together as part of an application. Packages help in organizing code, avoiding name conflicts, and improving maintainability. Types of Packages Packages in Java are classified into two types: 1. Inbuilt Packages 2. User-Defined (Programmer-Defined) Packages 1. Inbuilt Packages Inbuilt packages are predefined packages that are available in the JDK software. These packages usually start with the java keyword. Examples of Inbuilt Packages java.lang java.sql java.io java.util java.net java.time  📌 Note: The java.lang package is imported automatically by the JVM. 2. User-Defined (Programmer-Defined) Packages User-defined packages are created by programmers according to the application requirements. Package Naming Convention com.companyname.applicationname.modulename org.companyname.applicationname.modulename Example package com.example.billing.invoice; Importing a Pac...

Polymorphism in Java: Definition, Types, and Examples

Polymorphism in Java Polymorphism is an object-oriented programming concept in which a single reference variable can exhibit different behaviors depending on the object it refers to. Types of Polymorphism Polymorphism in Java is classified into two types: Compile-Time Polymorphism Run-Time Polymorphism 1. Compile-Time Polymorphism In compile-time polymorphism, the method resolution is performed by the compiler. Key Points The compiler resolves the method call based on the parameter list. Once method resolution is done, the compiler performs static binding. In static binding:               ✓ The method declaration is bound to the method definition at compile time. Compile-time polymorphism is achieved using method overloading. Example class Calculator {     int add(int a, int b) {         return a + b;     }     double add(double a, double b) {         return a + b;     } } ...

Down Casting in Java: Definition, Rules, and Examples

Down Casting in Java Down Casting is the process of converting a superclass reference (that actually refers to a subclass object) into a subclass reference. Key Points Down casting is also known as explicit casting. It must be performed by the programmer using a cast operator. The compiler does not perform down casting automatically. Up casting must occur before down casting. If down casting is done without a valid up cast, it results in a ClassCastException at runtime. Example class Parent {     void show() {         System.out.println("Parent method");     } } class Child extends Parent {     void display() {         System.out.println("Child method");     } } Parent p = new Child(); // Up casting Child c = (Child) p; // Down casting c.show(); // Allowed c.display(); // Allowed Invalid Down Casting Parent p = new Parent(); Child c = (Child) p; // ClassCastException Why Down Casting Is Risky Per...

Up Casting in Java: Definition, Rules, and Examples

Up Casting in Java Up Casting is the process of assigning a subclass object to a superclass reference or a super-interface reference. Key Points Up casting is also known as implicit casting. It is performed automatically by the compiler. In up casting, the compiler treats the subclass object as a superclass object. No explicit cast is required. Parent p = new Child(); // Up casting Accessing Members During Up Casting When up casting occurs, the programmer can access only the members of the superclass using the superclass reference. Subclass-specific members cannot be accessed directly. class Parent {     void show() {         System.out.println("Parent method");     } } class Child extends Parent {     void display() {         System.out.println("Child method");     } } Parent p = new Child(); p.show(); // Allowed // p.display(); // Not allowed Up Casting with Method Overriding If method overriding is...

Interface in Java: Definition, Rules, and Examples

Interface in Java An interface is a type definition block in Java that is completely abstract. It is used to achieve full abstraction and to define a contract that implementing classes must follow. Key Points An interface is declared using the interface keyword. An interface can contain:            ✓public static final variables (constants)           ✓public abstract methods (instance methods) An interface cannot be instantiated (object creation is not allowed). An interface can extend another interface using the extends keyword. A class can implement an interface using the implements keyword. An interface cannot extend a class. An interface supports complete abstraction because it contains only abstract methods. An interface supports multiple inheritance Example interface Animal {     int AGE = 5; // public static final by default     void sound(); // public abstract by default } class Dog implements Animal { ...

Abstract Class in Java: Definition, Rules, and Examples

Abstract Class in Java An abstract class is a class that is declared using the abstract keyword. It is used to achieve partial abstraction in Java. Key Points Any class declared with the abstract keyword is called an abstract class. An abstract class can contain:             ✓Abstract methods             ✓Concrete methods An abstract class cannot be instantiated (object creation is not allowed). If a class contains at least one abstract method, then the class must be declared abstract. An abstract class does not support multiple inheritance. An abstract class supports partial abstraction. Abstract Method An abstract method is a method that:                 ✓Does not have a method body                  ✓Ends with a semicolon (;) Abstract methods only provide method declaration, not implementation.             abstra...

Encapsulation in Java: Definition, Examples, and Advantages

Encapsulation in Java Encapsulation is an object-oriented programming (OOP) concept in which an object supports data binding and data hiding to protect its internal state. Key Concepts Data Binding Data binding is the process of associating the data with an object in memory. It ensures that the object's data is properly managed and accessed through the object. Data Hiding Data hiding is the process of restricting direct access to the object's data from other objects. Typically achieved by making variables private and providing getter and setter methods to access or modify the data. Encapsulation in Practice If a Java object supports both data binding and data hiding, it is considered an encapsulated object. Encapsulation protects the integrity of the data and allows controlled access. Example class Student {     // Private data (data hiding)     private String name;     private int age;     // Getter method     public String getName() { ...