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

Member Visible in Sub Class?

 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Have a look at this question:

In the given piece of code, which members of class A can be accessed withing class B?

A i
B f
C j
D f()
E g()

I answered it B,D and E. But the answer is wrong.
Can someone suggest the Right answer?

Thanks

[ October 17, 2005: Message edited by: Sandeep Chhabra ]
[ October 17, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class B is not a subclass of class A in this code example. If it was, the your answer would be correct. So only f and g() is visible.
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But since f() is public dont you think it can be accessed in class B as:

class B
{
A a=new A();
a.f();
}

Wont this work?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandy,
are these the only options available or are there any other answers.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But since f() is public dont you think it can be accessed in class B as:

class B
{
A a=new A();
a.f();
}

Wont this work?


When import is used with the static keyword, then only the static members are imported. Others are not. Following is an extract from JCP.

One might well ask why the keyword static should appear in the
import declaration, for the compiler can easily tell what is to be
imported without the presence of that keyword. There are two reasons:
.....
To remind the reader (and the programmer) that only static members
can be so imported. (It does not work to try to refer to an instance
member of an arbitrary class by a simple name, because a simple name
fails to supply necessary information as to which instance of the
class is involved.)

 
Sara Olsen
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sandeep Chhabra:
But since f() is public dont you think it can be accessed in class B as:

class B
{
A a=new A();
a.f();
}

Wont this work?



deleted....
[ October 17, 2005: Message edited by: Sara Olsen ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A in pack1 is not visible to class B in package pack2 at all.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i didn't know you are able to use import static ...; is this a tiger feature? because when i try to typ 'import static java.awt.Color;' my compiler complains about the 'static' identifier.

concerning the initial question: class A has default access modifier, so is only visible for classes in the same package, because B is in different package, class A isn't visible and so none of the members are accessible from B (unless this static import does also other things)
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"(unless this static import does also other things)"

It just imports static members which do not need an instance of the class to be used. It certainly does not overcome the issue of A not being a public class. First the class A has to be visible to the package pack2 before even public members of A can be accessed.

It is a Java 5.0 feature by the way - The Tiger is dead, long live Java 5.0! (for a few more months at least).
[ October 17, 2005: Message edited by: Barry Gaunt ]
reply
    Bookmark Topic Watch Topic
  • New Topic