Can you have a constructor in abstract class?

Answers were Sorted based on User's Feedback



Can you have a constructor in abstract class?..

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

Can you have a constructor in abstract class?..

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

Can you have a constructor in abstract class?..

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

Can you have a constructor in abstract class?..

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

Can you have a constructor in abstract class?..

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

Can you have a constructor in abstract class?..

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

Can you have a constructor in abstract class?..

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

Can you have a constructor in abstract class?..

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

Can you have a constructor in abstract class?..

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

Can you have a constructor in abstract class?..

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

Post New Answer

More Core Java Interview Questions

What is deadlock and how to avoid this?

2 Answers  


What are the 5 types of research methods?

0 Answers  


What is the benefit of singleton pattern?

0 Answers  


What is the vector class in java programming?

0 Answers  


What is vector capacity in java?

0 Answers  






What is generic class?

0 Answers   Tech Mahindra,


What class allows you to read objects directly from a stream?

0 Answers  


Explain differences between collection api and stream api?

0 Answers  


How are commas used in the initialization and iteration parts of a for statement?

2 Answers  


What is serialversionuid?

0 Answers  


What is casting?

3 Answers   BMC,


How will you convert an ArrayList to Arrays?

4 Answers   KPIT,


Categories