• 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

Constructors in Inheritance

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when a new class extends from another. Is the constructor kept? I was solving a mock exam and it came up with something like:
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructors are called in order, base constructor first. So every B object first constructs an A object.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructors in Java are not inherited although it may appear so when the compiler adds a default no-args constructor into a class that has no explicitly declared constructor. With a little experimentation (see code below) you will quickly see that constructors are not inherited.



Now see if you can figure out why GrandChild2 will not compile.
(Hint: by default, the compiler will insert a call to super() as the first statement in a constructor))

[ July 02, 2004: Message edited by: Junilu Lacar ]
(darned UBB )
[ July 02, 2004: Message edited by: Junilu Lacar ]
 
Jos� Alberto Mu�iz Navarro
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I get it now.. so in order for it to compile I'd have to add a super(i) (where i is an int) in order for it to work =)

Thank you.
reply
    Bookmark Topic Watch Topic
  • New Topic