Can you have a constructor in abstract class?
Answers were Sorted based on User's Feedback
Answer / sumit
But you can create constructor in an abstract class.
and whenever you careate object of class implementing
abstarct class, constructor of te abstract class runs.
Is This Answer Correct ? | 108 Yes | 13 No |
Answer / rajesh dangi
I don't understand why ppl start writing responses without
writing a small code to confirm it.
Guys an abstract class too can have a constructer. Any time
a class inheriting an abstract class is instantiated, its
constructer would first call the constructor of the
abstract class. Try it yourself.
Plus, the method in the abstract class with the same name
(i.e. its constructer) is always considered as a
constructer and not any regular method. Even when the
constructer is public, you can not invoke the constructer
of an abstract class in a sub class inheriting the abstract
class. That method can not be invoked by our code since it
is an abstract class and can not be instantiated by us
Is This Answer Correct ? | 62 Yes | 6 No |
Answer / sam
Yes
abstract class test{
int i=10;
abstract void method();
test(){
System.out.println("Abstract class constructor");
}
}
public class test1 extends test{
void method(){
//implement abstract method of test
}
public static void main(String args[]){
test1 t=new test1();
}
}
Is This Answer Correct ? | 29 Yes | 2 No |
Answer / jag bhushan
yes,
we can have constructor in abstract class.
But we can not make instance of the abstract class.
instead we can make a reference to that abstract class.
and when we make a new object of the class which extends
the abstract class, the constructor of abstract class get
called.
see the code for example:
public abstract class TestAbstract {
TestAbstract(){
System.out.println("...in abstract class'
constructor");
}
public abstract void showAbstract();
public void show(){
System.out.println("...in show");
}
}
public class Test extends TestAbstract{
public static void main(String[] args) {
TestAbstract ta = new Test(); //
constructor call
ta.showAbstract();
ta.show();
}
public void showAbstract() {
System.out.println("...in showAbstract");
}
}
Is This Answer Correct ? | 22 Yes | 3 No |
Answer / ranjan
We can write a method name same as class which is called
constructor in the abstract class , but as a abstract class
can not be instanciated so that method used as a general
method.
but no error would be there...
Is This Answer Correct ? | 31 Yes | 16 No |
Answer / dileep
Yes you can have a constructor in abstract class but you
should not instansiated directly you have to write one
subclass extends the abstract class.
Is This Answer Correct ? | 12 Yes | 1 No |
Answer / abdul hannan
Yes. We can have constructor in an abstract class but we can
not directly instantiate it. If the abstract class have no
argument constructor, it will automatically call by the
constructor of subclass through constructor chaining. If
abstract class have constructor with argument, then we need
to call it by super() and pass the argument in it.
Is This Answer Correct ? | 5 Yes | 0 No |
Answer / srinu
Yes constructor their abstract class.if u any doubt araise
just see HttpServlet Api. HttpServlet contain constructor
Is This Answer Correct ? | 6 Yes | 2 No |
Answer / vinoth kumar
Yes,
we can define it in the abstract class itself.But it invoke
only by super() in sub class constructor.
Example:
abstract class AbsConstCheck{
AbsConstCheck(){
System.out.println("I AM WORKING AbsConstCheck");
}//some other methods declaration
}
class Sub extends AbsConstCheck{
sub(){
super();//calling abstract class constructor
System.out.println("I AM WORKING Sub");
}
}
class Main{
public static void main(String vin[]){
Sub s=new Sub();//calling sub,abstract class constructor
}
}
Output:
I AM WORKING AbsConstCheck
I AM WORKING Sub
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / 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 |
What is a Null object?
Explain what access modifiers can be used for methods?
If a method is declared as protected, where may the method be accessed in java programming?
Q1.A. Write note on “The class path Environment Variable”? B. Which are different kinds of source code? Q2.A. How to create an interface? B. Why convert an applet to an application? Q3.A. How to use Media tracker Class. B. How to use string tokenizer class. Q4 A. Explain the overview of UDP messaging. B. Difference between SQL Exception class and SQL Warning class. Q5. A. How to create com object in Java? B. Write short notes on “The properties class” Q6. A. When object is created and destroyed? B. Explain the JDB in depth & command line. C. Write short notes on Web Sites.
2 Answers iGate, Seed Infotech,
Why is it important to initialize a variable?
all are saying java doesn't support multiple inheritance but by default Object class is super class for all the user defined classes and we can extend atmost one class so each class can extend more than one class so java supports multiple inheritance?i am confused with this,pls any one explain me.
what is type of statement in jdbc connection?
What is the most important feature of java? What is an interface?
Explain the difference between arraylist and linkedlist in java?
What is a generic type?
What should I import for arraylist in java?
How to create two different thread class inside a main function?