Solved

Overriding Constructors

Posted on 2003-11-18
10
2,511 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

863 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

22 Experts available now in Live!

Get 1:1 Help Now