super-constructor if there is no super class?

后端 未结 5 962
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 09:42

I found a class like this:

public class Computer implements Serializable {

        private static final long serialVersionUID = 1L;    

        //...
              


        
5条回答
  •  猫巷女王i
    2020-12-11 10:24

    By default any class even if we don't extend any super class, extends java.lang.Object class. So it calls Object constructor.

    By the inheritance concept, we can have following code compiled properly.

    Object obj = new Computer();
    

    Even, in fact, your interface reference can be assigned to java.lang.Object reference.

    interface ICommand 
    {
    
    }
    class Computer implements ICommand
    {
    
    }
    class Test
    {
        public static void main(String[] arg)
        {
            ICommand ic = new Computer();
            Object obj = ic;  // 'ic' is an interface reference
        }
    }
    

    by this way, java.lang.Object is also super interface (this is not exact technical term ) for all interfaces.

提交回复
热议问题