| Author |
why constructer for abstract class?
|
santhosh.R gowda
Ranch Hand
Joined: Apr 06, 2009
Posts: 296
|
|
As we know abstract classes can never be instantiated .then what is the use of building the constructer by the compiler during copilation of abstract class
for example if i declare my abstract class
when i decompile my class file..
please clear my doubt
|
Creativity is nothing but Breaking Rules
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8441
|
|
|
Constructors in abstract classes are typically used, when you want all your subclasses to share some data. In your example all subclasses will inherit 'i'
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
santhosh.R gowda
Ranch Hand
Joined: Apr 06, 2009
Posts: 296
|
|
Constructors in abstract classes are typically used, when you want all your subclasses to share some data.
Please explain me in detail.....
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8441
|
|
|
Which part of my statement did you not understand?
|
 |
santhosh.R gowda
Ranch Hand
Joined: Apr 06, 2009
Posts: 296
|
|
why compiler generates constructer for abstract class what is the need of constructer for abstract class
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Every class needs at least one constructor, for constructor chaining. If a class (abstract or not) does not have a constructor sub classes cannot call it. Therefore, you would not be able to sub class the class at all. In fact, this is exactly what happens if you only provide a private constructor.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8441
|
|
|
I do not think the "decompiled" version posted by the OP is the constructor generated by the compiler. The compiler will generate a default constructor which is no args andempty
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
Decompilers are hardly a beginners' topic. Moving.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Maneesh Godbole wrote:I do not think the "decompiled" version posted by the OP is the constructor generated by the compiler.
Yes . i too agree .
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Surprisingly, it is. Apparently the compiler puts all initializations that occur when declaring in source code, into all constructors. For instance:
As you can see, the initialization of x to 10 is moved to both constructors.
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8441
|
|
Interesting.
What do you get if you do not provide any constructors at all? Your existing code has two.
PS. I am not being lazy, but I do not have any decompile tools setup on my home laptop ;)
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
It provides a default constructor with the initializer as its body:
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8441
|
|
Thanks Rob.
I think its time I dusted off my core java and got down to basics again
|
 |
 |
|
|
subject: why constructer for abstract class?
|
|
|