[!INDEX]
- Poly - allows one mt() or object to behave in different ways depending on the context in which it is used
- ol-Definition + multiple mt() + same name + diff sign.
- Exceptions
- Not depends on return type (below is wrong), int and string
- type of Mt -OL ( number, data, sequence )
- AP ( char ) = (string) & (int)
- Data type priority List (BSC ILFD)
- Implicit Widening Conversion -( windening - small to larger ) (implicitly conversion - one to another data )
- (char)= (object) & (String)
- Autoboxing ( primitive - corresponding - wrapper class )
- (null) = (Stringbuffer) & (String)
- above solution + ((String)null) + ((StringBuffer)null)
- (int, int) = (float, int) & (int, float)
- above solution = (10, 20.0f) + (10.0f, 20)
- 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]
- In simpler terms, polymorphism means "many forms."
- It allows one method or object to behave in different ways depending on the context in which it is used.
- 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]
- a class can have multiple methods with the same name, but different signatures (parameter lists).
- multiple m/t with same name.
- must have same class.
- Static methods can also be overloaded.
- 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]
- Number of parameters .
- Data types of parameters .
- 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]
- 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:
- byte - (8 bits)
- short - (16 bits)
- char - (16 bits, unsigned)
- int - (32 bits)
- long - (64 bits)
- float - (32 bits, floating-point)
- double - (64 bits, floating-point)
8 - Implicit Widening Conversion -( windening - small to larger ) (implicitly conversion - one to another data )
[!important]
- Implicit conversion means the compiler automatically converts one data type to another without explicit instruction from the programmer.
- Widening means converting a smaller data type to a larger data type, which is safe and lossless because the larger type can accommodate all possible values of the smaller type.
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]
show(Object a): Thecharcan be autoboxed to aCharacterobject, and sinceCharacteris a subclass ofObject, this method can accept it.
10. 1. Autoboxing ( primitive - corresponding - wrapper class )
[!NOTE] What is Autoboxing?
- 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.
- Primitive types:
char,int,boolean,float, etc.- 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]
- The
nullliteral can be assigned to any reference type.- Both
show(StringBuffer a)andshow(String a)can acceptnullbecausenullcan be assigned to bothStringBufferandString.- The call
t.show(null)is ambiguous because bothshow(StringBuffer a)andshow(String a)can acceptnull.- The compiler cannot decide which method to call, resulting in a compilation error.
- To resolve this, you need to cast
nullto the specific type you want to call.
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]
- Java can perform implicit type conversion (widening) from
inttofloat.- However, both
show(int, float)andshow(float, int)require one of the arguments to be afloat.- When both conversions are possible, Java cannot decide which method to choose because both would require a conversion.
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