Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Can i have a private constructor in an abstract class

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

I have been asked a question in an interview that :- What is the use of having a private constructor in an abstract class
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ragi singh wrote:
I have been asked a question in an interview that :- What is the use of having a private constructor in an abstract class




I would be interested to know how you answered the question.

Henry
 
Henry Wong
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anyway, to answer this question. Sure, it is perfectly valid to have a private constructor in an abstract class. I can think of a few ways that it can be invoked. For example, there is another constructor in the abstract class that calls this one. Or there is a static factory method that can be invoked -- that creates an anonymous inner class. Or there is actually a nested concrete class that inherits from the abstract class.

Henry
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry , i made the concrete subclass invoke the super classes overloaded constructor from which we can call this() . and finally create an instance of the sub class . But they were asking as to where it can be used in real time scenario .
 
Henry Wong
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ragi singh wrote:Henry , i made the concrete subclass invoke the super classes overloaded constructor from which we can call this() . and finally create an instance of the sub class . But they were asking as to where it can be used in real time scenario .



First, I am assuming that you mean "real world" scenario, and not "real time" scenario -- two very different meanings. And the answer that you gave is fine. It is perfectly valid to have many constructors, including private ones that get called from the non-private ones, for any class, including abstract classes.

Henry
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Anyway, to answer this question. Sure, it is perfectly valid to have a private constructor in an abstract class. I can think of a few ways that it can be invoked. For example, there is another constructor in the abstract class that calls this one. Or there is a static factory method that can be invoked -- that creates an anonymous inner class. Or there is actually a nested concrete class that inherits from the abstract class.

Henry



Why can we have constructors for abstract classes? Don't constructors allocate memory to an object at the time of instantiation? We know that abstract classes cannot be instantiated, isn't that true?
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Anchit
it it true that we cannot instantiate abstract classes
but the abstract class constructor is called when a subclass of abstract class is instantiated
this is because constructor has implicit call to super constructor
 
Anchit Herredia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Kharkar wrote:@Anchit
it it true that we cannot instantiate abstract classes
but the abstract class constructor is called when a subclass of abstract class is instantiated
this is because constructor has implicit call to super constructor



Please tell me why that needs to happen?

Suppose class B is extending class A:

During instantiation of class B object I understand that constuctorB will implicitly call constructorA. This is because we are not only creating everything specified in class B but also other members such as private methods and private variables of class A also need to exist in the memory. For example: Class A has a public method display() that uses its private variables. We extend class A and call it class B and add a few more methods. Now we create an instance of class B. We know that we can invoke display() method on B object. How is this possible? It can only happen when everything defined in A is also present in the memory. Hence in B's constructor even if we don't call super() we know that implicitly that call will still occur.

Okay I just now realized that maybe the same principle applies to the case Class B implements abstract class A . One trivial clarification regarding this:

What happens in the following case:
abstract Class A
|
| (B extends A)
|
abstract Class B
|
| (C extends B)
|
abstract Class C
|
| (D implements C)
|
Class D <== we instantiate an object for D. Here is it true that constructor for D calls constructor for C which in turn calls its super all the way till A? I think that that is what should happen.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anchit Herredia wrote: Class B implements abstract class A


A class CANNOT implement another class

a class can only implement an interface

and your consideration about the constructor calling for class A class B class C and class D is true
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic