• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need clarification on what this code is doing

 
Raghavendra Desoju
Ranch Hand
Posts: 95
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Above code throws below compiler error when NO ARGUMENT constructor is not present:

==================================================
error: constructor Mammal in class Mammal cannot be applied to given types;
public Playtypus() {
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
1 error
==================================================

I am confused why code is throwing compiler error even though we're not making an object of child class (Platypus). Can someone clarify please?

Thanks, Raghu
 
Liutauras Vilda
Bartender
Pie
Posts: 2812
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raghavendra Desoju,

Thanks for a nicely presented question (nice subject, used code tags, shown compiler errors).

So, what happens when you extend class.
1. Compiler implicitly adds default constructor call of Base class if you don't call it in extended class. That constructor call looks like "super()".
2. When you remove no argument constructor from your Base class, compiler gives an error, because, as I mentioned, in extended class default constructor call "super()" being added by compiler, and this no longer exist in your Base class.
3. It would solve problem, if in Playtypus class you explicitly would add a call of Base class constructor with the correct list and type of arguments. Example:
Keep in mind, "super" call must be a first statement in your constructor, otherwise you'd get compiler error too.
 
Liutauras Vilda
Bartender
Pie
Posts: 2812
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavendra Desoju,

Actually you deserved a cow for a nicely presented question.
Lets see what insights other guys could add in answering your question from the other point of view.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavendra Desoju wrote:I am confused why code is throwing compiler error even though we're not making an object of child class (Platypus). Can someone clarify please?

Of course we can clarify it for you! And it appears I already explained the exact same issue to you in this topic The explanation why the code doesn't compile is exactly the same, although the code snippet in the linked topic uses Elephant and here it's using Playtypus.

Why this code fails to compile even if you didn't create an instance of the child class? That's very easy! Let's start with a very, very, very important rule: The compiler doesn't execute any code! So every compiler error you get, is because the compiler knows something is wrong without executing any line of code.

With that rule in mind, the compiler knows you will never be able to create a Playtypus instance, because an appropriate call to the constructor of the parent class is missing. The compiler knows that's a definite error and fails to compile the class. The compiler doesn't care if you create 0 Playtypus instances or 1 million.
Now let's think about it a little bit more. Assume the compiler needs to verify the number of created Playtypus instances: if it's zero, then compilation succeeds; otherwise compilation fails. The compiler probably can verify any line of (Java) code on your computer. But what if both classes are part of a very popular animal library used by thousand Java developers and you change the Playtypus class. How can the compiler verify if no Playtypus instances are created by any of these developers using this amazing animal library? That's simply mission impossible (even Ethan Hunt would not be able to pull that one off )! And that's why the compiler doesn't care at all about executing code.

Hope it helps!
Kind regards,
Roel
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yet another cool question by op
Liutauras's explanation has itself cleared the doubt
But then comes Roel(MI lover like me ) had made the concept more clear,in his own way

I understood every part of Roel's explanation, but was wondering whether Roel will Care to elaborate further by posting code snippets for what he explained in the last portion(problem ,compiler will face if it has to verify the statements)
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't we make a rule like this
"If you are not explicitly calling extended class's constructor,in the constructor of extending class or neither using "this()" to refer any other constructor which might has explicit call to super constructor(considering presence of several constructors ,so if in any constructor we call super class's constructor and refer that constructor by "this()",then again IT will work) (This explicit call , if it does exist in extending class's constructor, it should has an argument list same as the parameter list of super constructor)
Then you should always add a no argument constructor in super class"
 
Campbell Ritchie
Sheriff
Posts: 51468
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There already are rules in the JLS.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlike you,every one don't understand Jls that easily.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:but was wondering whether Roel will Care to elaborate further by posting code snippets for what he explained in the last portion(problem ,compiler will face if it has to verify the statements)

I would if I knew which code snippets you are looking for. Because the problem I described is about the compiler needing to know about the code of other developers in order to compile your code successfully.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:Can't we make a rule like this
"If you are not explicitly calling extended class's constructor,in the constructor of extending class or neither using "this()" to refer any other constructor which might has explicit call to super constructor(considering presence of several constructors ,so if in any constructor we call super class's constructor and refer that constructor by "this()",then again IT will work) (This explicit call , if it does exist in extending class's constructor, it should has an argument list same as the parameter list of super constructor)
Then you should always add a no argument constructor in super class"

Let's see if I can make this rule more concise and easier to understand:
  • If you add an explicit call to this() (another constructor of this class) or super() (a constructor of its parent class), then the parameter list must match (just like when you invoke a method); otherwise you'll get a compiler error
  • if you don't add an explicit call to this() nor to super(), the compiler will add a call to super(). So the parent class must have a no-arg constructor, otherwise you'll get a compiler error as well
  •  
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    As you had said how will compiler know if no developer had created instance of Platypus by using Animal library.

    If other developers do that then it should have error in their respective code.Why is it showing while op is not creating any instance of Platypus
     
    Roel De Nijs
    Sheriff
    Posts: 10666
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sachin Tripathi wrote:If other developers do that then it should have error in their respective code.Why is it showing while op is not creating any instance of Platypus

    How would you feel if I create an awesome library and promote this library by saying "you can create as many Platypus instances as you want". So you (being a true Platypus lover) download my library and you want to create 1000 Platypus instances. But with the first one, you'll get a compiler error in your code by using my library. Now you are completely stuck, because your code doesn't compile. And you can't fix it, because you only have the class files. So you have to file a bug report against my framework and hope I'll be able to fix it asap so you can create your 1000 lovely animals. But you are really unlucky, I'm on a holiday for 3 months, so you'll have to wait for at least 3 months to finish your program. Not really developer-friendly, isn't it?

    PS. With such a bug my library would be anything but awesome
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Got you,yeah it is simplified
    Thanks
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    By library you mean interface?
     
    Roel De Nijs
    Sheriff
    Posts: 10666
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sachin Tripathi wrote:By library you mean interface?

    If you are referring to an application programming interface (API), then yes; otherwise no.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic