New answers tagged

0

You mentioned Swift explicitly, so I'll answer about why Swift doesn't have protected. Unlike many other languages, Swift lets you write "extensions" to other types (classes, structs, enums and protocols alike), even those which you don't own. Such extensions can even allow you to making library A's type conform to library B's protocol (an example of "...


0

As Flater wrote, acces restrictions are not strictly needed. And some argue that protected access is trying to do multiple things at once. You can use protected in case like: Method should be called by subclass methods Method should be implemented by subclass methods and will be called by superclass or other subclasses Method that can be ovveriden by ...


1

Most combinators allow some exclusion sort of parser: "accept all characters except \" It then becomes pretty straight-forward to make something where your start/end are one parser and "everything else" is another, combined within zero-or-more(either()) sort of construct. Where it might fall down is if you require the start/end symbols to be nested. Once ...


1

protected is about access control of data. OOP is about encapsulation. The main goal of OOP is to structure the code such that entities (data+operations on it) are weakly coupled to each others. The fact that encapsulated data are controlled (relatively to their access) or not is not a necessary concern. Protection is more closely linked to inheritance; one ...


45

It depends on what you mean by "required". Access modifiers are not a necessity. You could replace every access modifier with public and most applications will work just like they did when you used varied access modifiers, proving the point that the compiler's main goal (outputting a working application) is not directly dependent on access modifiers. As ...


8

Before we decide that the protected access modifier must be removed from all popular OO languages I would like to point out that it would be pretty inconvenient to lose it. In abstract base classes that serve as a blueprint for a number of derived classes you will likely have a lot of support methods for these derivatives that will be meaningless to the end ...


3

One of the first object oriented languages, Smalltalk, does not have a protected keyword or mechanism, and private also isn't explicit but implied for instance variables, and suggested by convention for methods. Works pretty well unless people see the malleability as an invitation to hit everything with a big hammer :-)


32

No, it's not required: Bjarne Stroustrup, explained how he naively added protected to C++ release 1.2, thinking to provide a useful feature to class developers, just to conclude only 5 years later that it was a nasty source of bugs, that fortunately no one was forced to use. Nowadays, he recommends not to use it. The practical arguments against ...


0

In Swift, it was decided that a subclass is not significantly related to the base class. If some information is not available to the public, it should not be available to a subclass. There is also “fileprivate” which allows members to be available within a file only, so if classes are strongly related, they can be implemented in one file.


9

Python is also a language that strongly adheres to the object oriented programming approach. It uses the classical approach of classes and objects. The thing to remember however is that any "word" is just a contract between you and (future) maintainers. Having a different, or even non explicit name for something does not mean that this contract is not there....


2

C and C++ may be used to write code for things like thermostat controllers, blinkie-light toys, and other devices that don't have any kind of console, file system, or other such features, and may only have a few hundred bytes of RAM available, if that (the code itself is often stored in read-only memory or flash). While it's useful to have the Standard ...


0

It took me a LONG time to truly grok that there will never truly be a best language. For a programming team the most important aspects are that the language be well known, supported by many tools, should have minimum language syntax and should surprise you as rarely as possible. For a single coder a powerful language that allows quick test/run cycles is ...


3

Rule of thumb: Errors are proportional to the time it takes to read code out loud. Anything that increases the number of open bracket, close bracket, open curly brace, close curly brace, open parenthesis, close parenthesis... will increase the number of errors in the code. This is one reason why * is star or splat, and not asterisk. # is shhh, ! is bang. ...


14

One relatively well-known example is of some Fortran code in which a single typo completely changed the meaning of the code. It was intended to repeat a section of code 100 times (with I as the loop counter): DO 10 I = 1,100 However, the comma was mistyped as a dot: DO 10 I = 1.100 Because Fortran allows spaces in identifiers (and because it ...


59

Is it a bad design for a programming language to allow spaces in identifiers? Short answer: Maybe. Slightly longer answer: Design is the process of identifying and weighting conflicting solutions to complex problems, and making good compromises that meet the needs of stakeholders. There is no "bad design" or "good design" except in the context of the ...


6

It is not inherently bad design to allow spaces in symbol names. This can be shown with a simple counter-example. Kotlin allows spaces in names. It also has official coding conventions which state when it is ok to use this feature: Names for test methods In tests (and only in tests), it's acceptable to use method names with spaces enclosed in ...


8

Is it a bad design for a programming language to allow spaces in identifiers? You forgot important implementation details: what is source code for you? I like the FSF definition of it: the preferred form on which developers work. It is a social definition, not a technical one. In some languages and their 1980s implementation (think of original SmallTalk ...


102

Consider the following. var [Example Number] = 5; [Example Number] = [Example Number] + 5; print([Example Number]); int[] [Examples Array] = new int[25]; [Examples Array][[Example Number]] = [Example Number] Compare it with the more traditional example: var ExampleNumber = 5; ExampleNumber = ExampleNumber + 5; print(ExampleNumber); int[] ...


-3

because the array is covariant which every type is a subclass of the object so this gives an error in run time due to casting exception. while the generic is invariant so when it builds on the type ensure or type-safe so if the type not like it creates type it gives a compiler error.


Top 50 recent answers are included