Posts

Dynamic Method Dispatch

  Dynamic Method Dispatch in Java Dynamic Method Dispatch is a mechanism in Java that determines which version of an overridden method to call at runtime , not compile time. It is the core of runtime polymorphism in Java. ✅ Definition: Dynamic Method Dispatch refers to the process by which a call to an overridden method is resolved at runtime rather than at compile time. This happens when a superclass reference is used to refer to a subclass object . 📘 Example: class Animal { void sound () { System.out.println( "Animal makes a sound" ); } } class Dog extends Animal { @Override void sound () { System.out.println( "Dog barks" ); } } class Cat extends Animal { @Override void sound () { System.out.println( "Cat meows" ); } } public class Main { public static void main (String[] args) { Animal a; // superclass reference a = new Dog...

Final keyword in Inheritance

In Java, the final keyword plays an important role in controlling inheritance and method overriding . Here's a detailed explanation. 🔐 final Keyword in Inheritance The final keyword can be applied to: Usage Purpose final class                          Prevents a class from being subclassed final method                          Prevents a method from being overridden final variable                          Prevents a variable from being reassigned When applied in the context of inheritance , final helps to restrict changes to a class or method. 📌 1. final Class A final class cannot be extended . You use it when you want to stop inheritance completely . ✅ Example: final class Vehicle { void start () { System.out.println( "Vehicle...

Packages in Java

  📦 What Are Packages in Java? A package in Java is a namespace that organizes a set of related classes and interfaces. Think of it as a folder in your file system that groups related files together. 🎯 Purpose of Packages: Avoid name conflicts between classes. Organize classes logically (like utility classes, database classes, etc.). Control access with access modifiers ( public , protected , default , private ). Makes it easier to maintain and reuse code. 📁 Types of Packages: Type Example Built-in java.lang , java.util , etc. User-defined Your own custom packages ✅ Example: Creating and Using a Package 1. Creating a Package Create a file named MyClass.java in a folder called mypack . 📄 MyClass.java : package mypack; // package declaration public class MyClass { public void showMessage () { System.out.println( "Hello from mypack.MyClass" ); } } 2. Compiling the Package In the terminal: javac -d . MyClass.java T...

CLASSPATH

  📌 The CLASSPATH in Java What is CLASSPATH ? The CLASSPATH is an environment variable that tells the Java compiler ( javac ) and Java runtime ( java ) where to find : User-defined classes and packages External .jar libraries 🔍 Why Is CLASSPATH Important? If your .class files or .jar files are not in the current directory , Java needs the CLASSPATH to locate them. Setting the CLASSPATH Temporarily: # Linux/Mac export CLASSPATH=/path/to/classes # Windows set CLASSPATH=C:\path\to\classes Or use it while running the program: java - cp . Test -cp . tells Java to look for classes in the current directory ( . ). ⚠️ Common CLASSPATH Issues: “ ClassNotFoundException ” when the class isn’t in the path Incorrect directory structure (package name must match folder name)

Java Access Modifiers and Package-Level Access

  🔐 Java Access Modifiers and Package-Level Access Java provides four access levels : Access Modifier Class      Package      Subclass      World (outside package) public           ✅           ✅           ✅           ✅ protected      ✅           ✅           ✅           ❌ (default) no modifier      ✅           ✅           ❌           ❌ private                ✅           ❌           ❌           ❌ 📦 How It Works in Packages 1. public Accessible from any class in any package....

Interfaces and Abstract Classes

  🔷 Interfaces in Java An interface in Java is a reference type similar to a class, but it is used to specify a contract that a class must follow. It contains abstract methods (without implementation) and constants . From Java 8 onwards, interfaces can also have default and static methods. ✅ Key Points Declared using the interface keyword. All methods in an interface are public and abstract by default (until Java 7). All fields are public , static , and final (i.e., constants). A class implements an interface using the implements keyword. A class can implement multiple interfaces (Java supports multiple interface inheritance). Interfaces cannot have constructors . 🧩 Syntax interface MyInterface { void display () ; // implicitly public and abstract } ✅ Implementing an Interface interface Animal { void makeSound () ; } class Dog implements Animal { public void makeSound () { System.out.println( "Bark...

extending Interface - multiple inheritance

 In Java, interfaces can be extended using the extends keyword. This allows you to build a more complex interface by inheriting from one or more existing interfaces. ✅ Syntax for Extending Interfaces interface A { void methodA () ; } interface B extends A { void methodB () ; } Here, interface B extends interface A . So any class that implements B must implement both methodA() and methodB() . ✅ Example interface A { void methodA () ; } interface B extends A { void methodB () ; } class MyClass implements B { public void methodA () { System.out.println( "methodA from interface A" ); } public void methodB () { System.out.println( "methodB from interface B" ); } } public class Main { public static void main (String[] args) { MyClass obj = new MyClass (); obj.methodA(); obj.methodB(); } } ✅ Extending Multiple Interfaces Java supports ...