encapsulation

Why protected method is not accessible from subclass?

狂风中的少年 提交于 2020-12-01 06:48:48
问题 Consider the following code snippets: package vehicle; public abstract class AbstractVehicle { protected int speedFactor() { return 5; } } package car; import vehicle.AbstractVehicle; public class SedanCar extends AbstractVehicle { public static void main(String[] args) { SedanCar sedan = new SedanCar(); sedan .speedFactor(); AbstractVehicle vehicle = new SedanCar(); // vehicle //WON'T compile // .speedFactor(); } } SedanCar is a subclass of AbstractVehicle which contains a protected method

Apply CSS dynamically with JavaScript

白昼怎懂夜的黑 提交于 2020-08-02 06:17:40
问题 What is a good way to apply styling dynamically (i.e. the value of styles are created at runtime) to HTML elements with JavaScript? I'm looking for a way to package a JavaScript widget (JS, CSS and markup) in a single component (basically, an object). The idea would be for the component to encapsulate styling (so users have a nice API to modify it instead of a more tightly coupled approach of modifying the CSS directly and having changes applied indirectly). The problem is that a single API

What are the differences between the private keyword and private fields in TypeScript?

送分小仙女□ 提交于 2020-07-17 07:10:58
问题 In TypeScript 3.8+, what are the differences between using the private keyword to mark a member private: class PrivateKeywordClass { private value = 1; } And using the # private fields proposed for JavaScript: class PrivateFieldClass { #value = 1; } Should I prefer one over the other? 回答1: Private keyword The private keyword in TypeScript is a compile time annotation. It tells the compiler that a property should only be accessible inside that class: class PrivateKeywordClass { private value =

What are the differences between the private keyword and private fields in TypeScript?

只谈情不闲聊 提交于 2020-07-17 07:10:47
问题 In TypeScript 3.8+, what are the differences between using the private keyword to mark a member private: class PrivateKeywordClass { private value = 1; } And using the # private fields proposed for JavaScript: class PrivateFieldClass { #value = 1; } Should I prefer one over the other? 回答1: Private keyword The private keyword in TypeScript is a compile time annotation. It tells the compiler that a property should only be accessible inside that class: class PrivateKeywordClass { private value =

Does a public static const variable break the encapsulation ideology?

笑着哭i 提交于 2020-07-09 12:14:26
问题 I always seem to struggle with the decision of whether a value in a class which should be static and const should be public or private with a static public method for access. class DeepThought { public: static const int TheAnswer = 42; }; versus: class DeepThought { public: static int GetTheAnswer() { return TheAnswer; } private: static const int TheAnswer = 42; }; I want to do it the first way, but somewhere deep inside me feels like it breaks encapsulation even though its a constant value.

How to sort a list by a private field?

♀尐吖头ヾ 提交于 2020-04-29 10:23:06
问题 My entity class looks like this: public class Student { private int grade; // other fields and methods } and I use it like that: List<Student> students = ...; How can I sort students by grade , taking into account that it is a private field? 回答1: You have these options: make grade visible define a getter method for grade define a Comparator inside Student make Student implement Comparable use reflection (in my opinion this is not a solution , it is a workaround / hack ) Example for solution 3

How to sort a list by a private field?

こ雲淡風輕ζ 提交于 2020-04-29 10:22:17
问题 My entity class looks like this: public class Student { private int grade; // other fields and methods } and I use it like that: List<Student> students = ...; How can I sort students by grade , taking into account that it is a private field? 回答1: You have these options: make grade visible define a getter method for grade define a Comparator inside Student make Student implement Comparable use reflection (in my opinion this is not a solution , it is a workaround / hack ) Example for solution 3

In what cases should public fields be used instead of properties? [duplicate]

痞子三分冷 提交于 2020-02-06 03:58:08
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Public Data members vs Getters, Setters In what cases should public fields be used, instead of properties or getter and setter methods (where there is no support for properties)? Where exactly is their use recommended, and why, or, if it is not, why are they still allowed as a language feature? After all, they break the Object-Oriented principle of encapsulation where getters and setters are allowed and

How to choose between private and protected access modifier to encapsulate members between base and childs classes?

孤者浪人 提交于 2020-01-30 04:04:32
问题 I am trying on a project to use private values in my internal functions. In past I used only public ones, but I noticed that obfuscation is working much better when using as much as possible private parameters. My question is regarding Parent/Child classes. In my main class I define all the parameters as following : public class MyFatherClass { private long id = -1; public long ID { get { return this.id; } set { this.id = value; } } ... } So in all internal functions I access to my private

Why can't the VBA Me keyword access private procedures in its own module?

痞子三分冷 提交于 2020-01-28 10:28:25
问题 I just discovered that the Me keyword cannot access private procedures even when they are inside its own class model. Take the following code in Class1: Private Sub Message() Debug.Print "Some private procedure." End Sub Public Sub DoSomething() Me.Message End Sub This code instantiates an instance of the class: Sub TestClass() Dim objClass As New Class1 objClass.DoSomething End Sub Me.Message throws compile error "Method or data member not found." If I change Private Sub Message() to Public