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 :-
Very useful post for beginners sir:) Hope you included checking of the class with "if(< > instanceof < >)" it would be more useful
ReplyDeleteIEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. A IEEE Domain project Final Year Projects for CSE system development life cycle is essentially a phased project model that defines the organizational constraints of a large-scale systems project.
DeleteIT Company Employess Productivity usually increases when a company implements corporate training courses on latest technologies.
corporate training in chennai
It Companies need of Corporate training programme arises due to improvement in technology, need for getting better performance or as part of professional development. corporate training companies in chennai Corporate Training refers to a system of professional development activities provided to educate employees.
corporate training companies in india
Great Article
Digital Marketing Company in Chennai
SEO Company in India
Digital Marketing Agency
SEO Services in India
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.
ReplyDeleteThanks a lot very much for the high quality and results-oriented help.
ReplyDeleteI 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
Ciitnoida provides Core and java training institute in
ReplyDeletenoida. 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
very useful
ReplyDeleteThe information you shared is very effective for learners.
ReplyDeleteVisit for a real time training for Java Training in Chennai | Best Java Training Institute in Chennai | Java Master Program Training in Chennai
Thanks for the info. Loved it. java training in chennai
ReplyDeleteThis is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me..
ReplyDeleterpa online training |
rpa course in bangalore |
rpa training in bangalore |
rpa training institute in bangalore
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
ReplyDeleteexcel advanced excel training in bangalore | Devops Training in Chennai
All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
ReplyDeletepython Online training in chennai
python Online training in bangalore
python interview question and answers
The site was so nice, I found out about a lot of great things. I like the way you make your blog posts. Keep up the good work and may you gain success in the long run.
ReplyDeleteOracle training in Chennai
Java training in Chennai | Java training in Annanagar
Java training in Chennai | Java training institute in Chennai | Java course in Chennai
Java training in Chennai | Java training institute in Chennai | Java course in Chennai
After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
ReplyDeleteData Science Training in Chennai | Best Data science Training in Chennai
Data Science training in anna nagar | Data science training in Chennai
Data Science training in chennai | Best Data Science training in chennai
Data science training in Bangalore | Data Science training institute in Bangalore
Data Science training in marathahalli | Data Science training in Bangalore
Data Science interview questions and answers
I am really happy with your blog because your article is very unique and powerful for new reader.
ReplyDeleteClick here:
selenium training in chennai | selenium course in chennai
selenium training in bangalore | selenium course in bangalore
selenium training in Pune | selenium course in pune | selenium class in pune
selenium training in Pune | selenium course in pune | selenium class in pune
selenium online training | selenium training online | online training on selenium
ReplyDeleteGreetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.
AWS Training in Pune | Best Amazon Web Services Training in Pune
Amazon Web Services Training in OMR , Chennai | Best AWS Training in OMR,Chennai
AWS Training in Chennai |Best Amazon Web Services Training in Chennai
I really appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thx again!
ReplyDeleteaws Training in indira nagar
selenium Training in indira nagar
python Training in indira nagar
datascience Training in indira nagar
devops Training in indira nagar
Great content with an excellent idea!!! Thanks for sharing.
ReplyDeleteSelenium Training in Chennai
Best selenium training in chennai
iOS Training in Chennai
.Net coaching centre in chennai
French Classes in Chennai
Big Data Training in Chennai
Qtp training institutes in chennai
Qtp training Chennai
Very interesting content which helps me to get the indepth knowledge about the technology. To know more details about the course visit this website.
ReplyDeleteDigital Marketing Training in Chennai
JAVA Training in Chennai
core Java training in chennai
ReplyDeleteThanks Admin For sharing this massive info with us. it seems you have put more effort to write this blog , I gained more knowledge from your blog. Keep Doing..
Regards,
Cloud Computing Courses in Chennai
Cloud Computing Training in Chennai
RPA Training in Chennai
Ethical Hacking Course in Chennai
Blue Prism Training in Chennai
Cloud Computing Courses in T Nagar
Cloud Computing Courses in Velachery
It was really an interesting blog, Thank you for providing unknown facts.
ReplyDeleteAviation Courses in Chennai
best air hostess training institute in chennai
diploma in airport management in chennai
airport ground staff training courses in chennai
medical coding in Chennai
top 10 fashion designing colleges in chennai
Interior design courses in Chennai
Thanks For Sharing The Information Please Keep Updating us Information Shared Is Very Valuable Time Just Went On Reading The Article Python Online Training AWS Online Training Data Science Online Training Data Science Online Training Hadoop Online Training
ReplyDeleteJust found your post by searching on the Google, I am Impressed and Learned Lot of new thing from your post.
ReplyDeleteJava Training in Chennai | Java Training Institute in Chennai
Great Article. Thank you for sharing! Really an awesome post for every one.
ReplyDeleteProject Centers in Chennai
JavaScript Training in Chennai
Final Year Project Domains for IT
JavaScript Training in Chennai
thank you so much for this nice information Article, Digitahanks for sharing your post with us. digital marketing training in bangalore
ReplyDeleteYour articles really impressed for me,because of all information so nice.servicenow training in bangalore
ReplyDeleteThese provided information was really so nice,thanks for giving that post and the more skills to develop after refer that post.opennebula training in bangalore
ReplyDeleteI gathered a lot of information through this article.Every example is easy to undestandable and explaining the logic easily.openstack training in bangalore
ReplyDeleteVery useful and information content has been shared out here, Thanks for sharing it.salesforce developer training in bangalore
ReplyDeleteThis is really an awesome post, thanks for it. Keep adding more information to this.vmware training in bangalore
ReplyDeleteIt’s really Nice and Meaningful. It’s really cool Blog. You have really helped lots of people who visit Blog and provide them Useful Information. Thanks for Sharing.citrix training in bangalore
ReplyDeleteIt’s really Nice and Meaningful. It’s really cool Blog. You have really helped lots of people who visit Blog and provide them Useful Information. Thanks for Sharing.citrix training in bangalore
ReplyDeletethanks for sharing such an useful stuff...
ReplyDeleteapex course
thank you so much for this nice information Article, Digitahanks for sharing your post with us.WEBMETHODS training in bangalore
ReplyDeleteIt is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful. PEGA PPRC training in bangalore
ReplyDeleteThis is amazing and really inspiring goal.Hadoop Training in Bangalore
ReplyDeleteI have read your blog its very attractive and impressive. I like it your blog.Informatica Training in Bangalore
ReplyDeleteGreat post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.IMSBI Training in Bangalore
ReplyDeleteReally i appreciate the effort you made to share the knowledge. The topic here i found was really effective...
ReplyDeleteSoftgen Infotech is the Best Oracle Training institute located in BTM Layout, Bangalore providing quality training with Realtime Trainers and 100% Job Assistance.
This post is really nice and informative. The explanation given is really comprehensive and informative.
ReplyDeleteamazon aws tutorial
I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously in their life, he/she can earn his living by doing blogging.thank you for thizs article. pega online training , best pega online training ,
ReplyDeletetop pega online training
Effective blog with a lot of information. I just Shared you the link below for Courses .They really provide good level of training and Placement,I just Had Pega Classes in this institute,Just Check This Link You can get it more information about the Pega course.
ReplyDeleteJava training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
You have provided a nice article, Thank you very much for this one. And I hope this will be useful for many people. And I am waiting for your next post keep on updating these kinds of knowledgeable things...
ReplyDeleteOracle Training | Online Course | Certification in chennai | Oracle Training | Online Course | Certification in bangalore | Oracle Training | Online Course | Certification in hyderabad | Oracle Training | Online Course | Certification in pune | Oracle Training | Online Course | Certification in coimbatore
It's really nice and meanful. it's really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull information buy hyip website
ReplyDeleteThe unstructured or semi-structured details require processing in order to make it useful for the present-day entrepreneur environment. data science course syllabus
ReplyDeleteAim2Tech offering you result-oriented and scalable mobile application development services worldwide.
ReplyDeleteMobile App Development Services Indore
Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also.
ReplyDeletedata science course in India
ufabetxxx
ReplyDeleteExcellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
ReplyDeleteArtificial Intelligence Course
Really awesome blog!!! I finally found a great post here.I really enjoyed reading this article. Thanks for sharing valuable information.
ReplyDeleteData Science
Selenium
ETL Testing
AWS
Python Online Classes
Nice Blog, When I was read this blog, I learnt new things & it’s truly have well stuff related to developing technology, Thank you for sharing this blog. Need to learn software testing companies, please share. It is very useful who is looking for smart test automation platform.
ReplyDeleteToo good article,Keep sharing more article posts with us.
ReplyDeleteThank you...
ServiceNow Training