super-constructor if there is no super class?

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

I found a class like this:

public class Computer implements Serializable {

        private static final long serialVersionUID = 1L;    

        //...
              


        
相关标签:
5条回答
  • 2020-12-11 10:19

    By default all classes inherit java.lang.Object. So a hidden code in your class is

    public class Computer extends java.lang.Object implements Serializable {
    
            private static final long serialVersionUID = 1L;    
    
            //...
            public Computer() {
                super(); //here Object's default constructor is called
            }
            //...
    }
    

    If a parent class has default constructor (no argument) and if a child class defines a default constructor, then an explicit call to parent's constructor is not necessary.

    Java does it implicitly, in other words, Java puts super() before first statement of the child's constructor

    consider this example

    class Base {
    
        public Base() {
            System.out.println("base");
        }
    }
    
    class Derived extends Base {
    
        public Derived() {
            //super(); call is not necessary.. Java code puts it here by default
            System.out.println("derived");
        }
    }
    

    So when you create Derived d = new Derived(); the output is..

    base
    derived
    
    0 讨论(0)
  • 2020-12-11 10:19

    By default every java class implicitly inherits java.lang.Object class. So, super() will call the Object class constructor in your case!

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-11 10:31

    In Java, Object class is at the top of the Class hierarchy. Implicitly, All the Classes are implicitly of Object type. Invocation of super() method is not needed explicitly in code. But, this will be added by the compiler during conversion of Class into bytecode. so, the first line in any constructor would be invocation of super() implicitly by default. Construction of an object includes all the invocation of default constructors of Super classes. But, you can also call a customized argument constructor of the super class in your class constructor.

    0 讨论(0)
  • 2020-12-11 10:39

    When you do not extends any class then by default the inherited class is Object so it calls Object class constructor.

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