aspose file tools*
The moose likes Beginning Java and the fly likes Constructors in Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructors in Inheritance " Watch "Constructors in Inheritance " New topic
Author

Constructors in Inheritance

Jos� Alberto Mu�iz Navarro
Ranch Hand

Joined: Oct 27, 2003
Posts: 33
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:
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Constructors are called in order, base constructor first. So every B object first constructs an A object.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

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 ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Jos� Alberto Mu�iz Navarro
Ranch Hand

Joined: Oct 27, 2003
Posts: 33
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors in Inheritance