Two public classes in one file java

前端 未结 5 751
青春惊慌失措
青春惊慌失措 2020-12-09 17:58

Ok, this might be kiddies question in java. We can\'t define two public classes in one file. But, in one of the examples from the book SCJP study guide, this example was men

5条回答
  •  误落风尘
    2020-12-09 18:31

    Yes you can have two classes in the same file. You can have them by removing the public access modifier from both the class name, like shown below,

    abstract class A{
        public abstract void show(String data);
    }
    
    class B extends A{
        public void show(String data){
            System.out.println("The string data is "+data);
        }
        public static void main(String [] args){
            B b = new B();
            b.show("Some sample string data");
        }
    }
    

提交回复
热议问题