?
Solved

Overriding Constructors

Posted on 2003-11-18
10
Medium Priority
?
2,538 Views
Last Modified: 2012-08-13
Hi there..

can you help me with my question..

Can we override constructors..and If yes how?

thanks,,
0
Comment
Question by:SEstudent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 2

Assisted Solution

by:Tols
Tols earned 160 total points
ID: 9769604
Yes:
public class First
{
 public First()
{
}
}
public class Second extends First
{
public Second() //overriding constructor
{
    super(); //posibble call of superconstructor
}
}
0
 

Author Comment

by:SEstudent
ID: 9769612

thanks..

but shouldn't overrided methods have the same?
0
 

Author Comment

by:SEstudent
ID: 9769628

sorry..

but shouldn't overrided methods have the same name?
0
Industry Leaders: 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!

 
LVL 35

Expert Comment

by:girionis
ID: 9769661
>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
 

Author Comment

by:SEstudent
ID: 9769735

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

but should overriding constructors have the same name too..

0
 
LVL 35

Accepted Solution

by:
girionis earned 240 total points
ID: 9769745
 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
 
LVL 2

Expert Comment

by:Tols
ID: 9769846
;-)

A little notice: superconstructor (superclass' constructor) in derived class' contructor must be called (if it is) before any other initialization.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9769879
 Yes true. You first call the superclass' constructor and then you do all the rest :)
0
 

Author Comment

by:SEstudent
ID: 9769956
thanks alot..

I was just confused with naming :)
0
 
LVL 35

Expert Comment

by:girionis
ID: 9769960
 Thank you. Glad we helped :)
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question