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?
Re: 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?
Re: 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?
It will run fine with no exception here is the example :
public class CheckException extends Thread{
public CheckException(){
super.start();
this.start();
}
@Override
public synchronized void start() {
// TODO Auto-generated method stub
super.start();
}
}
Re: 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?
Re: 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?
The question clearly specifies "what happen if u _CALL_
super and this in the same class"
By call, probably, they mean:
this(); /*Invoking the constructor*/
super(); /*Invoking the parent class's constructor*/
I think the right answer is that it will raise a compiler
exception. Such a program will not compile.
Re: 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?
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
Re: 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?
Given:
10. interface A { void x(); }
11. class B implements A { public void x() { } public
voidy() { } }
12. class C extends B { public void x() {} }
And:
20. java.util.List list = new java.util.ArrayList();
21. list.add(new B());
22. list.add(new C());
23. for (A a:list) {
24. a.x();
25. a.y();;
26. }
What is the result?
1 Compilation fails because of an error in
line 25.
2 The code runs with no output.
3 An exception is thrown at runtime.
4 Compilation fails because of an error in
line 20.