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

Explain the use of shift operator in java. Can you give some examples?

704


How use .contains in java?

849


Explain the difference between arraylist and linkedlist in java?

680


What is diamond operator in java?

689


What is the use of inner class?

729






Difference between ‘>>’ and ‘>>>’ operators in java?

836


Difference between string s= new string (); and string s = "abv";?

949


Is 9 a prime number?

630


Explain about sets?

731


If two threads have same priority which thread will be executed first ?

1096


What is a function easy definition?

710


What does \ mean in regex?

818


Difference between notify() method and notifyall() method in java?

730


What is queue in java?

760


How to change the priority of thread or how to set the priority of thread?

806