Solved

Overriding Constructors

Posted on 2003-11-18
10
2,506 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
  • 4
  • 4
  • 2
10 Comments
 
LVL 2

Assisted Solution

by:Tols
Tols earned 40 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 35

Accepted Solution

by:
girionis earned 60 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now