• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making a class non extensible without using final

 
Vipin Das
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is it possible to make a class non extensible without using the final key word?
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark the constructor with private access.
 
Vipin Das
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you vishnu. It worked.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Make all constructos private.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Make all constructors private of a top level class.
 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
raghu babu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it....thanks
 
bhavesh bhanushali
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic