File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Inheritance through package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance through package" Watch "Inheritance through package" New topic

Inheritance through package

Howard Ting

Joined: Feb 17, 2004
Posts: 19
Suppose I have a directory C:\Java with a subdirectory pkgA. Now I code two JAVA files and, the former in C:\Java and the latter in C:\Java\pkgA. The codes are below:
// in C:\Java
import pkgA.*;
public class Test1 {
public static void main(String[] args) {
Test3 test = new Test3();
class Test3 extends Test2 {
void method1() {
//some statements
class Test2 {
// 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
Why not use Test3 extends pkgA.Test2 ?
The fully qualified class name should work.
Howard Ting

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~
I agree. Here's the link:
subject: Inheritance through package
It's not a secret anymore!