• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Constructors in Inheritance

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Constructors are called in order, base constructor first. So every B object first constructs an A object.
 
Marshal
Posts: 15873
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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.
    Bookmark Topic Watch Topic
  • New Topic