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
How do you remove an element from an arraylist in java?
how does the run() method in runnable work? : Java thread
Why do people says “java is robust”?
What is a private class in java?
How does regex work?
Why call by value prevents parameter value change?
What is treemap in java?
What is java argument list?
How will you compute size of a structure?
Addition to previous section relative word 5th one was Putrid ans: rotten, also there was prob. in 1st section on bucket weight ans:10kg, also there was a prob. on train speed to find bridge length ans:800 mtrs.
How to display all the prime numbers between 1 and 100
What is singleton service?
Can we have more than one package statement in source file ?
What is treeset in java collection?
what is deadlock? : Java thread