• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Why adding super() in subclass class constructor doesn't produce an error?

 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I learned from a book mentioning, "Interfaces do not have constructors". But if that's the case then why there isn't a run-time error, while adding super() in the subclass constructor?



Also, I learned that whenever there is an instantiation of a subclass, then implicitly compiler will add super() in the subclass's default constructor which in turn invokes the parent class constructor. So in this case too why didn't it produce a runtime error even?
Also, there's a compile-time error when I declare a constructor for the interface.
Could anyone help me understand this ?
 
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't run across that one. My guess is that all classes at some level inherit from the Object class though it is implied, not explicit. So super() may be calling Object's constructor.
 
Ranch Hand
Posts: 186
1
Netbeans IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Akshayyha Krishnamurthy wrote: I learned that whenever there is an instantiation of a subclass, then implicitly compiler will add super() in the subclass's default constructor which in turn invokes the parent class constructor. So in this case too why didn't it produce a runtime error even?
Also, there's a compile-time error when I declare a constructor for the interface.
Could anyone help me understand this ?



It is because you are calling from Object (the Parent or Superclass to call classes).

Moderator edit: please quote only relevant parts, not the entire post.
 
Sheriff
Posts: 13685
227
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP, you are confusing inheriting from/extending a *class* versus implementing an *interface*.

What you call a "SuperClass" is misleadingly named because it is *not*, in fact, a class. If you look at the code carefully, you can see that it is an *interface*.

Your so-called SubClass is not a subclass of SuperClass, it is a subclass of java.lang.Object. Hence, the super() statement invokes the sole, no-argument constructor of the Object class.
 
Ranch Hand
Posts: 201
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be a bit more specific - maybe it helps when we extend the source to what the compiler does implicit:

As someone already noted: calling your interface something with "class" in its name is not a good idea: it is an interface, not a class.
 
Marshal
Posts: 65383
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . all classes at some level inherit from the Object class though it is implied, . . . .

All classes which don't explicitly include a superclass declaration “class Foo extends Bar” impliclty have “extends Object” imputed by the compiler, as MW said, so that is correct. All inheritance heirarchies will eventually lead back to Object. In which case, yes, an explicit or implicit super(); will call Obect().

Akshayyha Krishnamurthy wrote:. . . then implicitly compiler will add super() in the subclass's default constructor which in turn invokes the parent class constructor. . . .

You appear to have misread three things in that quote.
  • Don't say default constructor; some classes don't have default constructors. A default constructor is added by the compiler to every class which doesn't have a compilerconstructor written by the programmer.
  • The compiler automatically adds super(); as the first line of every constructor which doesn't already say super(...); or this(...);. That includes default constructors.
  • Don't say parent class; say superclass. Maybe the best term for a superclass is base class, but they only say that in C# They also call subclasses derived classes in C#.
  • Read about default constructors in the Java® Language Specification. That link shows an example of the meaning of a default constructor.

    Edit: correct error somebody pointed out: compiler should read constructor.
     
    We find this kind of rampant individuality very disturbing. But not this tiny ad:
    professionally read, modify and write PDF files from Java
    https://products.aspose.com/pdf/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!