+ 2

We can't creat object for abstract class but inside abstract class we can create constructor what is the use of this constructor

what is use of abstract class constructor ?

29th May 2017, 6:09 PM
Rama krishna
Rama krishna - avatar
10 Réponses
+ 1
The use of the constructor of an abstract class is the same for the use of a constructor in a non-abstract class. It's still going to be called, you don't need to create the object for the constructor to be useful. You use a constructor to initialize anything necessary upon creation of the object. The abstract class can contain default members for all sub classes. Review the use of an abstract class / why use one. Here's a good explanation on abstract classes in general: http://www.javacoffeebreak.com/faq/faq0084.html
29th May 2017, 7:31 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
When creating an object of a sub class the base class constructor is always invoked. Consider the following: abstract class a{ a(){ System.out.println("A called"); } } class b extends a{ b(){ System.out.println("B called"); } } public class program{ public static void main(String args[]){ b objB = new b(); } } Output: A called B called You can also call the base class constructor with the super keyword. super();
29th May 2017, 6:27 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Thanks @faith, the link is very pretty cleared explanation. Thanks for your explanation also.👍
30th May 2017, 12:31 AM
Rama krishna
Rama krishna - avatar
+ 2
@Abhishek "Hence you can't have abstract class constructors" What do you mean by this? You can. You just have to have a constructor that has no arguments so the super() can be called. Or did I read that wrong? Edit: I'm wrong to say you have to have a no argument constructor. Disregard that, for anyone reading. Although, You can still have many abstract constructors.
31st May 2017, 2:49 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
That's a default constructor. For any class if you do not create a constructor the compiler creates one for you. Sorry, what I meant to say before is if you made a constructor with arguments, you would also need to make one without arguments. Yup, never mind. You don't have to have a no argument constructor if you call it explicitly, you're right about that. Though, you can still have abstract constructors.
31st May 2017, 2:54 AM
Rrestoring faith
Rrestoring faith - avatar
+ 1
but we can't create object of abstract class right, so what is the use of constructor in abstract class?even though normal class also do the same but what special in abstract class?
29th May 2017, 6:42 PM
Rama krishna
Rama krishna - avatar
+ 1
an abstract class is intended to be a superclass and wenever objects of its subclasses are created then super() is the first statement in the subclass constructor which is inserted by the compiler if u don't explicitly call super() with or without arguments.. hence u can't have abstract constructors even in abstract classes
31st May 2017, 2:33 AM
Abhishek Mehandiratta
Abhishek Mehandiratta - avatar
+ 1
BTW even if u don't supply ur constructor in abstract class compiler can still resolve a call to super()
31st May 2017, 2:52 AM
Abhishek Mehandiratta
Abhishek Mehandiratta - avatar
+ 1
this case wud be wen u dont explicitly call super() with arguments in subclass constructor
31st May 2017, 2:57 AM
Abhishek Mehandiratta
Abhishek Mehandiratta - avatar
0
now correct I guess @faith
31st May 2017, 2:50 AM
Abhishek Mehandiratta
Abhishek Mehandiratta - avatar