encapsulation

What is encapsulation? How does it actually hide data?

懵懂的女人 提交于 2019-11-26 22:59:28
问题 Searching turns up a simple definition: data hiding. But, consider the following two examples: 1) First Example: Class Employee { public int age; } 2) Second Example: Class Employee { private int age; public int getAge(){return age;} } Question: In both the above specified examples, there is no data hiding, as age is either being modified by others or being viewed by others. Where is the data hiding? How does encapsulation help in the above examples? 回答1: There is data hiding. The second

What is the visibility of @synthesized instance variables?

醉酒当歌 提交于 2019-11-26 22:31:27
If you have a property in your public interface like the following @interface MyClass : NSObject @property(strong) NSString *myProp; @end And then synthesize it, in effect synthesizing the variable: @implementation MyClass @synthesize myProp = _myProp; // or just leave it at the default name.. @end What is the visibility of the instance variable _myProp ? That is, is this considered @public , @protected or @private ? I'm guessing since MySubClass could inherit from MyClass then it would also get the properties (naturally), but would it also inherit the instance variable visibility? What

Globally defined AngularJS controllers and encapsulation

坚强是说给别人听的谎言 提交于 2019-11-26 22:16:44
问题 According to AngularJS's tutorial, a controller function just sits within the global scope. http://docs.angularjs.org/tutorial/step_04 Do the controller functions themselves automatically get parsed into an encapsulated scope, or do they dwell within the global scope? I know that they are passed a reference to their own $scope, but it appears that the function themselves are just sitting in the global scope. Obviously this can cause problems down the road, and I have learned through

method without access modifier

对着背影说爱祢 提交于 2019-11-26 20:47:29
问题 Ok this is bugging me.. I know I've read it somewhere and google isn't helping. What is the accessibility level of a method that does not specify an access modifier? void Foo() { //code } I want to say internal but I'm not 100% sure. 回答1: The default accessibility for a type is internal , but the default accesibility of that type's members depends on the type. Generally speaking, members of a class are private by default, where as members of a struct are public by default. This varies by

Simple way to understand Encapsulation and Abstraction

别等时光非礼了梦想. 提交于 2019-11-26 19:30:35
Learning OOP concepts especially interested to understand Abstraction and Encapsulation in depth. Checked out the below already Abstraction VS Information Hiding VS Encapsulation difference between abstraction and encapsulation? I found very hard to understand those concepts with out a real and simple example class/code snippet. One of my colleagues said abstraction is nothing but creating abstract class and normal class that protects its member variable with scope is called Encapsulation. Is there a simple way I can understand and help others to understand what exactly they are, rather than

Encapsulation vs Data Hiding - Java

一个人想着一个人 提交于 2019-11-26 18:58:15
问题 Interviewer: What is encapsulation and how do you achieve it in Java? Me: Encapsulation is a mechanism to hide information from the client. The information may be data or implementation or algorithm. We achieve this using access modifiers. Interviewer: This is data hiding. How do we achieve encapsulation in Java? Me : uummmm Concrete Question: Other than 'Access Modifiers', what is the way to implement Encapsulation in Java? 回答1: More generally encapsulation refers simply to bundling the data

Must Dependency Injection come at the expense of Encapsulation?

。_饼干妹妹 提交于 2019-11-26 18:44:36
问题 If I understand correctly, the typical mechanism for Dependency Injection is to inject either through a class' constructor or through a public property (member) of the class. This exposes the dependency being injected and violates the OOP principle of encapsulation. Am I correct in identifying this tradeoff? How do you deal with this issue? Please also see my answer to my own question below. 回答1: There is another way of looking at this issue that you might find interesting. When we use IoC

Effective C++ Item 23 Prefer non-member non-friend functions to member functions

折月煮酒 提交于 2019-11-26 18:41:56
While puzzling with some facts on class design, specifically whether the functions should be members or not, I looked into Effective c++ and found Item 23, namely, Prefer non-member non-friend functions to member functions. Reading that at first hand with the web browser example made some sense, however convenience functions( named the nonmember functions like this in the book) in that example change the state of the class, don't they? So, first question, should not they be members then? Reading a bit further, he considers the STL functions and indeed some functions which are not implemented

How abstraction and encapsulation differ?

北城余情 提交于 2019-11-26 18:21:33
I am preparing for an interview and decided to brush up my OOP concepts. There are hundreds of articles available, but it seems each describes them differently. Some says Abstraction is "the process of identifying common patterns that have systematic variations; an abstraction represents the common pattern and provides a means for specifying which variation to use" (Richard Gabriel). and is achieved through abstract classes. Some other says Abstraction means to show only the necessary details to the client of the object and Let’s say you have a method "CalculateSalary" in your Employee class,

Getters and Setters are bad OO design? [duplicate]

假如想象 提交于 2019-11-26 17:37:19
问题 This question already has answers here : Why use getters and setters/accessors? (38 answers) Closed 4 years ago . Getters and Setters are bad Briefly reading over the above article I find that getters and setters are bad OO design and should be avoided as they go against Encapsulation and Data Hiding. As this is the case how can it be avoided when creating objects and how can one model objects to take this into account. In cases where a getter or setter is required what other alternatives can