• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

class' vs constructor's accessability

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do we handle cases when a class and its constructor aren't of the same accessability in JVM 1.4:
1) if the constructor has greater accessability than a class itself:
class AClass{
public AClass(){
}
}
or
class BClass{
protected BClass(){
}
}
2) if the constructor is less accessible than its class:
class CClass{
private CClass(){
}
}
or
public class DClass{
DClass(){
}
}
(there are more than 2 examples of constructor being less accessible than class).
What happens is we create:
- AClass() object from a class located in different package
- BClass() object from a class (other than its subclass) located in different package
- CClass() object from other class in the same package
- DClass() object from a class from different package ?
Do we get a compile error or run-time exception in JVM 1.4?
Is it always the more restricted of the two - class , constructor - that is taken into account?
Thank you!
Stanislava
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stanislava
The first thing that you need to look at is class visibility. Then look at method visibility.


- AClass() object from a class located in different package


AClass has a default access modifier so AClass is not visible from outside of that package. So in this case the access modifier on the constructor method does not matter. This will not compile.


- BClass() object from a class (other than its subclass) located in different package


Once again BClass has default access modifier so the class is not visible. This will not compile.


- CClass() object from other class in the same package


This time the class is visible but the constructor is not so this will not compile.


- DClass() object from a class from different package ?


DClass is visible but the constructor is not so this will result in a compile error as well.


Is it always the more restricted of the two - class , constructor - that is taken into account?


You have to look at class visibility first, then the method visibility.
Hope this helps
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to call a constructor of a class to intitialize an object of that class, you must first have access to the class. Once you have access to a class, it then depends on the accessibility to the constructor that decides whether you can call it to initialize the object.
Apply this rule, the compiler will report errors in all cases you mentioned
 
Stanislava Trajlov
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It helps, thank you both!
Stanislava
 
straws are for suckers. tiny ads are for attractive people.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic