Override a constructor class

后端 未结 2 615
梦谈多话
梦谈多话 2021-01-29 01:23

Below is my code. I am not getting what is the mistake. Can anyone be able to guide.

class State {
    static String country;
    static String capital;

    Sta         


        
相关标签:
2条回答
  • 2021-01-29 01:35
    class State 
    {
        static String country;
        static String capital;
    
    
        State()     //Constructor
        {
            country = "America's";
            capital = "Washington D.C";
    
        }
    
        static void display()
        {
            System.out.println(capital + " " + "is" + " " + country + " " +"capital." );
    
        }
    }
    

    Main class

    class Place extends State // Inheritance 
        static void display()
        {
            System.out.println("Capital is Washington D.C.");
        }
        public static void main(String[] args)
        {
    
            State st = new State();
            st.display(); // to print sub class method 
            display(); // to print same class method 
            //st = pl; No idea of this point ..
    
        }
    }
    
    0 讨论(0)
  • 2021-01-29 01:50

    Your Place class needs to be defined as public.

    Edit: The file also must be named Place.java

    0 讨论(0)
提交回复
热议问题