Getting To The Java Roots of XPages – Part 14

Adding additional methods to an existing class by extending it is not the only thing that you can do with Extends. You can also override existing methods in the extended class. In our StaffInfo class the getInternetAddress will return a simple internet email address like jbloggs@somecorp.xyz but for your application you need it to look like ‘Joe Bloggs ‘.

Now you COULD create a new method just like the getReversedName() method in the last part but then you would have to go back to the application and change all the calls to getInternetAddress() to your new method name. Instead, however, you can create your own getInternetAddress() method in StaffInfoPlus.

    @Override
    public String getInternetAddress() {
	return getCommonName() + " ";
    }

The @Override is called a compiler annotation. It is not strictly needed and the above would work perfectly without it but it does help the compiler to know that we are overriding a method in the extended class ( and an warning will show if it comes to an @Override on a method that doesn’t exist in the extended class ).

After the annotation is our new version of the getInternetAddress() method. It is returning a string made up of the users common name, a space, an opening angle bracket, the users internet address and then a closing angle bracket.

Look closely at how we get the users internet address. I’m overriding the getInternetAddress() method but I also need to get the users internet address. To do this I use a special keyword called ‘super’. This tells the compiler that I want to call the version of this method in the class that I’m extending and not the version in this class.

So my StaffInfoPlus class has all the methods that exist in the StaffInfo class, plus my reversedName method and now, any call to getInternetAddress() will use the version in this class rather then the StaffInfo class that I’m extending.

This java stuff is not so scary after all…

Advertisements
Tagged with: ,
Posted in None
4 comments on “Getting To The Java Roots of XPages – Part 14
  1. Erich Schmidt says:

    Sweet. I’ve been programming for 20 years — just not Java — but still I appreciate the step by step you’re laying out here. Thanks Declan.

    Like

  2. Timothy Briley says:

    An FYI : All the links at http://blog.dnaware.net/xpage-java-roots/ (except the 1st) point to part 2.

    Like

  3. Brandon Willoughby says:

    Great series! The format is fantastic and easy comprehend. It is a big help for my transition to Java. Thanks Declan.

    Like

  4. Devin Olson says:

    You are doing a great job with this series. Keep up the good work!

    Like

Comments are closed.

Archives
%d bloggers like this: