Question

today it's about the InetAddress class of java. There are 2 default implementations (actually 3 if you take InetAddressImpl in count): Inet4Address for representing IPv4 addresses and Inet6Address for representing IPv6 Addresses.

Now there are several tunneling protocols. Because I want the software to be type safe, I thought about implementing them as subclasses of InetAddress (InetTeredoAddress or Inet6to4Address for example). The best solution would be to inherit from Inet6Address but because it is final, this option is dropped. The next idea is to create subclasses of InetAddress and delegate every method to it's Inet6Address pendant:

class InetTeredoAddress
    extends InetAddress {

    protected Inet6Address address;

    @Override
    public boolean isMulticastAddress() {
        return address.isMulticastAddress();
    }

    @Override
    public boolean isAnyLocalAddress() {
        return address.isAnyLocalAddress();
    }

    ...
}

This, again, fails because its constructors are only visible in java.net.* . Just for fun, I tried to create a package called java.net and placed all classes inside. This way I was able to compile but when starting the program, (surprise surprise) the following error occurred

org.testng.TestNGException: java.lang.SecurityException: Prohibited package name: java.net

What would be the best way to implement the protocols? Or is a Helper class the only solution?

Thanks in advance

Zota

Was it helpful?

Solution

Forget it, and set about implementing a family of URLConnectionHandlers instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top