in a constructor what happen if u call super and this in
the same class? i know that it is not possible to call
both in the same one? if we call what will happen?
Answer Posted / madhu
We can't give both super() and this()in a constructor,
because both of these statements must be the first
statements in constructor. if you give super() as the first
statement then compiler error will come with this() call.
and vice versa.
but you can call the members with both super and this in a
constructor. here is the code.
class Base
{
Base()
{
System.out.println("Base constructor");
}
void m1()
{
System.out.println("m1 of Base");
}
}
class Derived extends Base
{
Derived()
{
super();//it is ok.
this();//raises compiler error
//but we can call the members with super and this
super().m1();//make sure that super()in
line must be marked comment
this().m1();
System.out.println("Derived constructor");
}
void m1()
{
System.out.println("m1 of Derived");
}
public static void main(String[] args)
{
Derived d=new Derived();
}
}
it works out
| Is This Answer Correct ? | 9 Yes | 3 No |
Post New Answer View All Answers
Explain yield() method in thread class ?
What is java util concurrentmodificationexception?
what are abstract functions?
When is an object subject to garbage collection?
How will you add panel to a frame?
How many types of array are there?
Can we override the overloaded method?
What is a hashmap used for?
I want to re-reach and use an object once it has been garbage collected. How it's possible?
What is the relationship between class and object?
How does java pattern compile work?
Which class represents the socket that both the client and server use to communicate with each other?
Explain about field hiding in java?
Does google use java?
What is lazy initialization in java?