[!INDEX]

  1. Poly - allows one mt() or object to behave in different ways depending on the context in which it is used
  2. ol-Definition + multiple mt() + same name + diff sign.
  3. Exceptions
  4. Not depends on return type (below is wrong), int and string
  5. type of Mt -OL ( number, data, sequence )
  6. AP ( char ) = (string) & (int)
  7. Data type priority List (BSC ILFD)
  8. Implicit Widening Conversion -( windening - small to larger ) (implicitly conversion - one to another data )
  9. (char)= (object) & (String)
  10. Autoboxing ( primitive - corresponding - wrapper class )
  11. (null) = (Stringbuffer) & (String)
  12. above solution + ((String)null) + ((StringBuffer)null)
  13. (int, int) = (float, int) & (int, float)
  14. above solution = (10, 20.0f) + (10.0f, 20)
  15. ct() ol - car() + car(" ") + car( " ", 12) + car( " ", 12, " ")

1. Poly - allows one mt() or object to behave in different ways depending on the context in which it is used

[!NOTE]

  1. In simpler terms, polymorphism means "many forms."
  2. It allows one method or object to behave in different ways depending on the context in which it is used.
  3. Type = compile time ( static polymorphism), by m/t overloading ( complier handle) run time ( dynamic polymorphism) by m/t overriding ( JVM handle )

2 - ol-Definition + multiple mt() + same name + diff sign.

[!NOTE]

  1. a class can have multiple methods with the same name, but different signatures (parameter lists).
  2. multiple m/t with same name.
  3. must have same class.
  4. Static methods can also be overloaded.
  5. Overloaded methods can throw different exceptions.

3 - Exceptions

void show() throws IOException {} 
void show(int a) throws SQLException {}

4 - Not depends on return type (below is wrong), int and string

int show(){return 0} 
stirng show(){}

5 - type of Mt -OL

[!NOTE]

  1. Number of parameters .
  2. Data types of parameters .
  3. Sequence of parameters.

void show(int a) {}
void show(double a) {} // Different data type
void show(int a, double b) {} // Different number and sequence of parameters

6 - AP ( char ) = (string) & (int)

class F_010_Test {
    void show(int a){
        System.out.println("int method");
    }
    void show(String a){
        System.out.println("String method");
    }
    public static void main(String[] args) {
        F_010_Test t = new F_010_Test();
        t.show(10);      // Calls the show(int a) method
        t.show("Raj");   // Calls the show(String a) method
        t.show('a');     // This is the interesting part
    }
}
int method
String method
int method

7 - Data type priority List (BSC ILFD)

[!important]

  1. In Java, data types have a specific order of size or "wideness." Implicit widening conversions happen automatically when you assign a value of a smaller data type to a larger data type. Here is the hierarchy of primitive data types from smaller to larger:
  2. byte - (8 bits)
  3. short - (16 bits)
  4. char - (16 bits, unsigned)
  5. int - (32 bits)
  6. long - (64 bits)
  7. float - (32 bits, floating-point)
  8. double - (64 bits, floating-point)

8 - Implicit Widening Conversion -( windening - small to larger ) (implicitly conversion - one to another data )

[!important]


9 - (char)= (object) & (String)

class F_011_A_Temp {
    void show(Object a) {
        System.out.println("object method");
    }
    void show(String a) {
        System.out.println("String method");
    }
    public static void main(String[] args) {
        F_011_A_Temp t = new F_011_A_Temp();
        t.show('a'); // This is the point of interest
    }
}
object method

[!NOTE]

  1. show(Object a): The char can be autoboxed to a Character object, and since Character is a subclass of Object, this method can accept it.

10. 1. Autoboxing ( primitive - corresponding - wrapper class )

[!NOTE] What is Autoboxing?

  1. compiler automatically converts a primitive type into its corresponding wrapper class when an object type is expected. Similarly, unboxing is the reverse process, where an object of a wrapper class is automatically converted back to a primitive type.
  2. Primitive types: char, int, boolean, float, etc.
  3. Wrapper classes: Character, Integer, Boolean, Float, etc.

11 - (null) = (Stringbuffer) & (String)

class F_014_Test {
    void show(StringBuffer a) {
        System.out.println("String Buffer");
    }
    void show(String a) {
        System.out.println("String method");
    }
    public static void main(String[] args) {
        F_014_Test t = new F_014_Test();
        t.show(null); // error
    }
}

[!important]


12 - proper code

class F_014_Test {
    void show(StringBuffer a) {
        System.out.println("String Buffer");
    }
    void show(String a) {
        System.out.println("String method");
    }
    public static void main(String[] args) {
        F_014_Test t = new F_014_Test();
        t.show((String)null);// Calls the show(String a) method
        t.show((StringBuffer)null);// Calls the show(StringBuffer a) method
    }
}

13 - (int, int) = (float, int) & (int, float)

class person {
    void show(int a, float b){
        System.out.println("int float method");
    }
    void  show(float a, int b){
        System.out.println("Float int m/t ");
    }
    public static void main(String[] args) {
        person t = new person();
        t.show(10,20); // error ( can't promote)
    }
}

[!important]


14 - solution

class F_016_Test {
    void show(int a, float b){
        System.out.println("int float method");
    }
    void show(float a, int b){
        System.out.println("Float int method");
    }
    public static void main(String[] args) {
        F_016_Test t = new F_016_Test();
        t.show(10, 20.0f);  // Calls the show(int, float) method,
        t.show(10.0f, 20);  // Calls the show(float, int) method,
    }
}

15 - ct() ol - car() + car(" ") + car( " ", 12) + car( " ", 12, " ")

class Car {
    String model;
    int year;
    String color;

    // Constructor 1: No arguments
    Car() {
        model = "Unknown";
        year = 0;
        color = "Not specified";
        System.out.println("Default constructor called");
    }

    // Constructor 2: One argument
    Car(String model) {
        this.model = model;
        year = 2024;
        color = "Not specified";
        System.out.println("Constructor with model called");
    }

    // Constructor 3: Two arguments
    Car(String model, int year) {
        this.model = model;
        this.year = year;
        color = "Not specified";
        System.out.println("Constructor with model and year called");
    }

    // Constructor 4: Three arguments
    Car(String model, int year, String color) {
        this.model = model;
        this.year = year;
        this.color = color;
        System.out.println("Constructor with model, year, and color called");
    }

    void displayInfo() {
        System.out.println("Car model: " + model + ", Year: " + year + ", Color: " + color);
    }

    public static void main(String[] args) {
        // Using different constructors
        Car car1 = new Car();  // Calls Constructor 1
        Car car2 = new Car("Toyota");  // Calls Constructor 2
        Car car3 = new Car("Honda", 2020);  // Calls Constructor 3
        Car car4 = new Car("Tesla", 2024, "Red");  // Calls Constructor 4

        // Display the information of each car
        car1.displayInfo();
        car2.displayInfo();
        car3.displayInfo();
        car4.displayInfo();
    }
}

Default constructor called
Constructor with model called
Constructor with model and year called
Constructor with model, year, and color called
Car model: Unknown, Year: 0, Color: Not specified
Car model: Toyota, Year: 2024, Color: Not specified
Car model: Honda, Year: 2020, Color: Not specified
Car model: Tesla, Year: 2024, Color: Red