Can you have a constructor in abstract class?

Answer Posted / kundan ranjan

ya
you can write constructor in abstract class
becoz,construct are use in abstract class only for initialize the state(variables) of class
you know that all the variable are allowed inside the abstract class
if you not initialize the variable at declaration time then you have need constructor becoz
you have no any alternative method to initialize the state thats why constructor are allowed inside
abstract class
see example:
abstract class hello
{
int x;
abstract void m1();
hello(int x)
{
this.x=x;
System.out.println(x);

}


}
class Hai extends hello
{
Hai(int x)
{
super();
}
void m1()
{
System.out.println("asdf");
}
}
class Lab84
{
public static void main(String as[])
{

hello h=new Hai(12);
h.m1();
h.m2();
}
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How objects are stored in java?

783


What is difference between array and vector?

823


Is a class an object?

795


What is bubble sorting in java?

826


What are the two environment variables that must be set in order to run any java programs?

737


What is the difference between static and non-static variables in java programming?

725


What is jdbc api?

764


what is the purpose of "virtual"?

829


Where are variables stored?

762


What are thread local variables?

787


What is split return?

695


What is the size of boolean variable?

839


What is nested top-level class?

786


What are the different types of collections in java?

747


Is string a class in java?

746