• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance through package

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ola.
Why not use Test3 extends pkgA.Test2 ?
The fully qualified class name should work.
Nimo.
 
Howard Ting
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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~
 
Seriously? That's what you're going with? I prefer this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic