1.5 generic question

From: Chris (sarge_chris_at_hotmail.com)
Date: 12/17/03


Date: 17 Dec 2003 03:19:14 -0800

Has anyone had a play with generics in the JDK1.5 beta-release and/or
knows the answer to this:

If I have the following:

// immutable
interface Address
{
    String getStreet() { ... }
    ...
}

// mutable
class MutableAddress implements Address
{
    String getStreet() { ... }
    void setStreet( String street ) { ... }
}

then using generics I could define this:

    private List<MutableAddress> list;

but could I 'down-cast' this list to do the following:

    List<Address> getList() { return list; }

i.e. the client will only get the immutable base-class.

Any ideas?

- sarge



Relevant Pages

  • Re: how to find the type of an object represented by an interface
    ... Simplifying, generics are strictly linked to compilation, not runtime. ... template parameter T must implement particular interface i.e. IDisposable: ... Readonly property MaximumRange() as Integer ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: .NET 2.0 to be launched on Nov 8
    ... Generics in .NET don't need interfaces to work, ... >> and probably Delphi for Win32 RSN. ... > - I also wonder if you can parameterise a template with an interface. ...
    (borland.public.delphi.non-technical)
  • RE: Generics in .NET 2.0 SUCK
    ... The reason Generics in .NET 2.0 SUCK? ... when you specify a type parameter without ... probably there is a way to specify a method constraint for T ... > My second thought was that there might be some interface like IArithmetic. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Casting Generic Classes - Possible Solution
    ... It leaves me with the open option to use generics or not when I ... This works as saying "accept a store of anything that are Record ... public class Record { ... So I tried adding an interface into the structure: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Java Generics: limitations?
    ... cannot go around and do something similar to what I've said, then Java ... generics cannot be called generics, ... have pointed out, the HasXY interface is not necessary, and could be ... public int getX(); ...
    (comp.lang.java.programmer)