• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sample experimanting mock code(consturctor related)

 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)?

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah,thanks garima for clearing my query
 
Gari Jain
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Happy to help:)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic