- Приведение типов объектов в Java
- Дальнейшее чтение:
- Основы Java Generics
- Java экземпляр оператора
- 2. Примитив против Ссылка
- 3. Апкастинг
- What is Type Casting in Java? Casting one Class to other class or interface Example
- What is Type Casting in Java? Up Casting and Down Cast
- Type casting example in Java
- Java Type Casting (Downcasting, Upcasting)
- Upcasting
- Implicit Upcasting
- Explicit Upcasting
- Upcasting Narrows the Choice Of Methods
- Upcasting with Interfaces
- Upcasting to Object Class
- Downcasting in Java
- ClassCastException in TypeCasting
- Using cast() and isInstance() methods
Приведение типов объектов в Java
Система типов Java состоит из двух типов типов: примитивы и ссылки.
Мы рассмотрели примитивные преобразования вthis article, и здесь мы сосредоточимся на приведении ссылок, чтобы получить хорошее представление о том, как Java обрабатывает типы.
Дальнейшее чтение:
Основы Java Generics
Краткое введение в основы Java Generics.
Java экземпляр оператора
Узнайте об операторе instanceof в Java
2. Примитив против Ссылка
Хотя примитивные преобразования и приведение ссылочных переменных могут выглядеть одинаково, они довольноdifferent concepts.
В обоих случаях мы «превращаем» один тип в другой. Но в упрощенном виде примитивная переменная содержит свое значение, а преобразование примитивной переменной означает необратимые изменения ее значения:
double myDouble = 1.1; int myInt = (int) myDouble; assertNotEquals(myDouble, myInt);
После преобразования в приведенном выше примере переменнаяmyInt равна1, и мы не можем восстановить из нее предыдущее значение1.1.
Reference variables are different; ссылочная переменная относится только к объекту, но не содержит самого объекта.
Приведение ссылочной переменной не затрагивает объект, к которому она относится, а только помечает этот объект другим способом, расширяя или сужая возможности для работы с ним. Upcasting narrows the list of methods and properties available to this object, and downcasting can extend it.с
Ссылка похожа на дистанционное управление объектом. Пульт дистанционного управления имеет больше или меньше кнопок в зависимости от его типа, а сам объект хранится в куче. Когда мы выполняем кастинг, мы меняем тип пульта дистанционного управления, но не меняем сам объект.
3. Апкастинг
Casting from a subclass to a superclass is called upcasting. Как правило, преобразование выполняется неявным образом.
Обновление тесно связано с наследованием — еще одна ключевая концепция в Java. Обычно используются ссылочные переменные для ссылки на более конкретный тип. И каждый раз, когда мы делаем это, происходит неявное обновление.
Чтобы продемонстрировать апкастинг, давайте определим классAnimal:
What is Type Casting in Java? Casting one Class to other class or interface Example
Type casting in Java is to cast one type, a class or interface, into another type i.e. another class or interface. Since Java is an Object-oriented programming language and supports b oth Inheritance and Polymorphism, It’ s easy that Super class reference variable is pointing to SubClass objects but the catch here is that there is no way for Java compiler to know that a Superclass variable is pointing to SubClass object. This means you can not call a method that is declared in the subclass. In order to do that, you first need to cast t he Object back into its original type. This is called type casting in Java. You can type cast both primitive and reference type in Java. The concept of casting will be clearer when you will see an example of type casting in the next section.
Type casting also comes with the risk of ClassCastException in Java, which is quite common with a method that accepts Object type and later types cast into more specific types.
We will see when ClassCastException comes during type casting an d How to avoid it in the coming section of this article. Another worth noting point here is that from Java 5 onwards you can use Generics to write type-safe code to r educe the amount of type casting in Java which also reduces the risk of java.lang.ClassCastException at runtime.
What is Type Casting in Java? Up Casting and Down Cast
From the first paragraph, we pretty much know What is type casting in Java. Anyway, In simple words, type casting is a process of converting on e type, whi ch cou ld be a class or interface t o another, But as per rules of Java programming language, only classes or i nterfaces (collectively known as Type) from the same type hierarchy can be cast or converted into each other.
If you try to cast two objects which don’t share same type hierarchy, i.e. there is no parent-child relationship bet ween them, y ou wil l get compile time error. On the other hand, if you typecast objects fr om same type hierarchy but the object which you are casting are not of the same type on which you are casting then it will throw ClassCastException in Java.
Some people may ask why do you need type casting? well, you need type casting to get access to fields and methods declared on the target type or class. You can not access them with any other type. Let’s see a simple example of type casting in Java with two classes Base and Derived which share the same type hierarchy.
Type casting example in Java
In this Example of type casting in Java, we have two classes, Base , and Derived . Derived class extends Base i.e. Base is a Super class and Derived is a Subclass. So their type hierarchy looks like the following tree :
Java Type Casting (Downcasting, Upcasting)
Typecasting is the process of conversion of the type of data. Object Typecasting can be of two types, depending on whether we are converting from parent type to child or going in the other way. In this tutorial, we will learn about Upcasting and Downcasting of objects in Java.
Upcasting
- The process of casting an object of child class to the parent class is called Upcasting.
- Upcasting can be done implicitly or explicitly.
- Upcasting will reduce the choice of methods, we can use on the typecasted object.
- However, for overridden methods, static or dynamic binding comes into play, and it will decide the method implementation to use.
To demonstrate upcasting, we will use a child and a parent class. The child class has an overridden print() method and a printName() method. The following code shows the implementation.
class SuperClass < public void print() < System.out.println("Printing from SuperClass"); >> class Child extends SuperClass < String name; Child(String s) < this.name = s; >@Override public void print() < System.out.println("Printing from Child class"); >public void printName() < System.out.println(this.name); >>
Implicit Upcasting
When we assign an object of the Child class to a reference variable of SuperClass type, then implicit upcasting takes place. We can use the print() method on this object, and the method implementation of the Child class will be used.
public static void main(String[] args) < Child c = new Child("child"); c.print(); //Implicit Upcasting SuperClass s2 = new Child("Second Child"); s2.print();//print() method of Child class is called due to dynamic binding >
Printing from Child class
Printing from Child class
Another example of implicit upcasting is shown below. If we create an ArrayList of SuperClass type and add child classes objects to it, then they are implicitly upcasted to the SuperClass. We also created a new class(AnotherChild).
import java.util.ArrayList; class AnotherChild extends SuperClass < public void print() < System.out.println("Printing from AnotherChild class"); >> public class TypeCastingDemo < public static void main(String[] args) < SuperClass obj1 = new SuperClass(); Child obj2 = new Child("First Child"); AnotherChild obj3 = new AnotherChild(); ArrayListsuperClassList = new ArrayList<>(); superClassList.add(obj1); superClassList.add(obj2); superClassList.add(obj3); for(int i=0; i >
Printing from SuperClass
Printing from Child class
Printing from AnotherChild class
Explicit Upcasting
We can also explicitly cast the object, but it is not really required.
public static void main(String[] args) < Child c = new Child("child"); c.print(); //Explicit Upcasting SuperClass s2 = (SuperClass) new Child("Second Child"); s2.print();//print() method of Child class is called due to dynamic binding >
Printing from Child class
Printing from Child class
Upcasting Narrows the Choice Of Methods
As discussed above, upcasting will narrow our choices of methods. For example, after upcasting a Child class object to SuperClass, we can no longer use the printName() method of the Child class. We will get a compilation error.
public class TypeCastingDemo < public static void main(String[] args) < SuperClass s = new Child("First Child"); s.printName();//Error >>
Exception in thread «main» java.lang.Error: Unresolved compilation problem:
The method printName() is undefined for the type SuperClass
Upcasting with Interfaces
Upcasting also works with interfaces. Let’s create a new interface and implement it in our Child class.
interface Name < public void printName(); >class Child extends SuperClass implements Name < String name; Child(String s) < this.name = s; >@Override public void print() < System.out.println("Printing from Child class"); >public void printName() < System.out.println(this.name); >>
Now, we will create an object of the Child class and assign it to an interface type variable. Implicit typecasting will take place in this case. We can use the implemented printName() method.
public class TypeCastingDemo < public static void main(String[] args) < Name obj = new Child("First Child"); obj.printName(); >>
But we can’t use the overridden print() method.
public class TypeCastingDemo < public static void main(String[] args) < Name obj = new Child("First Child"); obj.print();//Error >>
Exception in thread «main» java.lang.Error: Unresolved compilation problem:
The method print() is undefined for the type Name
Upcasting to Object Class
All classes internally extend the Object class. Object class is the superclass of all other classes. So we can upcast the Child class(or any other class) to the Object class.
public class TypeCastingDemo < public static void main(String[] args) < Object o = new Child("hello"); System.out.print(o.getClass()); >>
Downcasting in Java
- Downcasting can be considered as the reverse of Upcasting. We are typecasting an object of the parent class to the child class.
- Unlike upcasting, downcasting is not done implicitly by the compiler.
As we saw in upcasting, the following code gives an error.
public class TypeCastingDemo < public static void main(String[] args) < SuperClass s = new Child("First Child"); s.printName();//Error >>
To make it work, we can downcast from the SuperClass to the Child class. This will give us access to the print() method of the Child class.
public class TypeCastingDemo < public static void main(String[] args) < SuperClass s = new Child("First Child"); ((Child) s ).printName();//Downcasting from SuperClass to Child class. >>
ClassCastException in TypeCasting
The ClassCastException is a commonly encountered exception that occurs when downcasting. The following code demonstrates this error.
We created a new AnotherChild class and in the main() method, we created three objects, one for each class(SuperClass class, Child class, and AnotherChild class), and we are adding them to an ArrayList . Now when we cast each object to the Child class, then we will get this error. This happens because there is no parent-child relationship between the AnotherChild class and the Child class. So we can’t downcast from AnotherChild to Child.
class AnotherChild extends SuperClass < public void print() < System.out.println("Printing from AnotherChild class"); >> public class TypeCastingDemo < public static void main(String[] args) < ArrayListlist = new ArrayList<>(); SuperClass s = new Child("First Child"); Child c1 = new Child("Second Child"); AnotherChild c2 = new AnotherChild(); list.add(s); list.add(c1); list.add(c2); for(int i=0; i > >
First Child
Second Child
Exception in thread «main» java.lang.ClassCastException: class AnotherChild cannot be cast to class Child (AnotherChild and Child are in unnamed module of loader ‘app’)
at TypeCastingDemo.main(TypeCastingDemo.java:57)
We can use the instanceof operator to rectify this error. This operator returns true if the object is of a particular type. We can correct the above code by using the instanceof operator before downcasting.
public class TypeCastingDemo < public static void main(String[] args) < ArrayListlist = new ArrayList<>(); SuperClass s = new Child("First Child"); Child c1 = new Child("Second Child"); AnotherChild c2 = new AnotherChild(); list.add(s); list.add(c1); list.add(c2); for(int i=0; i > >
Using cast() and isInstance() methods
Java provides an alternative way of downcasting. We can use the cast() method to downcast from a superclass to a subclass. Similarly, the isInstance() method can replace the instanceof operator. These two methods are often used together in generic methods. Let’s rewrite the above code using these methods.
public class TypeCastingDemo < public static void main(String[] args) < ArrayListlist = new ArrayList<>(); SuperClass s = new Child("First Child"); Child c1 = new Child("Second Child"); AnotherChild c2 = new AnotherChild(); list.add(s); list.add(c1); list.add(c2); for(int i=0; i > > >
In this tutorial, we learned how to upcast and downcast objects in Java. Upcasting can be done implicitly or explicitly and converts the type from subclass to superclass. It narrows down the methods we can use with the object. Downcasting is the process of casting superclass to subclass. We can only explicitly downcast an object. Downcasting is performed to access the methods and properties of the child class. We must use the instanceof operator or the isInstance() method to downcast safely. It is done to avoid the ClassCastException.