This post is about the following OCAJP Java certification exam objectives:
- (7.3) Differentiate between the type of a reference and the type of an object.
- (7.4) Determine when casting is necessary.
The above two objectives in the Java certification exam would test your knowledge on type casting. If you want to be more confident on answering all the questions, then you should have good understanding on type hierarchy, down casting (casting a sub class to super class) and up casting (casting a super class to sub class).
Note that OCAJP exam would not check your knowledge on the casting in Java. Which is casting of primitive types either implicit or explicit casting. But, the exam will test your knowledge only on the Object casting.
You will be asked to insert or select some correct casting option, Which members are accessible when you cast one type to another type.
If you have any doubt about the popularity of Java, you can go through the Java version history. And if you are all set for a Java career and preparing for the OCAJP exam, please try our free OCAJP 8 mock exam questions.
Casting in Java
- Casting is the process of making a variable behaves as a variable of another type. If a class shares an IS-A or inheritance relationship with another class or interface, their variables can be cast to each other’s type. Some times the cast is allowed and some times that cast is not allowed. Because, some of the cast will not show any error at compile time, but will fail at run time. Those tricky scenarios will be asked in the certification questions.
- Here are some basic rules to keep in mind when casting variables:
- Casting an object from a sub class to a super class doesn’t require an explicit cast.
- Casting an object from a super class to a sub class requires an explicit cast.
- The compiler will not allow casts to unrelated types.
- Even when the code compiles without issue, an exception may be thrown at run time if the object being cast is not actually an instance of that class. This will result in the run time exception ClassCastException.
The third rule is important. The exam may try to trick you with a cast that the compiler doesn’t allow.
Example :
Public class Ocp {} Public class Oca { Public static void main(String[] args) { Oca oa = new Oca(); Ocp op = (Ocp)oa; // DOES NOT COMPILE } }
- In this example, the classes Ocp and Oca are not related through any class hierarchy that the compiler is aware of. Therefore , the code will not compile.
- Even though two classes share a related hierarchy, that doesn’t mean an instance of one can automatically be cast to another.
Example:
Public class Ocp {} Public class Oca extends Ocp { Public static void main(String[] args) { Ocp op = new Ocp(); Oca oa = (Oca)op; // Throws classcastexception at runtime } }
- This code creates an instance of Ocp and then tries to cast it to a subclass of Ocp , Oca.
- Although this code will compile without issue, it will throw a ClassCastException at runtime since the object being referenced is not an instance of the Oca class.
- The thing to keep in mind in this example is the object that was created is not related to the Oca class in any way.
OCAJP Casting Notes
Here is the important rules that you should remember for the casting.
- Casting an object from a sub class to a super class doesn’t require an explicit cast.
- Casting an object from a super class to a sub class requires an explicit cast.
- The compiler will not allow casts to unrelated types.
- Even when the code compiles without issue, an exception may be thrown at run time if the object being cast is not actually an instance of that class. This will result in the run time exception ClassCastException.
Conclusion
When reviewing a question in the exam that involves casting and polymorphism, be sure to remember what the instance of the object. Then, focus on whether the compiler will allow the object to be referenced with or without explicit casts.
References
We have published few useful articles for preparing OCAJP Exams. Please read the below articles for effective preparation.
- How to prepare for OCAJP Exam?
- What are the good books for OCAJP 8 Certification Exam?
- What are the good books for OCAJP 7 Certification Exam?
If you are looking for any support for Java certification preparation or purchasing our OCAJP Certification Kit, please send us a mail or call to our support.
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021
Very interesting analysis. Great information. Since last week, I am gathering details about JAVA Experience . There are some amazing details on your blog which I didn’t know.i want to ask you some thing.
Does JavaScript include other frameworks like NodeJs or AngularJs ? Same for Java, does it include Android? And PHP ? I hope there would be some overlap, even though it is marginal. Thoughts?
please Reply. Thanks.
Thank you for such an informative and clearly written article.
I totally understand how to use these now.
Such a very useful article. Very interesting to read this.I would like to thank you for the efforts you had made for writing this awesome article.
Thank you very informative and helpful.