Posts

Java Programming Language

Inheritance in Java: Types, Examples, and Explanation

Inheritance in Java Inheritance is one of the core concepts of Object-Oriented Programming (OOP). It allows one class to inherit the state (variables) and behavior (methods) of another class.  In simple words, inheritance promotes code reuse and establishes a parent–child relationship between classes. ✅ Definition Inheritance is a mechanism where one class acquires the properties and methods of another class The class that provides data is called the Superclass (Parent class) The class that receives data is called the Subclass (Child class) In Java, inheritance is implemented using the extends keyword 🌍 Real-Life Example Family Inheritance A child inherits eye color, height, and surname from parents Parents → Superclass Child → Subclass Similarly, a subclass inherits variables and methods from its superclass. 🔹 Syntax of Inheritance class SubClass extends SuperClass {     // subclass members } 🔑 Important Points A subclass consumes the state and behavior of the supercl...

this() Statement in Java: Constructor Chaining Explained

this() Statement in Java The this() statement in Java is used to call one constructor from another constructor within the same class. It helps in constructor chaining inside the same class and avoids code duplication . ✅ Correct Definition this() is used to call the current class constructor It is used inside a constructor It must be the first statement in the constructor body It is written explicitly by the programmer It does not support recursive calls A constructor body must contain either this() or super(), not both ⚠️ Important Correction this() → calls constructor of the same class super() → calls constructor of the super (parent) class 🌍 Real-Life Example Online Order System One constructor sets default values Another constructor sets custom values Instead of repeating code, one constructor calls another using this() 🔹 Syntax this(parameterList); 🔹 Example of this() Statement class Student {     Student() {         this(101, "Rahul");   ...

this Keyword in Java with Examples

this Keyword in Java The this keyword in Java is a reference variable that refers to the current object of the class that is currently executing. In simple words, this represents the current memory address of the object. ✅ Definition this refers to the current object It is used inside constructors, methods, and instance blocks It is explicitly written by the programmer The compiler does not create this implicitly It helps differentiate between instance variables and local variables when they have the same name 🌍 Real-Life Example Classroom Example A teacher says: “This student should come forward” The word “this” refers to the current student being addressed Similarly, in Java, this refers to the current object. 🔹 Uses of this Keyword in Java 1. Differentiating Instance Variables and Local Variables When local variables and instance variables have the same name, this is used to avoid confusion. class Student {     int id;     Student(int id) {     ...

Constructor Chaining in Java: Implicit and Explicit Calls Explained

Constructor Chaining in Java Constructor chaining is a mechanism in Java where one constructor calls another constructor, usually from the parent (super) class, during object creation.   This process ensures that all parent class constructors are executed before child class constructors, maintaining proper object initialization. ✅ Key Requirements Constructor chaining requires inheritance It is performed using the super() statement It cannot be performed within the same class using super() The super() call must be the first statement in the constructor 🌍 Real-Life Example Building Construction First, foundation is built (Parent class constructor) Then walls and rooms are built (Child class constructor) Similarly, Java first executes the superclass constructor, then the subclass constructor. 🔹 Types of Constructor Chaining Constructor chaining can be performed in two ways: Implicit Constructor Call Explicit Constructor Call 1. Implicit Constructor Call ✅ Definition An implicit con...

Constructor Overloading in Java with Examples

Constructor Overloading in Java Constructor overloading is a feature in Java where multiple constructors are created within the same class, all having the same class name but different parameter lists. It allows objects to be initialized in different ways, depending on the values provided during object creation. ✅ Definition Constructor overloading occurs when: Constructors have the same name as the class But have different parameters (number, type, or order) Java decides which constructor to call based on the arguments passed during object creation. 🌍 Real-Life Example Mobile Phone Purchase Buy phone with only model name Buy phone with model + price Buy phone with model + price + color Each purchase method represents a different constructor . 🔹 Ways to Perform Constructor Overloading Constructor overloading can be achieved in three ways: 1. Changing the Data Type of Parameters class Sample {     Sample(int a) {         System.out.println("Integer Constru...

Constructor in Java: Types, Rules, and Examples

Constructor in Java A constructor in Java is a special type of method that is used to initialize the instance variables of a class. It is automatically called when an object is created. Unlike normal methods, constructors are mainly used to set initial values for an object. Rules of Constructor   A constructor must follow these rules: The constructor name must be the same as the class name A constructor does not have any explicit return type (not even void) It is called automatically during object creation It is used to initialize instance-level data Types of Constructors in Java     Constructors are classified into three types: Default Constructor Zero Parameter Constructor Parameterized Constructor 1. Default Constructor ✅ Definition A default constructor is a constructor that is implicitly created by the Java compiler if no constructor is defined by the programmer. It has the same name as the class It has no parameters Its body contains a super() statement It supports ...

Methods in Java: Static and Instance Methods Explained with Examples

Methods in Java A method in Java is a block of statements that is used to perform a specific task and may return a result. Methods help improve code reusability, readability, and modular programming. Java methods are mainly classified into two types: Static Method Instance Method 1. Static Method ✅ Definition A static method is a method that is declared using the static keyword inside a class. It belongs to the class, not to any specific object. Static methods are generally used to perform common operations that are the same for all users. 🔹 Syntax static returnType methodName() {     // method body } 🌍 Real-Life Example ATM Bank Interest Rate Interest calculation formula is the same for all customers So it can be implemented as a static method 🧠 Example Code class Calculator {     static int add(int a, int b) {         return a + b;     } } public class Main {     public static void main(String[] args) {     ...