[java] Is there a keyword for not instantiating a super-class?

Started by
11 comments, last by Raghar 18 years, 7 months ago
Three sub-classes inherit its super-class "Common." How can I make sure that Common can't be instantiated, yet its subclasses can? This would prevent errors on the programmer's side later. Thanks in advanced, Phil
Advertisement
Declare its constructors protected.
For instance,

Common PC = new Common();

...should be illegal. Its sub-classes should only be allowed to instantiate.
Right. Declare the constructor protected, and only subclasses will be able to be instantiated.
Edit: This post is now void since I stupidly assumed C++ instead of noticing that the thread is in the Java Development forum. I guess that's what I get for only ever looking at the Active Topics page.

Or, if this is appropriate, make one or more of Common's functions pure virtual, which means that you don't provide any implementation in Common; the implementation will always be provided by the subclasses.

eg,
class Common{public:    virtual std::string GetName() const = 0; // <-- a pure virtual function};class AThing : public Common{public:    virtual std::string GetName() const; // AThing provides an implementation of the function};


One advantage (or disadvantage depending on circumstances, I guess), is that if a subclass doesn't implement all the pure virtual functions, then you won't be able to instantiate that subclass, either (a class with any pure virtual functions is an abstract class, and cannot be instantiated)

Edit: The point of pure virtual functions is that in some cases, a parent class defines an interface, but it doesn't make any sense for that parent class to define an implementation for that interface (if there isn't a suitable default implementation) - where that's the case, you should make those functions pure virtual to indicate how you intend the class to be used (and get the benefit of the compiler checking that the class is never instantiated.)

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Java, not C++. To make a class abstract in Java declare it as abstract.
Quote:Original post by SiCrane
Java, not C++. To make a class abstract in Java declare it as abstract.

Oh, crap. My mistake, I apologise.
[note to self: Remember to actually check the forum that a thread is posted in before replying]

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Cool, abstract worked! Sneftel, by declearing protected, the class still instantiated fine. But thank you for your efforts. By using abstract behind the class keyword, it gave what I hoped it would say: "Common is abstract; cannot be instantiated."

Thanks for the help.
Quote:Original post by dxFoo
Cool, abstract worked! Sneftel, by declearing protected, the class still instantiated fine. But thank you for your efforts. By using abstract behind the class keyword, it gave what I hoped it would say: "Common is abstract; cannot be instantiated."

Thanks for the help.

Yes... I, too, missed the forum name. I'm surprised that Java allows that, though. I wonder why it would allow you to instantiate a class if you don't have access to the constructor?
The protected keyword only marks a method (or in this case, constructor) as package private. You can still instantiate the class inside of the package in which the class is declared. If both of dxFoo's classes were declared in the default package (i.e., they had no package statement) Java would allow the instantiation of the supposedly protected class.

This topic is closed to new replies.

Advertisement