AF
Home
Tag
Submit Notes
Ask Anything
Login
Subscribe Us
A
ny
F
orum
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.
Follow @anyforumin
corejava-OOPs: Can we override the constructors?
Is there any way to implement the constructor overriding in java?
corejava x 353
OOPs x 49
Posted On :
2014-01-06 15:47:10.0
Garima Gupta
596
1295
54550
4
Answers
Constructors can´t be overridden. They´re not inherited. However, each subclass constructor has to chain either to another constructor within the subclass or to a constructor in the superclass.
A constructor can´t be overridden in similar class because both of them would have similar signatures and compiler error would be there.
for example:
public class Superclass
{
public Superclass(int x) {}
public Superclass(String y) {}
}
public class Subclass extends Superclass
{
public Subclass()
{
super(5); // chain to Superclass(int) constructor
}
}
The implication of constructors not being inherited is that you can´t do this:
// Invalid
Subclass x = new Subclass("hello");
Posted On :
2014-01-06 23:00:25
Satisfied :
1 Yes
0 No
Saksham Kumar
734
339
36114
Reply This Thread
5
Post Answer
Please Login First to Post Answer:
Login
Answer:
anyforum.in