-4

How do I inherit a class in java, when the sub- and super-classes are in different packages

2 Answers2

1

To make a class visible outside of its package, declare it as public.

It can then be extended by classes in other packages (unless it is final, then it cannot be subclassed at all).

Just like with any use of classes outside of the current (and the java) package, you have to import it (or use the fully qualified name my.other.package.ClassName).

Thilo
  • 257,207
  • 101
  • 511
  • 656
0

Just like normal, use the import statement.

import some.folder.SuperClass

public class SubClass extends SuperClass{
    public SubClass(){
        super();
    }
}
Brydenr
  • 798
  • 1
  • 19
  • 30
  • Is `class SubClass extends some.folder.SuperClass` ever used, or is it too cluttery? – RaminS Oct 04 '16 at 23:48
  • 1
    @Gendarme: It is too cluttery. You would use it only if you already have a class of the same name imported, such as `class Factory extends some.api.Factory`. But that should probably be avoided, too. – Thilo Oct 04 '16 at 23:50
  • @Gendarme see http://stackoverflow.com/questions/2079823/importing-two-classes-with-same-name-how-to-handle – Brydenr Oct 04 '16 at 23:53