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 :-
No comments:
Post a Comment