What are the other ways to prevent a class from being subclassed.one way is to declare the class final.does declaring a class private prevent it from being subclassed?

Showing Answers 1 - 20 of 20 Answers

Ranjeet Kumar Behera

  • Nov 17th, 2005
 

Hi,

As per ur question, yes we prevent a class to be subclass by making class as final, bt also if we make the constructor of that class private then also we cant inherite that class or cant subclass that class.

Regards

Ranjeet.

  Was this answer useful?  Yes

siva

  • Nov 21st, 2005
 

Im sorry we can able to subclass even if class constructor is private

Ranjeet

  • Nov 29th, 2005
 

Well Siva..i think there is no way  to subclass the class other than making constructor private and final..if u know then i request u to tell us one solution.

  Was this answer useful?  Yes

rani

  • Feb 1st, 2006
 

i'm totally confused. Shall we prevent the class to be inherited by declaring the constructor as private. please clarify this doubt.

  Was this answer useful?  Yes

Pankaj Vishwani

  • Mar 7th, 2006
 

I just tried out compiling a class with private constructor and other class extending this class and calling a method. JVM doesn't have any problem with it. So its very clear that making a constructor private doesnt solve the problem(I mean it doesnt makes the class non-inheriable.)here's the code that I used:class stud { private void stud(){ } void nothing(){ System.out.println("nothing wrong with private constructor"); } public class start extends stud { public void method(){ System.out.println(); } public static void main(String args[]) { start s = new start(); s.nothing(); }}

  Was this answer useful?  Yes

Mikhail Rubinshteyn

  • Mar 13th, 2006
 

In the constructur you can check if the created object of the class is the

same or it is a child. Something like that:

class A {

   public A() {

        if (this.getClass() != A.class)

               throw new RuntimeException("Not an A class");

   }

}

  Was this answer useful?  Yes

Niranjan

  • Mar 14th, 2006
 

If you declare a constructor as private, does not mean you cannot subclass it!! Think of it... Can't Singleton classes be sub-classed?

  Was this answer useful?  Yes

praveen

  • Mar 16th, 2006
 

i don't think there ia a cleaner way of preventing inheritance than using final.

  Was this answer useful?  Yes

JavaStudent

  • Mar 20th, 2006
 

Hello Pankaj Vishwani, it is no surprise that ur program worked properly bcos u have given a return type for ur constructor and so java does not take it as a constructor but just like any other method. so as per ur coding, ur program doesnt have a constructor atall, but two methods. Please delete the "void" given before the constructor and try running it, it will give u a compilation error. So the point is, when we make a construtor as private, it cannot be inherited.

Good Luck and Cheers

  Was this answer useful?  Yes

Anil Kumar

  • Mar 24th, 2006
 

Hi Everybody

Q: what are the other ways to prevent a class from be...

By declaring super class constructor as private, we can prevent that class(Super) to inherite other classes. this can be passible when the super class not a main class

Here is code

class C1 //not a main class
{
  private C1()
 {
  System.out.println("In C1 Con");
 }
}
 class D extends C1
{
  D() 
 {
 System.out.println("In class D");
 
 }
}
public  class C

{
 public static void main(String[] args)
 {
  //C1 c2=new C1();
  D d1=new D();
  System.out.println("Hello World!");
 }
}

  Was this answer useful?  Yes

santukt

  • Apr 15th, 2006
 

Hay sister dont know the rule of Constructor.No return type please.If return type is there it is a normal function

  Was this answer useful?  Yes

Michael Wei

  • Apr 22nd, 2006
 

Inheriting a base class with private constructors are allowed. But there is no way for you to write a constructor in sub class because when sub class is instanced, it instances a base class as well by using either default constructor or by super(...). If all the constructors are private, there is no way to find any constructor that can be called from sub class.

  Was this answer useful?  Yes

Ram

  • Jan 8th, 2007
 

well pankaj. The deal is to make the constructor private. In your example you have created a method as private as constructor do not have any return type. So try compiling again. cheers. Ram

  Was this answer useful?  Yes

Pankaj Vishwani.....
your code is compiling because you have prefixed the kerword void before the constructor of stud class...... hence its not a constructor anymore.... its beheaving like a method as contstructor can't have any return type.......

just remove void from stud class constructor and you will surely understand....

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions