• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Doubt on protected

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have 2 classes :

Class ProtectedTestClass2 :


and

Class ProtectedTestClass1 :


when i compile & run ProtectedTestClass1, it gives me the output :


Why ???

This has really got all my concepts about protected wrong. I thought that a protected member can be accessed only by a subclass in same or different package or by the object of the subclass if it is in the subclass itself.

But now here i stand watching the protected member being used, by the object of the class it is declared in, in some other class within the same package just like the default. Then this means that the only difference between default & protected is that protected can be accessed across different packages by extending the class whereas default only works for the same package.

Please someone look into this explain me why is such a behaviour of protected??
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please someone look into this explain me why is such a behaviour of protected??



That's is how it is defined. Protected allows access by all subclasses *and* classes within the same package.

Henry
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sen,

Try this code and be confident what you studied was correct
(but you need to add something to what to understood about
protected).


package com.nihilent.sourin.scjp2; //modified line
import com.nihilent.sourin.scjp1;

public class ProtectedTestClass1 {
public static void main(String[] args) {
ProtectedTestClass2 ptc = new ProtectedTestClass2();
System.out.println("Value of a = " + ptc.a); //compiler error
}
}
}
[/code]

One more tricky fact for exam:
See the code below



One more thing:
Read the code attentively

package com.nihilent.sourin.scjp2; //modified line
import com.nihilent.sourin.scjp1;

public class ProtectedTestClass1 {
public static void main(String[] args) {
ProtectedTestClass2 ptc = new ProtectedTestClass2();
System.out.println("Value of a = " + ptc.a); //compiler error
}
}
}
[/code]

One more tricky fact for exam:
See the code below



See one more example:



Got It Sen?

Regards,
cmbhatt
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Essentially, protected is less 'private' than package, so any member that is protected is also accessible as if it were a 'package' variable.

private > 'package' > protected > public.
 
money grubbing section goes here:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic