Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Make a class not to inherit and instantiate

 
ashok bantu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to make a class not to inherit and not to instantiate simultaneously
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you think about marking all the constructors in that class as private?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15286
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean exactly? You want to make a class that cannot be subclassed and that cannot be instantiated?

To make it impossible to subclass a class, make the class final.
To make it impossible to instantiate a class (from outside the class itself), make all constructors of the class private.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
To make it impossible to instantiate a class (from outside the class itself), make all constructors of the class private.

even the class cant subclass in this case.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... you cannot extend from a class with only private constructors, but it is best to mark the class final so any users know it cannot be extended from. That class should only have static members. There is a section about it in the Java™ Language Specification.
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That class should only have static members.

Only if it's meant to be a utility class. If the class is a singleton class, or if you provide static factory methods for creating instances, then having non-static members is allowed.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is a singleton, or has factory methods, then it can be instantiated, albeit only locally to its own class. It said "not to ... instantiate" in the thread title. So I presumed it must be a utility class with static members only, like java.lang.Math, which only has a private constructor.
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah, I forgot about that requirement.
 
ashok bantu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Not to inherit and instantiate any where whether it is inside or outside
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a private no-args constructor that throws an exception.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Write a private no-args constructor that throws an exception.
Like this?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably any kind of private constructor would do? With or without arguments.
 
Ramakrishna Gummadi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ashok bantu wrote:how to make a class not to inherit and not to instantiate simultaneously


declare a class's constructor as private then you cant inherit it and cant create object to it.
so that class cannot be inherited and cannot be instantiated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic