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

This topic is 4521 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
Declare its constructors protected.

##### Share on other sites
For instance,

Common PC = new Common();

...should be illegal. Its sub-classes should only be allowed to instantiate.

##### Share on other sites
Right. Declare the constructor protected, and only subclasses will be able to be instantiated.

##### Share on other sites
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

##### Share on other sites
Java, not C++. To make a class abstract in Java declare it as abstract.

##### Share on other sites
Quote:
 Original post by SiCraneJava, 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

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by dxFooCool, 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?

##### Share on other sites
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.

##### Share on other sites
Are you sure about that? I was almost certain that protected restricted access to the class and its subclasses, whereas a lack of an access specifier restricted access to package members.

EDIT: Ah, I got it. I hadn't realized that package-access was MORE restrictive than protected-access. Seems kinda odd.