How to Typecast in Java 6

In computer programming, typecasting refers to taking an object of one type and converting it into an object of another type. In Java 6, this usually, but not always, involves either upcasting or downcasting, which means to cast an object up or down the inheritance hierarchy.

Instructions

    • 1

      Paste the following Java classes into a blank text file. These are the class that will be used for the tutorial:

      public class Person { }

      public class MalePerson extends Person { }

    • 2

      Paste the following upcast:

      Person p = new MalePerson("Tom");

      The MalePerson class is a subclass of Person, so this is a cast up the hierarchy. Upcasting is the easiest type of cast to pull off. Java simply knows what to do when a subclass is assigned to one of its ancestors. Make a mental note, however: Java will always remember what an object really is during an upcast. Even now that the MalePerson "Tom" has been assigned to a generic person object, Java remembers that this object is really a MalePerson.

    • 3

      Paste the following downcast:

      Person p = new MalePerson("Tom");

      MalePerson m = (MalePerson) p;

      Downcasts like this, that move down the hierarchy, require the programmer to specify the class type being used for the cast in parenthesis. However, downcasting is more complex than upcasting.

    • 4

      Attempt the following downcast:

      Person p = new Person("Jane");

      MalePerson m = (MalePerson) p;

      This attempt fails, and it shouldn't be hard to see why. While it is reasonable to assume that a MalePerson is a Person, it is not reasonable for the computer to assume that all Persons are MalePersons. A few may be FemalePersons, and the difference may or may not be trivial for the computer's purposes. Just to be safe, the program throws an error.

      A downcast can only occur if an upcast has occurred first, which is why the upcast in Step 3 was valid, but not in Step 4. This may make upcasting sound useless at first. It begs the question of why the upcast was performed in the first place, but hold that thought.

    • 5

      Consider the following method:

      public void greet(Person p) {

      if (p instanceof MalePerson) {

      MalePerson m = (MalePerson) p;

      m.doMaleThings();

      } else if (p instanceof FemalePerson) {

      FemalePerson f = (MalePerson) p;

      f.doFemaleThings();

      } else throw new GenderConfusionException();

      }

      The writer of this method has no way of knowing in advance whether this method will be called with a MalePerson or a FemalePerson object, so he uses the generic Person object. Then, he uses the instanceof command to poll the Person given and see whether he should downcast it as a male or a female person.

Related Searches:

References

Comments

You May Also Like

  • How to Convert Java Objects

    The Java programming language was unique when it was first released in the mid 1990s in that the language was designed to...

  • What Is Casting in Java?

    In Java, casting refers to the act of treating an object or method of a certain class like an object of a...

  • How to Use Javascript With Flash

    There are many Web-programming situations that call for information to be passed between client-side Javascripts and embedded Flash content. Before the release...

  • Java 1.4 Vs. Java 1.5

    Java 1.5, otherwise known as Java 5.0, is the next to latest release of the Java programming language and virtual machine, the...

  • How to Learn Java 6

    Java is used world wide and takes several months to master. Subsequently, Java programmers are well paid and tend to enjoy stable...

  • How to Type

    Mastering the keyboard isn't difficult if you go about it methodically and practice a little every day. There are different techniques to...

  • How to Download Java 1.6 for Mac

    Java 2 Platform, or Java Standard Edition, is a developer tool for using the Java computer language. Programmers use this language to...

  • How to Use Assignment Operators in Java

    Type your Java variable so that the left-hand variable is the same type as the right-hand expression. Otherwise, there must be an...

  • Java to Convert Integers to Characters

    In the Java programming language, a whole number can be stored in a variable of the integer data type. This type of...

  • How to Update a Java Version in PATH

    Sun's Java Programming Language allows a developer to write an application once and deploy it on any system running the appropriate version...

  • Java Tutorial on the Random Method

    Randomly generated numbers have many uses in computer programs, such as creating unpredictability in games, modeling simulations and performing encryption. Java ...

  • How to Convert INT to String in Java

    Converting an integer to a string is a common practice when programming. For some application processes, it's necessary to manipulate the format....

  • Message Types in JMS

    Message Types in JMS. The Java Message Service API is a Message Oriented Middleware, or MOM, application-programming interface for sending messages between...

  • How to Input & Output in Java

    Java programs can take the form of simple utilities that work off the command line, or complex graphical user interfaces. While skilled...

  • How to Install Java 6.11 Silently

    The Java Runtime Environment is required for applications written in the Java programming language to function. All Java installation packages support silent...

  • How to Compile Java Programming Code

    Java is a programming language that is widely used for desktop, mobile and Internet development. When writing a Java program, you first...

  • Yearly Salary For Programmers

    Being able to tinker around on a keyboard is one thing, but being able to write the step-by-step instructions that direct computers...

Related Ads

Featured