• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Package

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody explain why the statement "package packageB;" must be there before SubclassB can compile.

// Filename SuperclassA.java
package packageA;
public class SuperclassA {

public int superclassVarA;

public void superclassMethodA() {
}

}

class SubclassA extends SuperclassA {

void subclassMethodA() {
superclassVarA = 10;
}

}

class AnyClassA {

SuperclassA obj = new SuperclassA();

void anyClassMethodA() {
obj.superclassMethodA();
}

}
=============================================================
// Filename SublcassB.java
// Without this statement, SubclassB.java could not compile
// Error: file does not contain class SuperclassA
package packageB;
import packageA.*;
public class SubclassB extends SuperclassA {

void subclassMethodB() {
superclassMethodA();
}

}

class AnyClassB {

SuperclassA obj = new SuperclassA();

void anyClassMethodB() {
obj.superclassVarA = 20;
}

}
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you putting these files? In other words, what's the folder structure? This may mean that you physically have SubclassB class in the packageB folder. So without putting the right package, a compile error would occur.
[ October 24, 2003: Message edited by: Eric Sexton ]
[ October 24, 2003: Message edited by: Eric Sexton ]
 
Ron Lam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Eric,
 
Ron Lam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Eric,
I have put the source files in c:\temp and the generated class files in c:\temp\packageA and c:\temp\packageB respectively.
But, what is the reason for putting SubclassB in a packageB (or it may be other package) for it to be successfully compiled.
Regards.
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic