Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, object type conversion (also called casting) is the process of converting an object from one class type to another. This tutorial covers the basics of object type conversion in Java, including upcasting, downcasting, and the instanceof
operator.
Upcasting is converting a reference of a subclass type to a superclass type. Upcasting is always safe and performed automatically by the Java compiler because a subclass object is also an instance of its superclass.
Example:
class Animal { public void makeSound() { System.out.println("Some sound..."); } } class Dog extends Animal { @Override public void makeSound() { System.out.println("Woof!"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); Animal animal = dog; // Upcasting from Dog to Animal animal.makeSound(); // Output: Woof! } }
Downcasting is converting a reference of a superclass type to a subclass type. Downcasting is not automatically performed by the Java compiler because it can lead to runtime errors if the object is not an instance of the target subclass. You must perform an explicit type cast to perform downcasting.
Example:
class Animal { // ... } class Dog extends Animal { public void bark() { System.out.println("Woof!"); } } public class Main { public static void main(String[] args) { Animal animal = new Dog(); Dog dog = (Dog) animal; // Downcasting from Animal to Dog dog.bark(); // Output: Woof! } }
The instanceof
operator is used to check if an object is an instance of a specific class or one of its subclasses. It's particularly useful for safely performing downcasting because you can verify the object's type before performing the cast.
Example:
class Animal { // ... } class Dog extends Animal { // ... } class Cat extends Animal { // ... } public class Main { public static void main(String[] args) { Animal animal = new Dog(); if (animal instanceof Dog) { Dog dog = (Dog) animal; System.out.println("This animal is a Dog."); } else if (animal instanceof Cat) { Cat cat = (Cat) animal; System.out.println("This animal is a Cat."); } else { System.out.println("This animal is neither a Dog nor a Cat."); } } }
In this tutorial, we discussed the basics of object type conversion in Java, including upcasting, downcasting, and the instanceof
operator. Understanding how to use these concepts effectively is crucial for working with inheritance and polymorphism in your Java applications.
Upcasting and Downcasting in Java with Examples:
// Upcasting Animal animal = new Dog(); // Downcasting if (animal instanceof Dog) { Dog dog = (Dog) animal; // Perform operations specific to Dog }
Upcasting in Java and Its Use Cases:
Animal animal = new Dog(); // Upcasting
Downcasting in Java with instanceof
:
ClassCastException
.Animal animal = new Dog(); if (animal instanceof Dog) { Dog dog = (Dog) animal; // Downcasting }
Java Casting Between Primitive Types and Objects:
int intValue = 42; Integer integerValue = (Integer) intValue; // Boxing (Auto or Manual)
Polymorphism and Casting in Java:
Animal animal = new Dog(); animal.makeSound(); // Calls Dog's makeSound if overridden
Covariant Return Types and Casting in Java:
class Animal { Animal reproduce() { return new Animal(); } } class Dog extends Animal { @Override Dog reproduce() { return new Dog(); } // Covariant return type }
Java Casting Exceptions and Handling:
ClassCastException
if the types are incompatible. Using instanceof
helps avoid exceptions.if (animal instanceof Dog) { Dog dog = (Dog) animal; // Downcasting with instanceof check } else { // Handle the case when casting is not possible }
Java Casting with Interfaces:
interface Shape { /*...*/ } class Circle implements Shape { /*...*/ } Shape shape = new Circle();
Java Generics and Type Casting:
List<String> strings = new ArrayList<>(); strings.add("Hello"); String str = strings.get(0); // No casting needed