Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interface, generic, subclass - A design question

677253Jul 29 2008 — edited Jul 30 2008
Hello everyone,

Assume the following :
public class A { }

public class B extends A { }
public class C extends A { }
Now I have interface and base impl
public interface IFacade {
  public void method(A a);
}

public class FacadeBaseImpl implements IFacade {
  public void method(A a) {
    return;
}
Further down I have a concrete classes that know how to deal with B, C:
public class FacadeBImpl extends FacadeBaseImpl {
...
here I would like to implement method in such a way that I would be able to pass to it only B and subclasses of B.

I've tried to do this with Generic methods and it does work, I tried to use lower bounds of generics (extends) on the interface and in FacadeBImpl to define the method with the final class and it does work either. I know I can simply cast inside my method to B, and it works. I would on the other hand prefer something that could catch the error at compile time.

Anyone has any ideas? I would appreciate a tip or two.

Thank you,
Maxim.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 27 2008
Added on Jul 29 2008
10 comments
110 views