Reputation: 1
I wanna compile java file but dont know how to name the file, since it has 2 public classes, where 1 class inherits from another. I named the file with name of Superclass, however cmd is giving me an error saying that "subclass is public, should be declared in file named Subclass.java". Can anybody help me to resolve this? thank you
public class Superclass {
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}
public class Subclass extends Superclass {
// overrides printMethod in Superclass
public void printMethod() {
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void main(String[] args) {
Subclass s = new Subclass();
s.printMethod();
}
}
Upvotes: 0
Views: 1888
Reputation: 7194
Either use separate file for both classes or remove public access specifier
from Superclass
. In java ,in same file you can have only one public
class.
And the class containing main
method should be declared public
.
Upvotes: 0
Reputation: 753
both classes should be in separate files. i.e. Subclass in Subclass.java and Superclass in Superclass.java
Upvotes: 1