Re: How to make a class an alias of another one?



Mark Space wrote:
public abstract class ClassA {
// your methods here

public static ClassA getInstance() {
return new Temporary()
}
}

class Temporary extends ClassA {
private String temp;

// implement ClassA methods

}

Now you can declare everything as ClassA and switch implementations when you need easily. Note the lack of "public" on Temporary. It should be in the same package as ClassA.

This gives the appearance of an antipattern: one should not hardcode subclasses into their parent classes.

What safety there is in this use lies in that 'Temporary' is package-private. This isn't completely safe, in that other subclasses of 'A' can do Bad Things.

--
Lew
.



Relevant Pages

  • Re: How to make a class an alias of another one?
    ... public static ClassA getInstance() { ... in the same package as ClassA. ... in that other subclasses of 'A' can do Bad Things. ...
    (comp.lang.java.programmer)
  • Re: abstract class question about operators
    ... public abstract class ClassA ... public static ClassA operator + ... public class ClassB: ClassA ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to make a class an alias of another one?
    ... I am planning to implement a class (say ClassA) in some special ... public static ClassA getInstance() { ... // implement ClassA methods ... Now you can declare everything as ClassA and switch implementations when you need easily. ...
    (comp.lang.java.programmer)
  • abstract class question about operators
    ... public abstract class ClassA ... public static ClassA operator + ... public class ClassB: ClassA ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Help with modules/packages.
    ... __init__.py makes the dir a package. ... "import CJB.ClassA" imports MODULE ClassA from PACKAGE CJB into the ... Ok now onto the imports. ...
    (comp.lang.python)