Java and C# are very similar languages, so if you have to switch between the two, it’s easy to overlook subtle differences. One of the tricky bits is the meaning of protected
access modifier.
In C#, if you mark a field with protected
keyword, it will be available to the class that owns it and to its derived classes. In Java, access will be broader. Not only the owner and derived classes will be able to access the field but also all classes defined in the same package. In C#, similar effect can be achieved by assigning protected internal
access level. Member marked like that has access which is a union of internal
(same assembly) and protected
levels. The important thing to note is that concepts of Java package and C# assembly are not equivalent. C# assembly can span multiple namespaces and is related to physical unit (EXE, DLL) that keeps intermediate code and metadata. Package
in Java is more similar to namespace
in C# with key (not only) difference that it has an impact on accessibility…
Below are two projects that show protected
access level differences between Java and C# (both are available in this GitHub repository). C# program was made in Visual Studio 2015 Community and targets .NET 4.5.2. Java program was made in IntelliJ IDEA 15 Community Edition and is set to use Java 8. Version of Java/.NET is not relevant and there’s nothing special in the projects - could’ve been easily done in Notepad but isn’t it great that these days, we can get such awesome IDEs for free? :)
Java
Base.java
package com.example;
public class Base {
protected int someProtectedFiled = 123;
public void testAccessInBaseClass() {
System.out.println(someProtectedFiled);
}
}
Derived.java
package com.example;
public class Derived extends Base {
public void testAccessInDerivedClass() {
System.out.println(someProtectedFiled);
System.out.println(new Base().someProtectedFiled);
}
}
NotDerived.java
package com.example;
public class NotDerived {
public void testAccessInNotDerivedClass() {
System.out.println(new Base().someProtectedFiled);
}
}
DerivedInAnotherPackage.java
package com.example.another;
import com.example.Base;
public class DerivedInAnotherPackage extends Base {
public void testAccessInDerivedClassFromAnotherPackage() {
System.out.println(someProtectedFiled);
}
}
C#
Base.cs
namespace Protected
{
public class Base
{
protected int someProtectedFiled = 123;
public void TestAccessInBaseClass()
{
System.Console.WriteLine(someProtectedFiled);
}
}
}
Derived.cs
namespace Protected
{
public class Derived : Base
{
public void TestAccessInDerivedClass()
{
System.Console.WriteLine(someProtectedFiled);
}
}
}
NotDerived.cs
namespace Protected
{
public class NotDerived
{
public void TestAccessInNotDerivedClass()
{
}
}
}
DerivedInAnotherAssembly.cs
using Protected;
namespace AnotherAssembly
{
public class DerivedInAnotherAssembly: Base
{
public void TestAccessInDerivedClassFromAnotherAssembly()
{
System.Console.WriteLine(someProtectedFiled);
}
}
}
I hope that comments in the code make everything clear and this dry topic is exhausted... I've just ordered USB shield for my Arduino so if it works, the next post will be about moving this thing with PlayStation controller. :)
Update (2016-03-14)
Unfortunately, I don’t have time to write the post mentioned above, but the good news is that you definitely can use SparkFun USB Host Shield with USB Host Shield 2.0 library to steer servos with PS3 controller. :)
Here’s a short video of how it worked on my paintball turret. And here’s a clip of test circuit (hardware diagram).