• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructors  RSS feed

 
Daniel Wawrzyniec
Greenhorn
Posts: 1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome this is my first message at coderanch.
I know that a constructor can have parameters: public, private and protected;
But I can not find an example showing a situation;
How can I show the impact of private constructor for a class that inherits;
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch Daniel Wawrzyniec
Daniel wrote:I know that a constructor can have parameters: public, private and protected;

these are not parameters. they are access modifiers. it control the visibility of a constructor
Daniel wrote:
But I can not find an example showing a situation;
How can I show the impact of private constructor for a class that inherits;

1.create packages bar and foo
2. Create public BarClass, put under bar
3. Create FooClass, put under foo
4. define a main method in FooClass and now provide private, protected, public constructors to BarClass(like private BarClass(), protected BarClass(int), public BarClass(String)) and try instantiate them from main method of FooClass
 
sandy sean
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How can I show the impact of private constructor for a class that inherits;


Consider following code:



above program could not compile as B() will not able to call its parent-class constructor A() as it is declared private. the program will give

ERROR:

B.java:9: A() has private access in A
B(){
^
1 error
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!