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 do you sort in descending order in java using collections sort?

709


What is variable in java?

724


Which is easier netbeans or eclipse?

814


Is an object null?

735


What are inner classes or non static nested classes in java?

924


What is the arraylist in java?

744


Does collectionutils isempty check for null?

1117


What is advantage of java?

765


Give reasons supporting that string is immutable.

711


What are the 6 functions?

723


Why main function is static?

898


What is use of functional interface in java 8? Explain

773


What are the two categories of data types in the java programming language?

729


What is the main purpose of java?

754


What is lazy programming?

771