encapsulation

I can't create a clear picture of implementing OOP concepts, though I understand most of the OOP concepts. Why? [closed]

我的梦境 提交于 2019-11-28 05:33:00
I have been working on some of the projects of my own and dont have any indrustial exposure. Currently i use simple approach for developing small applications with negligible OO approach like creating a common class for database functions using polymorphism of functions little bit use of constructors but i dont really able to think of how to implement whole project logic using OOP. I know what are interface, abstract class, sealed classes and other oops concepts and very much clear picture in mind. But problem is that when and how should i implement OOP heavily where I should. Do i need to

When should I prefer non-member non-friend functions to member functions?

喜夏-厌秋 提交于 2019-11-28 05:02:55
Meyers mentioned in his book Effective C++ that in certain scenarios non-member non-friend functions are better encapsulated than member functions. Example: // Web browser allows to clear something class WebBrowser { public: ... void clearCache(); void clearHistory(); void removeCookies(); ... }; Many users will want to perform all these actions together, so WebBrowser might also offer a function to do just that: class WebBrowser { public: ... void clearEverything(); // calls clearCache, clearHistory, removeCookies ... }; The other way is to define a non-member non-friend function. void

What is encapsulation with simple example in php?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 04:54:50
What is encapsulation with simple example in php? Jeremy Logan Encapsulation is just wrapping some data in an object. The term "encapsulation" is often used interchangeably with "information hiding". Wikipedia has a pretty thorough article . Here's an example from the first link in a Google search for 'php encapsulation' : <?php class App { private static $_user; public function User( ) { if( $this->_user == null ) { $this->_user = new User(); } return $this->_user; } } class User { private $_name; public function __construct() { $this->_name = "Joseph Crawford Jr."; } public function GetName(

What is wrong with making a unit test a friend of the class it is testing? [duplicate]

谁说胖子不能爱 提交于 2019-11-28 04:04:37
This question already has an answer here: How do I test a private function or a class that has private methods, fields or inner classes? 50 answers In C++, I have often made a unit test class a friend of the class I am testing. I do this because I sometimes feel the need to write a unit test for a private method, or maybe I want access to some private member so I can more easily setup the state of the object so I can test it. To me this helps preserve encapsulation and abstraction because I am not modifying the public or protected interface of the class. If I buy a third party library, I

How to encapsulate an array in Java

眉间皱痕 提交于 2019-11-28 03:16:31
问题 I'm starting with Java and I'm learning about setters, getters and encapsulation. I have a very simple program, two classes: Container has a private int array ( numArray ) with his setter & getter. Main creates a Container object and uses it in totalArray method. public class Container { private int numArray[]= {0,0,0}; public int[] getNumArray() { return numArray; } public void setNumArray(int index, int value){ numArray[index] = value; } } public class Main { public static void main(String[

Difference between Encapsulation and Abstraction

丶灬走出姿态 提交于 2019-11-28 02:50:59
I had an interview today. I had a question from OOP , about the difference between Encapsulation & Abstraction ? I replied her to my knowledge that Encapsulation is basically to bind data members & member functions into a single unit called Class . Whereas Abstraction is basically to hide complexity of implementation & provide ease of access to the users. I thought she would be fine with my answer. But she queried, if the purpose of both are to hide information then what is the actual difference between these two? I could not give any answer to her. Before asking this question, I read other

Do objects encapsulate data so that not even other instances of the same class can access the data?

不问归期 提交于 2019-11-28 01:12:11
In Java, Do objects encapsulate data so that not even other instances of the same class can access the data? Only when the keyword "private" is used? What are "accessor methods" in Java - methods like getName()? Thanks I don't tend to think of it in terms of one object having access to another, but rather what code has access to what data within an object. In Java (and C#, btw) code within a class has access to the private members of any object of the same class. Then you've got package/assembly access and public access. The tricky one is protected access, which is sort of access to code in

Property and Encapsulation

半世苍凉 提交于 2019-11-28 00:16:44
Following is a question regarding using properties in class. I have been using public properties instead of exposing member variables publically. Majority advise that this approach helps encapsulation. However, I don’t understand the encapsulation advantage by making it a property. many people donot know the real reason for using properties. They just do it as part of coding standard. Can someone clearly explain how a property is better than public member variable and how it improves encapsulation? Encapsulation helps by insulating calling classes from changes. Let's imagine you have a simple

method without access modifier

浪尽此生 提交于 2019-11-27 21:48:21
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. 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 language; default struct access modifiers for C++ are public, where as for C#, they are private. Minyu Assuming

What is encapsulation? How does it actually hide data?

☆樱花仙子☆ 提交于 2019-11-27 20:54:40
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? There is data hiding. The second example hides how the value is stored. Also, the second example makes the value read-only for code outside the