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
Can we call the run() method instead of start()?
Write a java program to print fibonacci series?
What are loops in java?
Is namespace same as package in java?
What is bufferedreader in java?
Explain thread life cycle in java?
Can java hashmap have duplicate keys?
What is better - 'bit-shift a value' or 'multiply by 2'?
describe synchronization in respect to multithreading? : Java thread
Explain about the performance aspects of core java?
What should I import for arraylist in java?
What are 3 boolean operators?
Explain naming conventions for packages?
What are the differences between heap and stack memory?
What is a ternary operator in java?