MainMenu

Home Core Java Overview Maven Tutorials

Saturday, 14 January 2017

Upcasting and Downcasting in Java with example



For Video : CLICK HERE



What is Casting ?


All casting really means is taking an Object of an particular type and "turning it into" another Object Type.
This process is called casting a variable.
Casting means it is telling the compiler that an object of type A is actually of more specific type B, and thus gaining access to all the methods on B that you would not have had otherwise.

Example :
Object o = "str";
String str = (String)o;
The object being stored in o is actually a string, and therefore we can cast to a string without any problems.

Upcasting and Downcasting in Java with example


Upcasting: When we put the Reference ID of child class object into reference
variable of parent class then it is known as Upcasting :
Example : In below example we have passed Reference ID of child class object into reference
variable of parent class to access the method of parent class :


package csc;
class Parent
{
void call()
{
System.out.println("i am in parent");
}
}
public class Child extends Parent
{
public static void main(String args[])
{
Parent obj = new Child();
obj.call();
}
}
Output : i am in parent

Note : Vice-versa is not possible, means by this object we can not access method of child class :
package csc;
class Parent
{
void call()
{
System.out.println("i am in parent");
}
}
public class Child extends Parent
{
void mum()
{
System.out.println("i am in child");
}
public static void main(String args[])
{
Parent obj = new Child();
obj.call();
obj.mum();
}
}
Output : Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Parent cannot be resolved to a type
at csc.Child.main(Up.java:18)

Important:If parent class and child class both have same method name means Child class is overriding the parent class method then child class method will be called.
Example : This is again above example here method name is same in both child and parent but Now only child class method will be called.

package cpack;
class parent
{
void alpha()
{
System.out.println("i am inside parent class");
}
}
public class Child extends parent
{
void alpha()
{
System.out.println("i am inside child class");
}
public static void main(String args[])
{
parent obj = new Child();
obj.alpha();
}
}

Output :i am inside child class.




Downcasting in Java with example


Downcasting : Downcasting is to put the child class object reference ID back into child class object by using the parent class referene variable.
Upcasting is required for downcasting.
Main use of downcasting is that we can access child class method too from single object.
Example :In this example by single object we will access both parent child class method :

package cpack;
class parent
{
void alpha()
{
System.out.println("i am inside parent class");
}
}
public class Child extends parent
{
void beta()
{
System.out.println("i am inside child class");
}
public static void main(String args[])
{
parent obj = new Child(); //upcasting
obj.alpha();
Child obj1 =(Child)obj; //downcasting
obj1.beta();
}
}
Output :
i am inside parent class
i am inside child class


Tags :-

Upcasting and downcasting in java, Example of upcasting in java with example, example of downcating in java with example

6 comments:

  1. Very useful post for beginners sir:) Hope you included checking of the class with "if(< > instanceof < >)" it would be more useful

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete
  3. Thanks a lot very much for the high quality and results-oriented help.
    I won’t think twice to endorse your blog post to anybody who wants
    and needs support about this area.


    java training in chennai


    java training in bangalore

    ReplyDelete
  4. Ciitnoida provides Core and java training institute in

    noida
    . We have a team of experienced Java professionals who help our students learn Java with the help of Live Base Projects. The object-

    oriented, java training in noida , class-based build

    of Java has made it one of most popular programming languages and the demand of professionals with certification in Advance Java training is at an

    all-time high not just in India but foreign countries too.

    By helping our students understand the fundamentals and Advance concepts of Java, we prepare them for a successful programming career. With over 13

    years of sound experience, we have successfully trained hundreds of students in Noida and have been able to turn ourselves into an institute for best

    Java training in Noida.

    java training institute in noida
    java training in noida

    ReplyDelete
  5. The information you shared is very effective for learners.

    Visit for a real time training for Java Training in Chennai | Best Java Training Institute in Chennai | Java Master Program Training in Chennai

    ReplyDelete