• 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:

Object visibility

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
" Objects themselves don not have visibility, only references to the object "
I did't get this point.
The ans says it is true?
Bye
 
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I don't know the exact answer for sure, but here is what I think about it.
Take this example. Suppose you have a public class A and a subclass B extending A with default (package) visibility. These two clases are in a package (poorly) named package1.
You implement a method in any class of package1 that creates a reference to a class A object. You initialize this object with a new instance of class B (as polymorphism permits it).
Somehow, a class C from another package (let's call it package2) gains a copy of that reference. You have a class C instance holding a reference to a class A object which is legal as A is declared coderanch. Indeed, the reference points to an object of class B, but it can only be treated as an instance of class A since class C is not allowed to cast that reference back to B nor can it creates another B instance because it isn't in the same package.
So, I think we can say that the reference has visibility constraint as a reference to a class B object within class C would throw a compiler error. But the class itself has no visibility constraint as the Virtual Machine allows you to hold an instance of class B outside the package thanks to inheritance.
I know this example is not as clear as one could hope, but I hope it'll help you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic