Quick and Easy
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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");
}
}
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");
}
}
"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Dan Temple
Ranch Hand
Posts: 93
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Wow! I had no idea that abstract classes could have constructors. Thanks so much for the info
Dan
Dan
| Skool. Stay in. Smartness. Tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |






