Solved

Overriding Constructors

Posted on 2003-11-18
10
2,532 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 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
Technology Partners: 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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

688 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