This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can you please clarify my doubt ?

 
siva chaitanya
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i write this code as below



I am getting compile time error: constructor superclass in class superclass
cannot be applied to given types;
{
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
1 error

Actually when we create object s with new keyword and constructor, In that constructor we are passing parameter then only parameterized constructor is invoked no argument constructor is not invoked then why i am getting this error.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A subclass constructor always ends up calling a superclass constructor. You can call it explicitly (like you have on line 16), but if you don't then the compiler will insert a call to the no-arg superclass constructor. Which means that your code is effectively the same as this:
Now can you see the problem? There is no no-arg constructor in the superclass! The compiler will only insert a no-arg constructor if you don't provide any constructor yourself, but that's not the case here.

(By the way, I'd suggest getting used to following Java conventions, which means that all your class names ought to start with a capital).
 
siva chaitanya
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:A subclass constructor always ends up calling a superclass constructor. You can call it explicitly (like you have on line 16), but if you don't then the compiler will insert a call to the no-arg superclass constructor. Which means that your code is effectively the same as this:
Now can you see the problem? There is no no-arg constructor in the superclass! The compiler will only insert a no-arg constructor if you don't provide any constructor yourself, but that's not the case here.

(By the way, I'd suggest getting used to following Java conventions, which means that all your class names ought to start with a capital).


Thank you Matthew for your reply i know that compiler places super keyword in no-arg constructor but subclass no-arg constructor is not called right because we are passing value 10 with an object then subclass parameterized constructor is called and from there control passes to superclass parameterized constructor
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler doesn't just have to worry about how you happen to be using the class in this case. It will make sure that the class can be used in all cases. As things stand, you've added a constructor that can never be called without an error. The compiler knows this must be a mistake, so flags it up.
 
Ankush Kaundal
Ranch Hand
Posts: 36
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructors are called in such a way that the parent constructor is called first and then the flow ends up in calling the child constructor and since you have one constructor in a child class that is non parametrized so the compiler will look for the constructor of the superclass which is also non parametrized and since it is not there so the compiler will throw an error. The solution is either you explicitly call the parametrized constructor of superclass just above line no.12 or define a no argument constructor in the superclass.

Note: Although you are calling the parametrized constructor only but the compiler will check for other combinations also.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic