Compiler time TypeCasting in Java ? with Example
Java mein type casting compile time par bhi hota hai, lekin uska ek specific context hota hai jo compile-time type safety ko maintain karta hai.
***********************************************************************************
### Compile-Time Type Casting in Java
1. **Syntax and Context**:
- Compile-time type casting Java mein hota hai jab hum ek reference variable ko uski derived class (subclass) mein convert karte hain.
- Syntax: `SubclassType obj = (SubclassType) superTypeObject;`
- Yahaan `superTypeObject` ek superclass type ka reference hai jo subclass type mein cast kiya ja raha hai.
2. **Type Safety Check**:
- Compile-time type casting ek type safety check provide karta hai. Jab hum ek superclass type ko subclass type mein cast karte hain, tab Java compile-time par yeh check karta hai ki kya yeh conversion possible hai.
- Agar conversion possible nahi hai (for example, jab superclass reference actual mein subclass object ko refer nahi kar raha hai), to compile-time error generate hota hai.
***********************************************************************************
3. **Example**:
```java
class Animal {
void makeSound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class TypeCastingExample {
public static void main(String[] args) {
Animal animal = new Animal();
Dog dog = (Dog) animal; // Compile-time error: Inconvertible types
}
}
***********************************************************************************
```
- Is example mein, `Animal` type ka reference ko `Dog` type mein cast karne ki koshish ki gayi hai, lekin `animal` reference actual mein `Animal` object ko refer kar raha hai, jo `Dog` object nahi hai. Isliye compile-time par error aayega.
4. **Compile-Time Behavior**:
- Compile-time type casting se yeh ensure hota hai ki runtime par type conversion errors avoid ho sakein.
- Yeh type safety ko maintain karta hai aur unexpected runtime exceptions se bachata hai.
***********************************************************************************
Corrected code :
***********************************************************************************
class Animal {
void makeSound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class TypeCasting_1 {
public static void main(String[] args) {
// Step 1: Creating a Dog object and assigning it to an Animal reference
Animal animal = new Dog(); // Animal reference, Dog object
animal.makeSound(); // Calls makeSound() from Animal class
// Step 2: Type casting Animal reference to Dog reference
Dog dog = (Dog) animal;
// Step 3: Accessing methods using Dog reference
dog.makeSound(); // Calls makeSound() from Animal class (inherited by Dog)
dog.bark(); // Calls bark() from Dog class
}
}
### Conclusion
Compile-time type casting Java mein ek important concept hai jo type safety ko ensure karta hai aur runtime errors ko minimize karta hai. Isse compile-time par hi errors detect hote hain jisse code robustness improve hoti hai.
Upcasting:
Vehicle vehicle_ref1 = new Bike();
: Yahanvehicle_ref1
ekVehicle
type ka reference hai joBike
type ke object ko point kar raha hai.- Upcasting mein hum superclass (
Vehicle
) ka reference subclass (Bike
) ke object ko refer kar sakte hain.
Compile-Time Behavior:
- Compile-time par Java
vehicle_ref1
koVehicle
type ka reference maan lega, isliyestart()
method ko call karne mein koi problem nahi hai kyunkistart()
methodVehicle
class mein defined hai.
- Compile-time par Java
Accessing Subclass Methods:
- Jab hum
vehicle_ref1
seride()
method ko access karne ki koshish karenge, compile-time error aayega kyunkiVehicle
class meinride()
method define nahi hai. - Yeh compile-time type checking se hota hai jo type safety ko maintain karta hai. Java compiler yeh ensure karta hai ki reference type ke methods hi compile-time par access kiye ja sakein.
- Jab hum
Benefits of Compile-Time Type Checking:
- Type Safety: Code compile hone se pehle hi type mismatches ko detect kar leta hai.
- Robustness: Runtime errors jaise ki
ClassCastException
se bachata hai. - Maintainability: Code ko samajhna aur maintain karna aasan hota hai kyunki type information compile-time par available hoti hai.
Conclusion:
Java mein compile-time type checking ek crucial feature hai jo programming errors ko reduce karta hai aur code quality ko improve karta hai. Isse hume pata chalta hai ki hum kis methods ko reference type ke basis par access kar sakte hain aur kis methods ko nahi. Agar aapko aur koi query hai is topic par ya kisi aur topic par, toh mujhse pooch sakte hain!
Comments
Post a Comment