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

Can you extend main method in java?

713


Can a constructor be protected?

627


What are the methods to rectify ambiguities in the interfaces in JAVA?

706


What is int short for?

624


What are the Main functions of Java?

711






These static constructors are correct ? class A { statc intA() { } static A(int x,int y) { } static A(int x) { } }

681


What is a boolean flag in java?

656


What are methods in java?

615


Why static functions are used?

657


Write a program to calculate factorial in java?

664


Explain about instanceof operator in java?

716


What is r * in math?

622


Write a program to print 15 random numbers using foreach of java 8?

632


What is the use of put method?

622


How do you trim a space in java?

598