How to block class inheritance and instances?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: How to block class inheritance and instances?

  1. #1
    Join Date
    Jul 2004
    Posts
    2

    How to block class inheritance and instances?

    I have this:
    public class myProgram{
    public static void main(String[] args){
    System.out.println("Hello everyone!");
    }
    }

    class myInherit extends myProgram{
    // Anything
    }

    class myInstance{
    public static myProgram theInstance;

    public static getInstance(){
    myInstance.theInstance = new myProgram();
    }
    }

    ------------------------------------------------

    I don't want people to inherit OR make an instance of the class myProgram(). Mainly because it would be pointless! There is nothing inside of the class to use! But it has to be Public so the program can run.

    So I had a couple options, but none of them worked 100% (all 50%):

    //Problem with this? They can still extend the class!
    public abstract class myProgram{ // The abstract would prevent people making instances of this class
    public static void main(String[] args){
    System.out.println("Hello everyone!");
    }
    }

    //Problem with this? They can still create instances of this class!
    public final class myProgram{ // The final would prevent people inheriting this class
    public static void main(String[] args){
    System.out.println("Hello everyone!");
    }
    }


    ------------------------------------------------

    Anyone have any suggestions? Thank you for your time!

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    a final class with a private constructor, can be neither instantiated, nor extended. it's a lot of effort for nothing though..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Jul 2004
    Posts
    2
    Thank you cjard!

    "it's a lot of effort for nothing though.."
    Yes, that is true lol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center