• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

A class cannot be subclassed...

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By making the constructor private, a class cannot be subclassed

1. True
2. False

is this True???
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your question talks about "the constructor", but a class can have more than one constructor. If all constructors of a class are private, then you can't subclass it. Try it and see what the compiler says:

[ August 31, 2007: Message edited by: Jesper Young ]
 
Thiru Mu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joseph,
Yes I have tested it.

If I make the default Constructor private, then the Class cannot be subclasses. for instance,

class Super{
private Super(){
}
Super( int a){
}
}
public class Test extends Super{ // LINE
public static void main(String args[]){

}
}

I am getting a compiler error here saying Super() is not visible at //LINE

But if I make the default constructor as public and the other one as private, the class can be subclassed.

So here I can conclude that, if the default constuctor is provided and marked as private the class cannot be subclessed. You agree this??
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Test2 compiles happily.


Note by the way that you should not mix up the technical terms "default constructor" and "parameterless constructor" or you may confuse yourself and others:


Class one has a default constructor, class two has a parameterless constructor.

Yours,
Bu.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If all the constructors of a class are private then we can not create subclass for that class.
if it contains combinations i.e. both private and others , then we can create subclass.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if we are using more than one constructor in superclass for eg
class Super{
Super(){}//1
Super(int a){}//2
}

If 1 is private and when called in main give compilation error and same we'll get if 2 is private.
now as in above eg
class Super{
private Super(){}
Super(int a){}
}

public class Test2 extends Super{
Test2() {
super(42);
}
public static void main(String args[]){}
}


so here we are using constructor of super class which is not private at alland moreover it is called from constructor test2().

its not a solution as we always can't depend on other constructor to call first one and the problem was originally calling a private constructor not any constructor.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

The above discussion has confused me a bit
to what is the answer please explain me
that if which type of constructor(default,no-arg,parameterised)
if made private will lead to a



class having that constructor cannot be subclassed?


Please explain!!!
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, back to the question:


By making the constructor private, a class cannot be subclassed

1. True
2. False

is this True???



Yes, it is true. Plain and simple.

The question states: "By making the constructor private..."

Using the article "the" implies that there is only one constructor.

If a class had more than one constructor, you could not ask about the constructor, because this would be bad english.



Yours,
Bu.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic