• Post Reply Bookmark Topic Watch Topic
  • New Topic

Every class including Abstract Class must have a Constructor, is this Statement True?  RSS feed

 
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across this statement 'Every class including Abstract Class must have a Constructor'. Is this statement true, because as i know abstract class dont have body we cant create instance from abstract class.Please advise.
 
Rancher
Posts: 989
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default constructor is still inserted (with an implicit call to super). Constructors are useful in abstract classes because they can still be called from the concrete sub classes to initialize state defined in the abstract class.
 
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:I came across this statement 'Every class including Abstract Class must have a Constructor'.
Is this statement true, because as i know abstract class dont have body we cant create instance from abstract class.Please advise.

first thing -"not MUST it MAY if is needed , actually when you do not call SuperClassConstructor() explicitly it will automatiaclly be called before the SubClass constructor runs (first line super() would be there) , that mean constructor with no-argument (does not matter provided by you or by JVM ).
the main motive of using abstract class was that there may be possibility for some implementation will have been left (but not mandatory) you are just flexible for that abstraction, if you do not want just don't .

Vishal if I guess you are confused with abstract method which have no body, not abstract class . right ?
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnk you E Armitage your post cleared my doubt.

Hi Pramod, I found your post confusing can you please elaborate.
 
Pramod Kumar Pandey
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal tell me , what the confusion still you have, because once you said your doubt is cleared ... again you said you are confused. so please ...
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote: . . . 'Every class including Abstract Class must have a Constructor'. Is this statement true, . . .
Yes, it is true, but it is not a syntax rule. What it means is that writing a class without writing its constructor is a bad design.

Yes, you need constructors for abstract classes. Especially if you have to initialise fields in the constructor.Comment out the Vehicle() constructor and it won't even compile!
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I came around this program, and had one doubt as per the screenshot for stack why Animal Constructor is not calling Object Class's Constructor.Is it some book error or I am missed something.

Animal Constructor with no Parameter should call random... method along with super() right?
Animal.PNG
[Thumbnail for Animal.PNG]
Animal.java
Stack.PNG
[Thumbnail for Stack.PNG]
Stack
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't post screenshots, which can be difficult to read. Please also tell us where you found that program.
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please don't post screenshots, which can be difficult to read. Please also tell us where you found that program.


I found it from SCJP6 book pg 141
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the stack diagram(second digram) the third record that is the Animal Constructor should also call Object right?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote: . . . I found it from SCJP6 book pg 141
Thank you.

The details of calling constructors are described in the Java Language Specification. I hope I gave you the correct link. Maybe §15.9.4.
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Vishal Hegde wrote: . . . I found it from SCJP6 book pg 141
Thank you.

The details of calling constructors are described in the Java Language Specification. I hope I gave you the correct link. Maybe §15.9.4.


On further reading I got it this() and super() cannot be used together where this() refers to the next Constructor within the same class while super() indicated constructor of a superclass
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote: . . . this() and super() cannot be used together . . .
Both have to be the first line of the constructor and you can't have two first lines.
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Vishal Hegde wrote: . . . this() and super() cannot be used together . . .
Both have to be the first line of the constructor and you can't have two first lines.


Sorry Campbell didnot understood your sentence.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this(...); must be the first line of the constructor. Write a constructor with this(...); in.
super(...); must be the first line of the constructor. Write a constructor with super(...); in
Now write a constructor with both this(...); and super(...);
Now tell me which is the first line

Does that help?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!