• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Dans mock exam -Question on protected access

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This Question is taken from Dans Mock exam
***********************************************

// Class A is declared in a file named A.java.
package com.dan.chisholm;
public class A {
public void m1() {System.out.print("A.m1, ");}
protected void m2() {System.out.print("A.m2, ");}
private void m3() {System.out.print("A.m3, ");}
void m4() {System.out.print("A.m4, ");}
}
// Class C is declared in a file named C.java.
package com.dan.chisholm.other;
import com.dan.chisholm.A;
public class C extends A {
public static void main(String[] args) {
C c = new C();
c.m1(); // 1
c.m2(); // 2
c.m3(); // 3
c.m4(); // 4
}}

*****************************************
The answer to this question is there is a compiler error in step 3.
How come there isnt error in the step 2 too.
My understanding accordking to the K & B is the subclass can see the protected class through interitance and it cannot use the dot operator (reference)

Please clarify my question.

Thanks,
vijaya
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The protected method is an instance member and so it will need the dot operator !!
However if the method were static then you cannot use the dot operator.
[ August 14, 2004: Message edited by: Murtuza Akhtari ]
 
Murtuza Akhtari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also in addition to the answer

Compile error at line 3

there will also be a compile error at line 4
since method m4() is declared without any modifiers and so will have the default access...which is only for the same package. Since class A and class C are in different packages...invoking method m4() from class C will result in compile error.
 
vijaya dev
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a bit confused here in Page 78 in K&B the example uses instance member

From K & b

package certificate;
public class Parent {
protected int x=9;
}
package other;
import certificate.Parent{
public void testit()
{
System.out.println(" X is"+ x); Works okay
Parent p=new Parent();
System.out.println(" X is"+ p.x); compiler error

}

Is this right..or Am i getting confused
 
Murtuza Akhtari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice the difference here...

In the first case, you are instantiating an object of the child class and in the second case you are instantiating an object of the parent class.

You cannot use a parent reference to access protected members, however you can use the child reference.

Also note on Page 79 K&B book---paragraph right after the error message

...Finally we've seen that subclasses outside the package can't use a superclass reference to access a protected member.

 
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 Murtuza Akhtari:
Also in addition to the answer

Compile error at line 3

there will also be a compile error at line 4
since method m4() is declared without any modifiers and so will have the default access...which is only for the same package. Since class A and class C are in different packages...invoking method m4() from class C will result in compile error.



Murtuza,

You are correct and the answer in my exam is consistent with your statement.
 
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 Murtuza Akhtari:
Notice the difference here...

In the first case, you are instantiating an object of the child class and in the second case you are instantiating an object of the parent class.

You cannot use a parent reference to access protected members, however you can use the child reference.

Also note on Page 79 K&B book---paragraph right after the error message



Here's an example.

 
vijaya dev
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies...
 
reply
    Bookmark Topic Watch Topic
  • New Topic