• Post Reply Bookmark Topic Watch Topic
  • New Topic

how abstract class constructor invokes while creating instance of Subclass  RSS feed

 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Users not allowed to create instance of Abstract Class while We create the instance of subclass then
How the abstract class constructor calls.

I am having simple code:



child class having default constructor , how compiler creates default constructor for abstract class also.

It is because of what , while same code in interface not allowed.

Is it the nature of both different as Interface is pure Abstract class while Abstract class in partial. This is theoretically correct.

As i am want to know how internally it could be treated by JVM or design level.

We also know every class extends Object class.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code isn't creating an instance of an abstract class. And there's nothing unusual or mysterious about the fact that a subclass's constructor has to invoke a constructor of its superclass, even if the superclass does happen to be abstract. In fact nothing different happens when the superclass is abstract.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not saying it is creating instance but calling the default constructor once we create the instance of subclass. right ?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes because your subclass extends the abstract class. Every class (not interface, including abstract classes) has at least one constructor. In your case there is an implicit call to the superconstructor in base (which is added by the compiler).
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are right if we say about class means we have the constructor of that class like Abstract class
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My point is, there are rules for how constructors work, including how they call constructors of their superclass. And those rules are the same whether any of the classes are abstract or not. In particular abstract classes need constructors just like any other classes. So the answer to your question is "It makes no difference whether the superclass is abstract or not."

It's possible that you don't understand the rules and need to discuss them here. That would be perfectly normal for Beginning Java. But abstract classes don't come into that question.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!