{"id":6563,"date":"2024-04-21T23:57:28","date_gmt":"2024-04-22T06:57:28","guid":{"rendered":"https:\/\/zentut.com\/java-tutorial\/java-casting\/"},"modified":"2024-04-21T23:58:35","modified_gmt":"2024-04-22T06:58:35","slug":"java-casting","status":"publish","type":"page","link":"https:\/\/www.zentut.com\/java-tutorial\/java-casting\/","title":{"rendered":"Java Casting"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use Java casting that convert an object of a class to another.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Java Casting<\/h2>\n\n\n\n<p>In Java, casting is a technique that converts an object of a <a href=\"https:\/\/zentut.com\/java-tutorial\/java-class\/\">class<\/a> into another. Casting allows you to access fields and methods that are specific to a target class.<\/p>\n\n\n\n<p>Technically, you should only use casting when there is an actual relationship between the classes in the class hierarchy. It means you can only cast an object of classes that have a <a href=\"https:\/\/zentut.com\/java-tutorial\/java-inheritance\/\">superclass-subclass relationship<\/a>.<\/p>\n\n\n\n<p>If you attempt to cast unrelated classes, you will encounter an error runtime. More specifically, you&#8217;ll get an <code>ClassCastException<\/code>.<\/p>\n\n\n\n<p>Java supports two types of object castings: upcasting and downcasting.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Upcasting<\/h3>\n\n\n\n<p>Upcasting converts an object of a subclass to its superclass:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">Superclass object = <span class=\"hljs-keyword\">new<\/span> Subclass(); <span class=\"hljs-comment\">\/\/ upcasting<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, we create a new object of a subclass and assign it to a reference variable with the type of Superclass. The upcasting is always safe and doesn&#8217;t require an explicit cast.<\/p>\n\n\n\n<p>In the following syntax, we assign an instance of a subclass to a reference of a superclass:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">Superclass object = subclassObject; <span class=\"hljs-comment\">\/\/ upcasting<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Downcasting<\/h3>\n\n\n\n<p>Downcasting is the casting of an object of a superclass to a subclass:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">Subclass subObj = (Subclass) superObj; <span class=\"hljs-comment\">\/\/ Downcasting<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that you can only perform a downcasting if the original object is of the subclass type. And you need to use parentheses and the subclass type.<\/p>\n\n\n\n<p>For safety, you can use the <code>instanceof<\/code> operator to check if the object being downcasted is an instance of the Subclass:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">if<\/span> (obj <span class=\"hljs-keyword\">instanceof<\/span> Subclass) {\n    Subclass subObj = (Subclass) obj; <span class=\"hljs-comment\">\/\/ Downcast only if safe<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Java casting example<\/h2>\n\n\n\n<p>Suppose you have two classes, <code>Person<\/code> and <code>Employee<\/code>. The <code>Employee<\/code> class is a subclass of the <code>Person<\/code> class.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-comment\">\/\/ Person.java<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span> <\/span>{\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n}\n\n<span class=\"hljs-comment\">\/\/ Employee.java<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Employee<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Person<\/span> <\/span>{\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example illustrates how to perform an upcasting that converts an object of the <code>Employee<\/code> class to the <code>Person<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">Person person = <span class=\"hljs-keyword\">new<\/span> Employee();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this case, the <code>person<\/code> object can access only fields and methods of the <code>Person<\/code> class. And the <code>person<\/code> object cannot access the fields and methods defined in the <code>Employee<\/code> class.<\/p>\n\n\n\n<p>The following example shows how to perform a downcasting that converts <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">Person person = <span class=\"hljs-keyword\">new<\/span> Employee();\n<span class=\"hljs-keyword\">if<\/span> (person <span class=\"hljs-keyword\">instanceof<\/span> Employee) {\n    Employee employee = (Employee) person;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Because it is a downcasting, we use the <code>instanceof<\/code> operator to ensure that the <code>person<\/code> is an instance of the <code>Employee<\/code> class and use explicit cast by placing the <code>Employee<\/code> class in parentheses.<\/p>\n\n\n\n<p>Because of downcasting, the employee can access the fields and methods defined in the <code>Employee<\/code> class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use Java casting to convert an object of one class to another, within the class hierarchy.<\/li>\n\n\n\n<li>Use downcasting to convert an object of a superclass to its subclass.<\/li>\n\n\n\n<li>Use upcasting to convert an object of a subclass to its superclass.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use Java casting that converts an object of a class to another.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":2190,"menu_order":40,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6563","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/comments?post=6563"}],"version-history":[{"count":2,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6563\/revisions"}],"predecessor-version":[{"id":6566,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6563\/revisions\/6566"}],"up":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/2190"}],"wp:attachment":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/media?parent=6563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}