Posts

Showing posts from May, 2025

Method overloading in Java

  ๐Ÿ”ท Method Overloading ✅ What is Method Overloading? Method Overloading is a feature in Java where a class can have multiple methods with the same name but different parameter lists (type, number, or order). It is a form of compile-time polymorphism . ๐Ÿง  Why Use Method Overloading? To perform similar operations in different ways. Improves code readability and reusability . ✅ Rules for Overloading Methods must have different parameter lists . Can have different return types , but return type alone is not enough to overload a method. ๐Ÿงช Example: class Calculator { int add ( int a, int b) { return a + b; } double add ( double a, double b) { return a + b; } int add ( int a, int b, int c) { return a + b + c; } } public class Main { public static void main (String[] args) { Calculator c = new Calculator (); System.out.println(c.add( 2 , 3 )); // 5 ...

Object as parameters and Returning Objects

  ๐Ÿ”ท Using Objects as Parameters ✅ Why Use Objects as Parameters? Passing an object as a parameter allows one class to use the data or behavior of another class. This supports: Code modularity Encapsulation Code reuse ๐Ÿงช Example: class Student { String name; int age; Student(String n, int a) { name = n; age = a; } void display () { System.out.println(name + " is " + age + " years old." ); } } class DisplayDetails { void printStudent (Student s) { s.display(); // calling method of Student class } } public class Main { public static void main (String[] args) { Student s1 = new Student ( "Alice" , 20 ); DisplayDetails d = new DisplayDetails (); d.printStudent(s1); // Passing object as parameter } } ๐Ÿ”„ Output: Alice is 20 years old . ๐Ÿ”ท Returning Objects from Methods ✅ What is it? In Java, a method can retur...

Inner Classes in Java

  ๐Ÿ”ท What is an Inner Class? An Inner Class is a class defined inside another class . Java allows nesting classes to logically group classes that are only used in one place, increasing encapsulation and readability. ✅ Why Use Inner Classes? Logical grouping of classes Better encapsulation Can access members (even private ones) of the outer class Useful in event handling, GUI applications, etc. ๐Ÿ”ฐ Types of Inner Classes in Java: Type Description 1. Member Inner Class Regular class defined within another class. 2. Static Nested Class Static class defined inside another class. Doesn’t need outer class object. 3. Local Inner Class Defined inside a method or block. 4. Anonymous Inner Class A class without a name, used for one-time use (like in GUI event handling). ๐Ÿ”ท 1. Member Inner Class (Non-static) ๐Ÿ“Œ Example: class Outer { int outerVar = 100 ; class Inner { void show () { System.out.println( "Outer variable: " + ou...

Final variables in Java

  ๐Ÿ”ท Final Variables in Java ✅ What is final in Java? The final keyword is used to declare constants . Once assigned, a final variable's value cannot be changed . ๐Ÿ“Œ Types of Final Usage: Final variable Final method Final class ✅ 2.1 Final Variable Value is assigned only once . If not initialized during declaration, it must be initialized in a constructor or static block . ๐Ÿ”น Example: class Circle { final double PI = 3.14159 ; // constant double area ( double r) { return PI * r * r; } } ✅ 2.2 Final Method Cannot be overridden by subclasses. class A { final void show () { System.out.println( "This is a final method." ); } } class B extends A { // void show() {} ❌ Compilation error } ✅ 2.3 Final Class Cannot be extended (no subclassing). final class Animal { void sound () { System.out.println( "Animal sound" ); } } // class Dog extends...

Static Members in Java

  ๐Ÿ”ท Static Members in Java ✅ What is static in Java? The static keyword in Java is used for memory management . It makes a member (variable or method) belong to the class rather than an instance of the class. ๐Ÿ“Œ Types of Static Members: static variables (class variables) static methods static blocks static nested classes ✅ 1.1 Static Variable Shared among all objects of a class. Stored in method area (not heap). Initialized only once at class loading time. ๐Ÿ”น Example: class Student { int rollNo; static String college = "ABC College" ; // static variable Student( int r) { rollNo = r; } void display () { System.out.println(rollNo + " - " + college); } } public class Main { public static void main (String[] args) { Student s1 = new Student ( 101 ); Student s2 = new Student ( 102 ); s1.display(); s2.display(); } } ✅...

Super Class and Subclass

  ๐Ÿ”น What is a Superclass? A superclass (also called base class or parent class ) is a class that is inherited by another class . It defines common attributes and methods that can be shared by other classes. ✅ Key points: A superclass can be reused by multiple subclasses. It may be abstract (not directly instantiated). It provides common functionality to be extended or overridden. ๐Ÿ”น What is a Subclass? A subclass (also called derived class or child class ) is a class that inherits from another class using the extends keyword. It can: Use the methods and variables of the superclass. Add new methods or fields. Override superclass methods. ๐Ÿ“Œ Syntax of Inheritance in Java: class Superclass { // fields and methods } class Subclass extends Superclass { // additional fields and methods } ๐Ÿ“˜ Example: // Superclass class Animal { void eat () { System.out.println( "This animal eats food." ); } } // Subcla...

Types of Inheritance in Java

  ๐Ÿ”ท What is Inheritance in Java? Inheritance is the process by which one class (child) acquires the properties and behaviors (fields and methods) of another class (parent). It promotes code reusability and is a key feature of object-oriented programming . ๐Ÿ”‘ Syntax: class Parent { // parent members } class Child extends Parent { // child members } ✅ Types of Inheritance in Java: Type Description 1. Single Inheritance One class inherits from another. 2. Multilevel Inheritance A class inherits from a class which is already inherited from another class. 3. Hierarchical Inheritance Multiple classes inherit from a single parent class. 4. Hybrid Inheritance Combination of multiple types (achievable via interfaces in Java). ❌ Note : Java does not support multiple inheritance with classes to avoid ambiguity (Diamond Problem). But it is allowed through interfaces . ๐Ÿ”น 1. Single Inheritance A child class inherits directly from a parent class. ๐Ÿ“Œ Example: cl...

Super Keyword in Java

  ๐Ÿ”น super Keyword in Java The super keyword is used in a subclass to refer to members (variables or methods) of its immediate superclass . ✅ Uses of super : Access superclass constructor Access superclass methods Access superclass variables 1. Using super() to Call Superclass Constructor By default, when a subclass constructor is called, it implicitly calls the no-argument constructor of the superclass using super() . class Animal { Animal() { System.out.println( "Animal constructor called" ); } } class Dog extends Animal { Dog() { super (); // optional here, Java adds it automatically System.out.println( "Dog constructor called" ); } } public class Main { public static void main (String[] args) { Dog d = new Dog (); } } Output: Animal constructor called Dog constructor called 2. Using super to Access Superclass Method class Animal { void sound () { ...

Method overriding in Java

  ✅ What is Method Overriding in Java? Method overriding in Java occurs when a subclass provides a specific implementation of a method that is already defined in its superclass . The overridden method in the subclass should have: Same method name Same return type (or subtype - called covariant return type) Same parameter list ๐Ÿง  Purpose of Overriding To implement runtime polymorphism , where the method that gets called depends on the object's runtime type , not the reference type. ๐Ÿ“˜ Example of Method Overriding class Animal { void sound () { System.out.println( "Animal makes a sound" ); } } class Dog extends Animal { // Overriding the sound() method @Override void sound () { System.out.println( "Dog barks" ); } } public class Main { public static void main (String[] args) { Animal a = new Dog (); // Upcasting a.sound(); // Calls Dog's overridden meth...