• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2544
  • Last Modified:

Overriding Constructors

Hi there..

can you help me with my question..

Can we override constructors..and If yes how?

thanks,,
0
SEstudent
Asked:
SEstudent
  • 4
  • 4
  • 2
2 Solutions
 
TolsCommented:
Yes:
public class First
{
 public First()
{
}
}
public class Second extends First
{
public Second() //overriding constructor
{
    super(); //posibble call of superconstructor
}
}
0
 
SEstudentAuthor Commented:

thanks..

but shouldn't overrided methods have the same?
0
 
SEstudentAuthor Commented:

sorry..

but shouldn't overrided methods have the same name?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
girionisCommented:
>but shouldn't overrided methods have the same name?

  Yes. In Tols example you do not override methods you "override" a constructor (if we can use the word override here, depends on semantics).
0
 
SEstudentAuthor Commented:

yes..I know that overriding methods should have the same name..

but should overriding constructors have the same name too..

0
 
girionisCommented:
 No, you can't do it since a constructor needs to have the same name as the class. You can't have a constructor "First" for a class "Second".

  You just implement the functionallity you want in the subclass' constructur and then you call the superclass' constructor.
0
 
TolsCommented:
;-)

A little notice: superconstructor (superclass' constructor) in derived class' contructor must be called (if it is) before any other initialization.
0
 
girionisCommented:
 Yes true. You first call the superclass' constructor and then you do all the rest :)
0
 
SEstudentAuthor Commented:
thanks alot..

I was just confused with naming :)
0
 
girionisCommented:
 Thank you. Glad we helped :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now