protected

Why protected can be access in same Package without inheritance in java? [duplicate]

守給你的承諾、 提交于 2019-11-30 11:32:24
This question already has an answer here: Why does the “protected” modifier in Java allow access to other classes in same package? 6 answers Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N public class a { protected int x; } public class b { b() { a A=new a(); A.x=9;//why we can access this field ? } } please help my to know the specific work of protected in Java Why? Because that's how the Java programming language was designed. There's not much more to it. Something that is protected is accessible from the class itself, classes in

JavaDoc for private / protected methods? [closed]

烈酒焚心 提交于 2019-11-30 07:49:18
Should I write JavaDoc for private or protected methods? And what about private variables? I see class examples on my Java book and the private variables are JavaDoc'ed. So I can't understand if it is a good practice to JavaDoc the private (or protected ) methods. AlexWien Yes you should write JavaDoc for private methods, and even when it is only for yourself. In 3 years when you have to change the code, you will be happy that you documented it. If you leave the company, or have to work on another project, your co-workers will be happy to have a documented code. Undocumented code has much

PHP protected classes and properties, protected from whom?

浪尽此生 提交于 2019-11-30 06:59:48
问题 I'm just getting started with OOP PHP with PHP Object-Oriented Solutions by David Powers, and am a little curious about the notion of protection in OOP. The author clearly explains how protection works, but the bit about not wanting others to be able to change properties falls a bit flat. I'm having a hard time imagining a situation where it is ever possible to prevent others from altering your classes, since they could just open up your class.php and manually tweak whatever they pleased

What's the best way to unit test protected & private methods in Ruby?

旧街凉风 提交于 2019-11-30 06:11:59
问题 What's the best way to unit test protected and private methods in Ruby, using the standard Ruby Test::Unit framework? I'm sure somebody will pipe up and dogmatically assert that "you should only unit test public methods; if it needs unit testing, it shouldn't be a protected or private method", but I'm not really interested in debating that. I've got several methods that are protected or private for good and valid reasons, these private/protected methods are moderately complex, and the public

C++ why use public, private or protected inheritance?

廉价感情. 提交于 2019-11-30 05:05:44
Well there is enough information about this subject. For example this thread was very clear to me: Difference between private, public, and protected inheritance Except one point; Why is it useful? Use public inheritance to reflect an is-a relationship . This is the main use for inheritance, especially in combination with virtual functions. It allows re-use of interface, not just of old code by new code, but also re-use of new code by old code! (because of virtual function dispatch at runtime). In exceptional circumstances, use private inheritance to reflect an is-implemented-in-terms-of

How are java.lang.Object's protected methods protected from subclasses?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 02:08:23
The keyword protected grants access to classes in the same package and subclasses ( http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html ). Now, every class has java.lang.Object as superclass ( http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html ). Hence I conclude that every class may access java.lang.Object 's methods even if they are protected . Take a look at the following example: public class Testclass { public Object getOne() throws CloneNotSupportedException { return this.clone(); } public Object getTwo() throws CloneNotSupportedException { return ((Object)

Unit testing C# protected methods

痴心易碎 提交于 2019-11-29 21:13:38
I come from the Java EE world but now I'm working on a .Net project. In Java when I wanted to test a protected method it was quite easy, just having the test class with the same package name was enough. Is there anything similar for C#? Is there any good practice for unit testing the protected methods? I only found frameworks and people saying that I should test only public methods. It should be possible to do it without any framework… unarity You can inherit the class you are testing on your test class. public class Test1 : SomeClass { Asert.AreEqual(1, SomeClass.ProtectedMethod()); } Another

protected data in abstract class

柔情痞子 提交于 2019-11-29 17:16:33
问题 My question involves specifically Java, abstract classes, and the use of protected data. I am being told that all the data should be private, and protected getters/setters used only. Now, I understand we want to shield data from direct manipulation by casual users of the class, and that public data members in general are a questionable practice. I have looked at "Java protected fields vs public getters" ( Java protected fields vs public getters ), but I still am dubious that: protected int i;

Why protected can be access in same Package without inheritance in java? [duplicate]

老子叫甜甜 提交于 2019-11-29 17:09:46
问题 This question already has answers here : Why does the “protected” modifier in Java allow access to other classes in same package? (6 answers) Closed 3 years ago . Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N public class a { protected int x; } public class b { b() { a A=new a(); A.x=9;//why we can access this field ? } } please help my to know the specific work of protected in Java 回答1: Why? Because that's how the Java programming

C# accessing protected member in derived class [duplicate]

浪子不回头ぞ 提交于 2019-11-29 13:37:28
This question already has an answer here: Why can't I access C# protected members except like this? 7 answers I wrote the following code: public class A { protected string Howdy = "Howdy!"; } public class B : A { public void CallHowdy() { A a = new A(); Console.WriteLine(a.Howdy); } } Now, in VS2010 it results in the following compilation error: Cannot access protected member 'A.a' via a qualifier of type 'A'; the qualifier must be of type 'B' (or derived from it). This seems quite illogical to me - why can't I access the protected field of the class instance from a method of the class, which