Posts

Showing posts from August, 2025

OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 17 : Attributes: Final and Static in Java

  Unit - 2 Topic - 17 : Attributes: Final and Static in Java 1. Final in Java The final modifier can be used with variables , methods , and classes . Final Variables If a variable is declared as final , it becomes a constant . Once assigned, its value cannot be changed throughout the program. The value is usually assigned during declaration. Example – Final Variable class PhyTest { final int Minmarks = 35 ; // final variable declaration void run () { // Minmarks = 40; // ❌ This will cause a compile-time error System.out.println( "The subject is cleared with minimum marks: " + Minmarks); } public static void main (String args[]) { PhyTest obj = new PhyTest (); obj.run(); } } Output The subject is cleared with minimum marks: 35 Note: Since Minmarks is declared as final , attempting to modify it will result in a compile-time error . 2. Static Methods in Java When a method is dec...

OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 16 : Method Overriding in Java

  Unit - 2 Topic - 16: Method Overriding in Java Definition In a class hierarchy , when a method in a subclass has the same name and same type signature as a method in its superclass , the method in the subclass overrides the one in the superclass. Key Points When an overridden method is called from a subclass object , it will always execute the subclass version . The superclass version is hidden. First, Java checks if the method exists in the superclass. If yes , it executes the subclass version . If no , it throws a "method not found" error. Methods with different signatures are overloaded , not overridden. Example Program // Method Overriding Example class A { int i, j; A( int a, int b) { i = a; j = b; } // Display i and j void show () { System.out.println( "i and j: " + i + " " + j); } } class B extends A { int k; B( int a, int b, int c)...

OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 15 : Nesting of Methods in Java

  Unit - 2 Topic - 15 : Nesting of Methods in Java In Java, normally a method of a class is called using an object of that class with the dot ( . ) operator . However, if one method of a class calls another method from the same class , we don’t need to use an object — we can call it directly by name. This concept is called Nesting of Methods . Example Program // Program to demonstrate Nesting of Methods class Test { int a, b; // Constructor Test( int p, int q) { a = p; b = q; } // Method to find the greatest of two numbers int greatest () { if (a >= b) return a; else return b; } // Method that calls another method (nested call) void display () { int great = greatest(); // Calling greatest() without object System.out.println( "The Greatest Value = " + great); } } class Temp { public static void main (String args[]) { ...

OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 14 : Recursive Methods

  Unit - 2 Topic - 14 : Recursive Methods Recursion is the process where a method calls itself, either directly or indirectly. In Java, recursion is often used to solve problems that can be broken down into smaller subproblems of the same type. A recursive method must have: Base Case – A condition that stops further recursive calls. Recursive Call – The method calls itself with a modified argument. Example: Factorial using Recursion // A simple example of recursion (factorial) class Factorial { // This is a recursive function int fact ( int n) { if (n == 1 ) // Base case return 1 ; return fact(n - 1 ) * n; // Recursive call } } class Recursion { public static void main (String args[]) { Factorial f = new Factorial (); System.out.println( "Factorial of 3 is " + f.fact( 3 )); System.out.println( "Factorial of 4 is " + f.fact( 4 )); System.out.println(...

OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 13 : Class Objects as Parameters in Methods

  Unit - 2 Topic - 13 : Class Objects as Parameters in Methods In Java, you can pass entire objects as parameters to methods. This allows you to compare objects, update their values, or perform operations involving multiple instances of a class. Example: Passing Objects to Methods class Test { int a, b; // Constructor Test( int i, int j) { a = i; b = j; } // Compare two Test objects boolean equals (Test o) { if (o.a == a && o.b == b) return true ; else return false ; } } class PassOb { public static void main (String args[]) { Test ob1 = new Test ( 100 , 22 ); Test ob2 = new Test ( 100 , 22 ); Test ob3 = new Test (- 1 , - 1 ); System.out.println( "ob1 == ob2: " + ob1.equals(ob2)); System.out.println( "ob1 == ob3: " + ob1.equals(ob3)); } } Output: ob1 == ob2: true ob1 == ob3: fal...

OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 12 : Overloading Methods in Java

  Unit - 2 Topic - 12 : Overloading Methods in Java In Java, it is possible to define two or more methods within the same class that share the same name — as long as their parameter lists are different . This is called Method Overloading , and it’s one way Java implements polymorphism . Rules for Method Overloading Methods must have the same name . Parameter lists must differ (different number or type of parameters). Return type can be the same or different — but it alone cannot distinguish overloaded methods. Example: Method Overloading // Demonstrate method overloading class OverloadDemo { // No parameters void test () { System.out.println( "No parameters" ); } // One integer parameter void test ( int a) { System.out.println( "a: " + a); } // Two integer parameters void test ( int a, int b) { System.out.println( "a and b: " + a + " " + b); } ...

OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 11 : Introducing Methods in Java

Unit - 2 Topic - 11 :Introducing Methods in Java In Java, a method is a block of code that performs a specific task. Methods make programs modular , reusable , and easy to maintain . General Form of a Method type name (parameter-list) { // body of method } Where: type → The data type of the value returned by the method. Can be any valid type, including custom class types. If no value is returned, use void . name → The name of the method (must be a valid identifier). parameter-list → A list of parameters (type and variable name), separated by commas. If no parameters are needed, leave it empty. return value; → Used to send a value back to the caller (for non-void methods). Example 1: Method Without Return Value Here, the method volume() computes and displays the volume of a box directly. class Box { double width; double height; double depth; // Method to display volume void volume () { System.out.print( "V...

OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 10 : this Keyword in Java

  Unit - 2 Topic - 10 : this Keyword in Java In Java, sometimes a method needs to refer to the object that invoked it . To enable this, Java provides the this keyword. What is this ? this is a reference to the current object (the object whose method or constructor is being executed). You can use this anywhere a reference to the current object is needed. It’s implicitly passed to instance methods and constructors. 1. Simple Use of this in a Constructor Even if you don't explicitly use this , Java automatically applies it when accessing instance variables. Example: class Box { double width, height, depth; // Constructor using this keyword Box( double w, double h, double d) { this .width = w; this .height = h; this .depth = d; } void display () { System.out.println( "Width: " + width + ", Height: " + height + ", Depth: " + depth); } } public class ThisExamp...

OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 9 : Passing Arguments by Value and by Reference in Java

  Unit - 2 Topic - 9 : Passing Arguments by Value and by Reference in Java When a method is called in a programming language, the way the arguments are passed determines how changes inside the method affect the original variables. 1. Call by Value In call by value , a copy of the variable's value is passed to the method. Changes made inside the method do not affect the original variable. In Java: All primitive data types ( int , float , char , etc.) are passed by value . Example: Call by Value // Simple types are passed by value class Test { void meth ( int i, int j) { i *= 2 ; j /= 2 ; } } public class CallByValue { public static void main (String[] args) { Test ob = new Test (); int a = 15 , b = 20 ; System.out.println( "a and b before call: " + a + " " + b); ob.meth(a, b); System.out.println( "a and b after call: " + a + " " ...

OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 8 : Final Class and Methods in Java

  Unit - 2 Topic - 8 : Final Class and Methods in Java The final keyword in Java can be applied to classes, methods, and variables . In this section, we will focus on its use with classes and methods . 1. Final Class A final class is a class that cannot be inherited . Once a class is declared as final , no other class can extend it . Uses of Final Class Prevent Inheritance Example: All wrapper classes in Java ( Integer , Float , Double , etc.) are final. Create Immutable Classes Example: The predefined String class is final , ensuring that its state cannot be changed after creation. Example: Final Class final class A { void display () { System.out.println( "This is a final class." ); } } // The following is illegal and will cause a compile-time error // class B extends A { } // ❌ Compile Error public class FinalClassDemo { public static void main (String[] args) { A obj = new A (); obj.di...