• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sample experimanting mock code(consturctor related)

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

above code compiles fine.
we know if we are invoking the subclass overloaded constructor,then super-class no arg constructor will be
invoked first, and if super-class no arg constructor is not there, then it will be a compiler error.
if we commented out line2 ,we have to defined superclass no arg constructor(i.e. uncommented out line 1), othervise it
gives a compiler error,what is the logic behind it?

why we don't need no arg. super-class constructor(default) here ,as we are instantiating subclass object(with arg.)?

 
Gari Jain
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arjun

within a constructor the first line should be either a call to this() or super()

and when the compiler doesnt find such a call, it inserts super() by default.

in your code the Main() constructor already has a call to super(arg) and thus super() will not be invoked.
try removing the super(arg) line and you will see that the code will not compile.

Refer to K&B for detailed description about this.(very nicely explained in that book)
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah,thanks garima for clearing my query
 
Gari Jain
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happy to help
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic