Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, to use the "super" keyword, do I have to import the target class?

When, in a constructor, we use the super keyword, do we have to import the class the super refers to (when super doesn't refer to Object)?

class A extends ... {
    A() {
        super(); // do we need to import the class super refers to?
    }
}
like image 941
John Assymptoth Avatar asked Nov 28 '25 20:11

John Assymptoth


2 Answers

Yes, because it is in the extends clause.

The super() itself requires no imports, but for it to make sense you need a superclass. You don't need to import it, of course, if it is from java.lang

like image 135
Bozho Avatar answered Nov 30 '25 10:11

Bozho


You do need to import the super class if it is not in the same package or it is in java.lang. If the base class is not available, super() doesn't work anyway.

like image 28
fastcodejava Avatar answered Nov 30 '25 10:11

fastcodejava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!