0
class Animal {
    Animal(){
        super();
    }
}

class Bird extends Animal {
    Bird(String name){
        System.out.println(name);
    }
    
    Bird(){
        System.out.println("Unknown");
    }
}
public class WhizSuperKeyWord {

    public static void main(String[] args) {
        new Bird("Parrot");

    }

}

Hi, this is my first post. I am learning Core Java. Can you please help me understand what's the use of super() method in parent class. I understand super() is used in Child class to invoke constructor of Parent class. I am puzzled what is super() method doing in Parent class. this() is used in Parent class to invoke constructor of current class. Many thanks.

desiboy
  • 3
  • 2
  • 1
    In your base class constructor `super()` is unnecessary. You can omit it. Perhaps someone included it because they are in the habit of *always* calling a super-constructor. – khelwood Jul 29 '20 at 21:33

1 Answers1

2

It is pointless. All objects in Java inherit from the Object class. Java automatically inserts an implicit super() call in constructors of classes with no explicit superclass, so the call to super() in Animal is pointless.

Hari Menon
  • 33,649
  • 14
  • 85
  • 108
  • I've actually never seen my IDE (neither Eclipse nor IntelliJ) adding a call to `super()` when I create a constructor for a class which does not `extends`. With "Java automatically inserts", do you mean it does that in bytecode? – Matteo NNZ Jul 29 '20 at 21:55
  • Yes, it will be present in the bytecode – Hari Menon Jul 30 '20 at 05:31