Solved

Overriding Constructors

Posted on 2003-11-18
10
2,515 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 90
Java Timer (static) 9 43
Java Options in Zimbra Server 2 54
servlet example 11 40
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

772 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