Java Five programs to generate class-cast Exception.

Java Five programs to generate class-cast Exception. ClassCast Exception in Java occurs when you try to cast an object to a subclass of which it is not an instance. Here are five examples to illustrate how this exception can be generated, along with explanations.

1: Basic ClassCast Exception.

java
class Animal {}
class Dog extends Animal {}

public class ClassCastExceptionExample1 {
    public static void main(String[] args) {
        Animal animal = new Animal();
        Dog dog = (Dog) animal; // This will throw ClassCastException
    }
}

Explanation:
1. We define a base class `Animal` and a subclass `Dog`.
2. We create an instance of `Animal`.
3. We attempt to cast the `Animal` instance to `Dog`.
4. Since `animal` is not an instance of `Dog`, a `ClassCastException` is thrown.

2: Using Collections

java
import java.util.ArrayList;

public class ClassCastExceptionExample2 {
    public static void main(String[] args) {
        ArrayList<Animal> animals = new ArrayList<>();
        animals.add(new Animal());

        Dog dog = (Dog) animals.get(0); // This will throw ClassCastException
    }
}

Explanation:
1. An `ArrayList` of type `Animal` is created and an `Animal` instance is added.
2. We attempt to retrieve the first element and cast it to `Dog`.
3. Since the object in the list is of type `Animal`, this results in a `ClassCastException`.

3: Incorrect Object Type in Array

java
public class ClassCastExceptionExample3 {
    public static void main(String[] args) {
        Object[] objects = new Object[1];
        objects[0] = new Animal();

        Dog dog = (Dog) objects[0]; // This will throw ClassCastException
    }
}

Explanation:
1. An array of type `Object` is created, and an `Animal` is added to it.
2. We try to cast the first element of the array to `Dog`.
3. Since it’s actually an `Animal`, a `ClassCastException` is thrown.

4: Mixed Types in a List

java
import java.util.ArrayList;

class Cat extends Animal {}

public class ClassCastExceptionExample4 {
    public static void main(String[] args) {
        ArrayList<Animal> animals = new ArrayList<>();
        animals.add(new Cat());

        Dog dog = (Dog) animals.get(0); // This will throw ClassCastException
    }
}

Explanation:
1. We define another subclass `Cat`.
2. We create an `ArrayList` of `Animal` and add an instance of `Cat`.
3. We attempt to cast the first element to `Dog`, leading to a `ClassCastException` since the object is a `Cat`.

5: Downcasting in a Hierarchy

java
class Vehicle {}
class Car extends Vehicle {}

public class ClassCastExceptionExample5 {
    public static void main(String[] args) {
        Vehicle vehicle = new Vehicle();

        Car car = (Car) vehicle; // This will throw ClassCastException
    }
}

Explanation:
1. `Vehicle` is a base class with `Car` as a subclass.
2. An instance of `Vehicle` is created.
3. We attempt to cast this instance to `Car`, resulting in a `ClassCast Exception` since it’s not a `Car`.

Summary

In each of these examples, the `ClassCast Exception` arises when attempting to cast an object to a type that it is not compatible with. This typically happens when dealing with inheritance and collections. Always ensure that the object being cast is indeed an instance of the target class or its subclasses to avoid this exception.

Leave a Reply

Your email address will not be published. Required fields are marked *