• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Protected modifier

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all of you;

I found this example on the net ..but i can't understand the real reason why the line in bold is illegal, since it a reference of an a object that is calling the protected method

***
package Greek;

class Alpha {
protected void iamprotected() {
System.out.println("iamprotected");
}
}

package Greek;

class Gamma {
void accessMethod() {
Alpha a = new Alpha();
a.iamprotected();// legal
}
}

class Gamma extends Alpha {
void accessMethod(Alpha a) {
a.iamprotected = 10;// illegal
this.iamprotected = 10;// legal
}
}

Can anyone explain ?

thank you a lot
[ August 22, 2007: Message edited by: Abahmane mustafa ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'iamprotected' is a method in class Alpha. You can't assign a value to 'iamprotected' as if it is a member variable.

The statement 'this.iamprotected = 10;' also will not compile.
 
Abahmane mustafa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:
'iamprotected' is a method in class Alpha. You can't assign a value to 'iamprotected' as if it is a member variable.

The statement 'this.iamprotected = 10;' also will not compile.



Sure?

void accessMethod(Alpha a) {
a.iamprotected = 10;// illegal

It is a method of the class Alpha .

class Alpha {
protected void iamprotected() {
System.out.println("iamprotected");
}
}

I'm not considering it as a member variable, i'm calling the method from a reference to an a ojbect ..ok?
[ August 22, 2007: Message edited by: Abahmane mustafa ]
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic