File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Making a class non extensible without using final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Making a class non extensible without using final" Watch "Making a class non extensible without using final" New topic
Author

Making a class non extensible without using final

Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
Hi,
Is it possible to make a class non extensible without using the final key word?
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
mark the constructor with private access.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
Thank you vishnu. It worked.
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162


Make all constructos private.


SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608


Make all constructors private of a top level class.


Tony Morris
Java Q&A (FAQ, Trivia)
Ramakrishna Nalla
Ranch Hand

Joined: Apr 21, 2005
Posts: 61
What you mean tony... Inner classes always extensible even their constructors marked private...


The above code compiles fine...
Please Clear me...
raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60
Making all constructors private only stops that class being instantiated from outside, but doesn't stop it being sub-classed.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by vneeth babu:
Making all constructors private only stops that class being instantiated from outside, but doesn't stop it being sub-classed.


It sure does. Every subclass has to call a constructor of its superclass.

Please don't respond showing how inner classes can call the private constructors of other inner classes in the same parent, or of their containing class.


[Jess in Action][AskingGoodQuestions]
raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60
I think I'm not sure of :
"Every subclass has to call a constructor of its superclass."

If the parent class's constructor is private then how can
the subclass call superclass's constructor, as suprclass constructo won't be visible to subclass. Am I missing something in understanding the above statement, please?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Apparently, yes, you're missing something: that's the whole point. To make it impossible for a top-level class to have any subclasses (aside from its own inner classes), you can make all its constructors private. This will prevent any subclasses from calling those constructors, which will prevent any subclasses from compiling -- i.e., the class can't have any subclasses.
raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60
got it....thanks
bhavesh bhanushali
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
class Super
{
private Super ( )
{
System.out.println ( " We are in the super class " );
}
}

class Sub extends Super // ( 1 )
{
Sub ( )
{ // ( 2 )

System.out.println ( " We are in the sub class " );
}
}

public class InextensibleClasses
{
public static void main ( String args[] )
{
Sub objS = new Sub ( ) ; // ( 3 )
}
}

as we can see from line ( 1 ) , that the subclass can extend the super class, but the problem will arrive when we try to create an instance of the sub class at ( 3 ) , while creating the instance a call to the sub class constructor is made which results in a implicit call to the super class constructor at ( 2 ) , now since the super class constructor is private it is not accessible to the subclass , as a result of which we get a compile time error

Thus the subclass was'nt able to extend the super class with a private constructor

regards,
Bhavesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making a class non extensible without using final