• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

how abstract class constructor invokes while creating instance of Subclass

 
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 27227
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27227
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic