• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Do abstract classes instantiated, so what about super()

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,
As it's known to all that abstract classes can't instantiated, but if we have constructor in abstract class(as we can have) and subclassed it and call the super() frm subclass constructor, then what happens?
sandy
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
abstract classes can't be instantiated DIRECTLY. But you may have some member variables in your abstract class that you need to be instantiated correctly, so you may provide a constructor for that abstract class to achieve such instantiation. When subclassing abstract classes, you should call the appropriate constructor of the abstract superclass to execute proper intialization of that superclass.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
sandeep bhatnagar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well thanx again valentin. but as we can have constructor in abstract class to correctly instantiated the var. of this class so why we can't do this directly. it mankes some confusion in my mind. plz give me a favor
thanx
sandy
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


why we can't do this directly


what do you mean by that ??
remember that some member variables in the abstract superclass may be private and thus are not accessible in the subclass to be instantiated !!
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A constructor doesn't instantiate a class. It's new who does it.
If new were invoked on an abstract class the compiler complains.
If new is called on a proper class it finds out the space in the heap needed to create an object of that class. It clears the space for its instance fields to zero and push the reference "address of the object in the heap" into the operand stack. On this reference a specific constructor is called. Now the process of instance fields initalization (from top to bottom in the hierarquy) takes place.
So whe calling new on a subclass of an abstract class an instance of the subclass is created, and the base constructor "only" does its job: to initialize instance fields declared in the base class after the returning of the super(...) call that made the same for instance fields in supertypes.
hope helps.
 
sandeep bhatnagar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx a lot guys for ur kind support.
sandy
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic