Reputation: 178
Context: Two classes from different packages (Second class in second package inherits class in first package) are connected through inheritance and made a method call to subclass from parent class.
What I did:
Written two classes in two different notepad files and trying executing one after other but was not possible for me to execute and showing error messages and my classes are as follows:
package first;
import second.Sample1;
public class Sample {
public static void main(String a[])
{
Sample1 s=new Sample1();
s.dis(1);
}
package second;
import first.Sample;
public class Sample1 extends Sample{
public void dis(int i)
{
System.out.println(i);
}
}
In Eclipse, it is giving output as 1 but in what order I should execute these codes using notepads files. Observed that compiling these classes in any order giving error messages.
Thanks much. :)
Upvotes: 2
Views: 757
Reputation: 393781
You created a cyclic package dependency, which is not a good idea.
Your base class Sample
doesn't have to know anything about its sub-classes, and when it does, it is usually a sign of bad design.
Just move the main
method to Sample1
, and Sample
class won't have to import second.Sample1
.
Upvotes: 1