Blog Java OCAJP – Casting in Java

OCAJP – Casting in Java

This post is about the following OCAJP Java certification exam objectives:

  1. (7.3) Differentiate between the type of a reference and the type of an object.
  2. (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.

OCAJP 8 Free Test

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:
    1. Casting an object from a sub class to a super class doesn’t require an explicit cast.
    2. Casting an object from a super class to a sub class requires an explicit cast.
    3. The compiler will not allow casts to unrelated types.
    4. 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.

  1. Casting an object from a sub class to a super class doesn’t require an explicit cast.
  2. Casting an object from a super class to a sub class requires an explicit cast.
  3. The compiler will not allow casts to unrelated types.
  4. 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.

Practice Tests

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.

About Amit Verma

Amit is an impassioned technology writer. He always inspires technologists with his innovative thinking and practical approach. A go-to personality for every Technical problem, no doubt, the chief problem-solver!
Spread the love

4 COMMENTS

  1. 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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here