AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
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 ?.



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
profile Garima Gupta - anyforum.in Garima Gupta
596129554550
up-rate
4
down-rate

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
profile Saksham Kumar - anyforum.in Saksham Kumar
73433936114
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in