Override a constructor class

后端 未结 2 614
梦谈多话
梦谈多话 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 ..
    
        }
    }
    

提交回复
热议问题