CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2001
    Posts
    1

    typedef in JAVA???

    Does any body know what is the correspondence method in JAVA to typedef in C++?

    For example, it is supposed that L_Handle have the same data type with int. In C++, it can be obtained as follows:

    typedef L_Handle int

    what should i do in JAVA?
    I tried to use "extends" such as

    public class L_Handle extends Integer{}

    however, the above code is not working cos Integer is a final class and thus cannot subclass.

    if anybody knows how, please mail to me.

    thanks in advance


  2. #2
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    264

    Re: typedef in JAVA???

    Hi,

    There is no equivalent for typedef in Java. And subclassing from Integer is a good idea, but indeed, you can't. It's final. You can make a new class:

    [javaclass]
    public class L_Handle {
    int integerValue;
    public L_Handle(int integerValue) {
    this.integerValue = integerValue;
    }
    public int intValue() {
    return integerValue;
    }
    }
    [/javaclass]

    But indeed, this is very poor.

    What can make this a richer class, from an OO standpoint, is that this L_Handle indeed handles L's, whatever an L may be

    Please check out the classes java.net.InetAddress, java.net.URL. Both classes could just have been a subclass of String (if technically possible), and are in fact handles. However, they can do a lot more. E.g. an URL can give you an output stream and do other things.

    The message is: think OO. A handle is not an integer (!isA => not a subclass). One could have chosen a name for a handle too. An L_Handle handles L's, what can an L do for you? Put that functionality in the class above, and you'll see in which way a C programmer thinks differently from a Java programmer. (Note that it would have been better that in C++, the same OO reflexes would have been valid)

    Have a lot of fun with Java,

    Regards,
    Geert Arys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured