| Author |
Inheritance through package
|
Howard Ting
Greenhorn
Joined: Feb 17, 2004
Posts: 19
|
|
Suppose I have a directory C:\Java with a subdirectory pkgA. Now I code two JAVA files Test1.java and Test2.java, the former in C:\Java and the latter in C:\Java\pkgA. The codes are below: //Test1.java in C:\Java import pkgA.*; public class Test1 { public static void main(String[] args) { Test3 test = new Test3(); test.method1(); test.method2(); } class Test3 extends Test2 { void method1() { //some statements } } class Test2 { } //Test2.java in C:\Java\pkgA package pkgA; public class Test2 { protected void method2() { //some statements } } The problem I was describing is "How to inherit a class in some package while another class with the same name of the class is to be inherited is in the directory with the one is to inherit?" (Sorry for mouthful...) If I get the codes above compile, the complier complains that method2() can't be found. That's because what Test3 inherits is Test2 in C:\Java, not that in C:\Java\pkgA. But since there's a method1() with the default scope (or purview? I don't know what it's excatly called) that can only be invoked using "." in the same package, I can't move it to C:\Java\pkgA. In fact, I should just give Test2 in C:\Java a different name, but is there some way of solving this problem without changing the name of any class or moviing them? Any reply would be appreciated.
|
 |
C. Nimo
Ranch Hand
Joined: Mar 23, 2004
Posts: 82
|
|
ola. Why not use Test3 extends pkgA.Test2 ? The fully qualified class name should work. Nimo.
|
 |
Howard Ting
Greenhorn
Joined: Feb 17, 2004
Posts: 19
|
|
Your answer is excatly the one I expected! You know what? I just went back checking my original code and found the biggest mistake - I wrote "pkaA"! The wrong spelling resulted in compile error but I didn't notice it and presumed I can't code it this way, so I posted my question omitting this idea. Thanks for your help~
|
 |
 |
|
|
subject: Inheritance through package
|
|
|