This post is about the following OCAJP Java certification exam goals:
(7.3) Differentiate the type of a reference from the type of an item.
7.4) Decide when casting is required.
These two objectives would test your knowledge of type casting. You will be able to answer all questions confidently if you have a good understanding of type hierarchy, down casting (casting sub classes to super classes) and up casting (casting super classes to sub classes).
The OCAJP exam will not test your knowledge of Java casting. Which is implicit casting or explicit casting? However, the exam will only test your knowledge on object casting.
You will be asked to insert, or choose the correct casting option. Which members are available when you cast one type to the next?
You can read the Java version history if you are unsure about Java’s popularity. If you are ready to start a Java career or prepare for the OCAJP exam please take our free OCAJP 8 mock test questions.
Casting in Java
Casting is the act of making a variable behave like a variable of another type. Casting variables to another type is possible if a class has an IS-A relationship or inheritance relationship with another interface or class. Sometimes casting is allowed, and sometimes it is not. Some cast members will not make errors at compile time but may fail at run-time. These tricky scenarios will be covered in the certification questions.
Here are some guidelines to remember when casting variables. Casting an object from a sub-class to a superclass doesn’t require an explicit casting.
Casting an object from super class to sub class requires explicit casting
The compiler won’t allow casting to unrelated types.
Even if the code compiles fine, an exception could be thrown at the run-time if the object being thrown is not an instance of that particular class. This will cause the run-time exception ClassCastException.
Important is the third rule. The compiler may try to trick the exam by allowing a cast.
Example:
Public class OcpPublic class OcaPublic static void main[String[]args]] Oca oa = Oca() new Oca(); Ocp (Ocp.oa) = Ocp.oa; // DOESNT COMPILE In this example Ocp is not related to Oca through any class hierarchy the compiler is aware. The code will not compile.
Although two classes may share a similar hierarchy, this doesn’t mean that an instance of one class can be automatically cast to another.
Example:
Public class Ocp. Public class Oca extends Ocp. Ocp Op = new Ocp(). Oca oa = Oca (Oca)op. // Throws classcastexception during runtime. This code creates Ocp and then attempts to cast it to Oca, a subclass Ocp.
This code will compile fine, but it will throw a ClassCastException during runtime because the object being referenced does not belong to the Oca class.
Keep in mind that the object created in this example is not related to Oca in any way.
Casting Notes OCAJP
Here are the rules for casting.
An explicit cast is not required to cast an object from a sub-class to a superclass.
Casting an object from super class to sub class requires explicit casting
The compiler won’t allow casting to unrelated types.
Even if the code compiles fine, an exception could be thrown at the run-time if the object being thrown is not an instance of that particular class. This will cause the run-time exception ClassCastException.
Conclusion
Remember what instance you are using when reviewing questions in the exam that involve casting and polymorphism. Next, you should focus on whether the compiler allows the object to reference with or without explicit castings.
Refer to
We have published a few helpful articles to help you prepare for the OCAJP Exams. For effective preparation, please read the following articles.
How to prepare for the OCAJP Exam
What books are good for the OCAJP 8 Certification Exam Exam?
What books are good for the OCAJP 7 Certification Exam Exam?
We are available to help you with Java certification preparation and purchasing the OCAJP Certification Kit. Please send us a mail, or call our support.