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

Dans exam

 
Ranch Hand
Posts: 8953
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What is the result of attempting to compile and run the above program?
a. Prints A.m1, A.m2, A.m3, A.m3, A.m4,
b. Compiler error at line 16.
c. Compiler error at line 17.
d. Compiler error at line 18.
e. Compiler error at line 19.
f. None of the Above
[Dan added the UBB codes to format the code block.]
[ February 24, 2003: Message edited by: Dan Chisholm ]
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
compile time error at lines 18 and 19
 
Pradeep bhatt
Ranch Hand
Posts: 8953
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is possible to access protected method through dot operator.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just remove "c." while accessing the protected method at line 17. this way you can avoid compilation error at this line. because C class imports A and having a "HAS-A" relation.
rest of the methods can not be accessed.
 
Pradeep bhatt
Ranch Hand
Posts: 8953
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Dans answer does not include line 17.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tausif Khanooni:
Just remove "c." while accessing the protected method at line 17. this way you can avoid compilation error at this line. because C class imports A and having a "HAS-A" relation.
rest of the methods can not be accessed.



Since Class C extends class A then it is possible for Class C to access protected methods of Class A even though Class C and Class A are declared in different packages. The relationship between Class C and Class A is an "is-a" relationship because Class C "is-a" subclass of class A. Therefore, a compilation error does NOT occur at line 17.
If the reference, c, is removed from the statement at line 17 then a compilation error will occur due to an attempt to invoke an instance method from a static context.
An example of a "has-a" relationship would be a delagation relationship where one class "has-a" member that is an instance of a class.
 
Tausif Khanooni
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOPS thnx Dan for correcting me.. i was bit careless while replying.
 
Pradeep bhatt
Ranch Hand
Posts: 8953
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan,
What if m2 was a protected variable and I typed
System.out.println(c.m2);
[ February 25, 2003: Message edited by: Pradeep Bhat ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:
Dan,
What if m2 was a protected variable and I typed
System.out.println(c.m2);
[ February 25, 2003: Message edited by: Pradeep Bhat ]


As long as the reference is of type C then it would print the value of the variable. If the reference were of type A then it would generate a compile time error. Go ahead and try it.
reply
    Bookmark Topic Watch Topic
  • New Topic