Home arrow Java arrow Page 4 - What`s New in Java 1.5 Tiger?
JAVA

What`s New in Java 1.5 Tiger?


Call it Java 1.5, 2.0, Java 5, Tiger, or what have you -- this version of Java has a lot to offer. This article covers just some of the new features. It is excerpted from chapter one of Java 1.5 Tiger: A Developer's Notebook, written by Brett McLaughlin and David Flanagan (O'Reilly, 2004; ISBN: 0596007388).

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 13
May 19, 2005
TABLE OF CONTENTS:
  1. · What`s New in Java 1.5 Tiger?
  2. · Using Queues
  3. · Ordering Queues Using Comparators
  4. · Overriding Return Types
  5. · Taking Advantage of Better Unicode
  6. · Adding StringBuilder to the Mix

print this article
SEARCH DEVARTICLES

What`s New in Java 1.5 Tiger? - Overriding Return Types
(Page 4 of 6 )

One of the most annoying features when youíre using Java inheritance is the inability to override return types. This is most commonly desired when youíve got a base class, and then a subclass adds a dimension (either literally or figuratively) to the base class. Typically, youíre unable to return that extra dimension without defining a new method (and new name), since the method that the base class used probably had a narrower return type. Thankfully, you can solve this problem using Tiger.

How do I do that?

Example 1-4 is a simple class hierarchy that demonstrates overriding the return type of a superclassís method.

Example 1-4Overriding the methods of a superclass

  class Point2D {
   
protected int x, y;
    public Point2D() {
     
this.x=0;
     
this.y=0;
   
}
   
public Point2D(int x, int y) {
      this.x = x;
      this.y = y;
    }
  }
 
class Point3D extends Point2D {
    protected int z;
   
public Point3D(int x, int y) {
      this(x, y, 0);
    }
   
public Point3D(int x, int y, int z) {
      this.x = x;
      this.y = y;
      this.z = z;
    }
  }
 
class Position2D {
    Point2D location;
   
public Position2D() {
      this.location = new Point2D();
    }
   
public Position2D(int x, int y) {
      this.location = new Point2D(x, y);
    }
   
public Point2D getLocation() {
      return location;
    }
  }
  
class Position3D extends Position2D {
    Point3D location;
   
public Position3D(int x, int y, int z) {
      this.location = new Point3D(x, y, z);
    }
    
public Point3D getLocation() {
      return location;
    }
  }

The key is the line public Point3D getLocation(), which probably looks pretty odd to you, but get used to it. This is called a covariant return, and is only allowed if the return type of the subclass is an extension of the return type of the superclass. In this case, this is satisfied by Point3D extending Point2D. Itís accomplished through the annotation, covered in detail in Chapter 6.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials