• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Quick and Easy

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, here is a straightforward question:
If abstract classes have no constructors (since one cannot have instances of them) how is it possible to extend from them? If I make a subclass isn't there always an implicit call to super() in that subclasse's constructor? But in this case there is no super constructor ...
Thanks in advance
Dan
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dan
You are quite right when you say that an abstract class cannot be instantiated. However, this does not mean that abstract classes cannot have constructors - it is perfectly legal to define constructors in abstract classes, even though they cannot be used. As far as I'm aware, the normal rules apply regarding the compiler creating a default constructor in the absence of any constructors being defined by the programmer.
To illustrate this point, save the following code in a file called AbstractTest.java and compile it - you will see that the compiler is perfectly happy. Just to test the functionality of the construtors, you may want to add a main that attempts to construct an instance of AbstractTest via one of it's constructors - you should find the compiler complains.
Hope this helps
Cheers
Michael

public abstract class AbstractTest
{
public AbstractTest()
{
}
public AbstractTest(int i)
{
}
public abstract void doSomeStuff(int i);
}
class Extender extends AbstractTest
{
public Extender()
{
super();
}
public Extender(int i)
{
super(i);
}
public void doSomeStuff(int i)
{
System.out.println("Doing some stuff");
}
}
 
Dan Temple
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow! I had no idea that abstract classes could have constructors. Thanks so much for the info
Dan
 
Skool. Stay in. Smartness. Tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic