Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
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: 5852
57
Eclipse IDE Firefox Browser Java MySQL Database VI Editor 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
Java Netbeans IDE 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: 13478
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • 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: 164
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: 64494
225
  • 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.
     
    Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
    how do I do my own kindle-like thing - without amazon
    https://coderanch.com/t/711421/engineering/kindle-amazon
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!