0

I created a class named A and put this code under this class. I created a subclass named B under the same file.

package com.puneetred.sem3;

class A extends B {
    
    public static void methodB() {
        System.out.println("Second Class");
    }
    
    public static void main(String[] args) {
        methodA();
        methodB();
    }
}


public class B {
     
    public static void methodA() {
        
        System.out.println("First class");
    }

}

Compiling the above code throws this exception:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

    at com.puneetred.sem3/com.puneetred.sem3.B.methodA(A.java:18)
    at com.puneetred.sem3/com.puneetred.sem3.A.main(A.java:10)

But when I remove the public keyword before class B, the code just runs fine.

puneetred
  • 39
  • 3
  • 1
    class B is public, and should be declared in a file named B.java – Piyush Pranjal Sep 20 '22 at 04:25
  • While writing a java program first it is saved as a ".java" file, when it is compiled it forms byte code which is a ".class" file as such that if we made our program file similar to the class it will be comfortable for us to understand without any ambiguity. We are allowed to use any name for a filename only when class is not public. In the case of a public class, we can’t use a different file name. The filename must have the same name as the public class name in that file, which is the way to tell the JVM that this is an entry point. – Piyush Pranjal Sep 20 '22 at 04:27

0 Answers0