protected

PHP protected classes and properties, protected from whom?

限于喜欢 提交于 2019-11-28 23:53:32
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 seeing as how PHP is always in plain text. Caution: all of the above written by a beginner with a beginner

What is the practical use of protected inheritance?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 23:33:13
Public inheritance is easy. A : public B means every A is a B. In most programming language, like vb.net and objective-c, this is the only type of inheritance. Private inheritance is also easy but pointless A : private B means A is implemented by B. However, that's pointless because that means A should contain B instead. Ownership means less coupling with no disadvantage. Then we have protected inheritance. Can anyone explain to me what the hell is that for? Some says it's an "as a relationship". I am still not very clear on that. Does anyone has some sample cases where people uses protected

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

若如初见. 提交于 2019-11-28 23:03:01
问题 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 {

protected vs public constructor for abstract class? Is there a difference?

时光毁灭记忆、已成空白 提交于 2019-11-28 20:08:48
This question is out of curiosity. Is there a difference between: public abstract class MyClass { public MyClass() { } } and public abstract class MyClass { protected MyClass() { } } Thanks. They are the same for all practical purposes. But since you asked for differences, one difference I can think of is if you are searching for the class's constructor using reflection, then the BindingFlags that match will be different. BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; var constructor = typeof(MyClass).GetConstructor(flags, null, new Type[0], null); This will find the

Best practices to test protected methods with PHPUnit (on abstract classes)

痞子三分冷 提交于 2019-11-28 18:01:34
With PHPUnit and PHP >= 5.3 it is possible to test protected methods. The following page at stackoverflow outlined the best practice on it: "Best practices to test protected methods with PHPUnit" protected static function callProtectedMethod($name, $classname, $params) { $class = new ReflectionClass($classname); $method = $class->getMethod($name); $method->setAccessible(true); $obj = new $classname($params); return $method->invokeArgs($obj, $params); } To test public methods on abstract classes is easy with PHPUnit. To test protected methods on normal classes is easy with approach above. To

Java reflection - access protected field

筅森魡賤 提交于 2019-11-28 17:28:52
How can I access an inherited protected field from an object by reflection? kenj0418 Two issues you may be having issues with - the field might not be accessible normally (private), and it's not in the class you are looking at, but somewhere up the hierarchy. Something like this would work even with those issues: public class SomeExample { public static void main(String[] args) throws Exception{ Object myObj = new SomeDerivedClass(1234); Class myClass = myObj.getClass(); Field myField = getField(myClass, "value"); myField.setAccessible(true); //required if field is not normally accessible

Reasons to use private instead of protected for fields and methods

本小妞迷上赌 提交于 2019-11-28 17:28:09
This is a rather basic OO question, but one that's been bugging me for some time. I tend to avoid using the 'private' visibility modifier for my fields and methods in favor of protected . This is because, generally, I don't see any use in hiding the implementation between base class and child class, except when I want to set specific guidelines for the extension of my classes (i.e. in frameworks). For the majority of cases I think trying to limit how my class will be extended either by me or by other users is not beneficial. But, for the majority of people, the private modifier is usually the

What are practical uses of a protected constructor?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 15:41:51
Why would anyone declare a constructor protected? I know that constructors are declared private for the purpose of not allowing their creation on stack. When a class is (intended as) an abstract class, a protected constructor is exactly right. In that situation you don't want objects to be instantiated from the class but only use it to inherit from. There are other uses cases, like when a certain set of construction parameters should be limited to derived classes. one use could be factory patterns Non-public constructors are useful when there are construction requirements that cannot be

Why is Java's AbstractList's removeRange() method protected?

こ雲淡風輕ζ 提交于 2019-11-28 15:17:11
Does anyone have any idea, why removeRange method in AbstractList (and also in ArrayList ) is protected ? It looks like a quite well-defined and useful operation, but still, to use it, we're forced to subclass the List implementation. Is there some hidden rationale? Seems quite inexplicable to me. Yes, because that's not how you remove a range from outside code. Instead, do this: list.subList(start, end).clear(); This actually calls removeRange behind the scenes. † The OP asks why removeRange is not part of the List public API. The reason is described in Item 40 of Effective Java 2nd ed, and I

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

纵然是瞬间 提交于 2019-11-28 15:14:11
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 methods in the class depend upon these protected/private methods functioning correctly, therefore I