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 are void pointers?

714


What is mean by encoding?

627


How do you check if a string contains only numeric digits?

620


Explain the inheritance?

577


Why is a string immutable?

546






What is abstract class? Explain

597


what is server side caching?

1608


When the constructor of a class is invoked?

595


Does string is thread-safe in java?

593


What is the purpose of format function?

628


Can we overload the methods by making them static?

507


What do you mean by boolean?

580


Can string be considered as a keyword?

574


What is the difference between serializable and externalizable interfaces?

569


What is the difference between quicksort & mergesort? When should they be used? What is their running time?

655