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

What is meant by object?

583


What is quick sort in java?

565


How a variable is stored in memory?

512


What does microservices mean?

539


What are the types of literals?

573






Explain the difference between extends thread vs implements runnable in java?

543


What is string subsequence method?

637


How does hashmap work in java ?

547


why are wait(), notify() and notifyall() methods defined in the object class? : Java thread

559


What is main difference between variable and constant?

538


Explain an intermediate language?

516


What is the scope or life time of instance variables?

664


How many types of parsers are there?

559


What is variable explain?

530


What is the best definition for data?

526