Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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?

Answers were Sorted based on User's Feedback



in a constructor what happen if u call super and this in the same class? i know that it is not po..

Answer / 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

in a constructor what happen if u call super and this in the same class? i know that it is not po..

Answer / srikant bhat

it wil giv an exception -compiler exception

Is This Answer Correct ?    9 Yes 4 No

in a constructor what happen if u call super and this in the same class? i know that it is not po..

Answer / agile being

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.

Is This Answer Correct ?    3 Yes 0 No

in a constructor what happen if u call super and this in the same class? i know that it is not po..

Answer / srikant bhat

wont there be any stack overflow?

Is This Answer Correct ?    2 Yes 0 No

in a constructor what happen if u call super and this in the same class? i know that it is not po..

Answer / srisanjana

No it doesn't work I have just tested

Is This Answer Correct ?    1 Yes 0 No

in a constructor what happen if u call super and this in the same class? i know that it is not po..

Answer / george

super and this will use first line of constructor so we cant
use it...i ll show error

Is This Answer Correct ?    1 Yes 0 No

in a constructor what happen if u call super and this in the same class? i know that it is not po..

Answer / raja

It Works fine with out any Exceptions,because in java each
and every class is inherited from the object class indirectly...

public class consex
{
public consex(String a)
{
super();

System.out.println(a);
}
public static void main(String dd[])
{

consex c=new consex("raja");

}
}

Is This Answer Correct ?    1 Yes 1 No

in a constructor what happen if u call super and this in the same class? i know that it is not po..

Answer / jyotshna pardhia

In a constructor if we call super and this in
the same class than we will get compile time error saying
that call to this() must be the first statement of the
construtor OR call to super() must be the first statement
of the constructor.So it is not possible to call
both in the same one.

Ex:-1
class Test
{
Test()
{
super();
this();
systm.out.println("hello Jyotshna");
}
}
error:- call to this() must be the first statement of
constructor.

Ex:-2
class Test
{
Test()
{
this();
super();
systm.out.println("hello Jyotshna");
}
}
error:- call to super() must be the first statement of
constructor.


Thanks & Regards
jyotshna

Is This Answer Correct ?    1 Yes 1 No

in a constructor what happen if u call super and this in the same class? i know that it is not po..

Answer / atul

class A
{
public A()
{ super();//this is perfect no error
super();//but this call generate RunTimeException
//because inside constructor super called
//must be in first called ok thats it
}
public static void main(String args[])
{
A a1=new A();

}
}

Is This Answer Correct ?    0 Yes 0 No

in a constructor what happen if u call super and this in the same class? i know that it is not po..

Answer / ajay dhingra

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();
}
}

Is This Answer Correct ?    3 Yes 7 No

Post New Answer

More Core Java Interview Questions

Class c implements interface I containing method m1 and m2 declarations. Class c has provided implementation for method m2. Can I create an object of class c?

0 Answers  


What is regex in java?

0 Answers  


import java.io.*; class Demo { public static void main(String args[]) { File f=new File("1234.msg"); String arr[]=f.list(); System.out.println(arr.length); } }

3 Answers   IBM, Ramco,


what is the use of abstract class and interface with example?

2 Answers   Cycore, DNS, Technoram,


what is a static block?

4 Answers  


Which of the classes will have more memory allocated?

0 Answers  


Give reasons supporting that string is immutable.

0 Answers  


Explain what pure virtual function is?

0 Answers   Aricent,


What design pattern you have used in your project? I answered Factory pattern, how it is implemented? What are its advantage? Do know about Abstract Factory?

0 Answers   Bravura Solutions,


How do you allocate memory to object?

0 Answers  


What class allows you to read objects directly from a stream?

0 Answers  


Is a case study a method or methodology?

0 Answers  


Categories