Loading...

Differences in Java and C#: protected

View: 192    Dowload: 0   Comment: 0   Post by: admin   Category: Java Sun   Fields: Other

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 notequivalent. 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 and C# projects... Click to enlarge...

Java

Base.java

package com.example;

public class Base {
    protected int someProtectedFiled = 123;

    public void testAccessInBaseClass() {
        // SAME BEHAVIOR IN JAVA AND C#
        // The class can access its own protected field
        System.out.println(someProtectedFiled);
    }
}

Derived.java

package com.example;

public class Derived extends Base {
    public void testAccessInDerivedClass() {
        // SAME BEHAVIOR IN JAVA AND C#
        // The class can access inherited protected field
        System.out.println(someProtectedFiled);

        // DIFFERENT BEHAVIOR IN JAVA AND C#!
        // Access to protected field is possible because classes are in the same package
        // (notice access through qualifier of type Base instead of Derived)
        System.out.println(new Base().someProtectedFiled);
        // In C# the field would have to be public or protected internal 
        // (otherwise CS1540 error is produced)
    }
}

NotDerived.java

package com.example;

public class NotDerived {
    public void testAccessInNotDerivedClass() {
        // DIFFERENT BEHAVIOR IN JAVA AND C#!
        // Access to protected field is possible because classes are in the same package
        System.out.println(new Base().someProtectedFiled);
        // In C# the field would have to be public or protected internal 
        // (otherwise CS0122 error is produced)
    }
}

DerivedInAnotherPackage.java

package com.example.another;

import com.example.Base;

public class DerivedInAnotherPackage extends Base {
    public void testAccessInDerivedClassFromAnotherPackage() {
        // SAME BEHAVIOR IN JAVA AND C#
        // The class can access inherited protected field even from another package
        System.out.println(someProtectedFiled);

        // SAME BEHAVIOR IN JAVA AND C#
        // For the below to work (instead of compilation error) the field would have to be public
        // (notice access through qualifier of type Base instead of DerivedInAnotherPackage):
        // System.out.println(new Base().someProtectedFiled);
    }
}

C#

Base.cs

namespace Protected
{
    public class Base
    {
        protected int someProtectedFiled = 123;

        public void TestAccessInBaseClass()
        {
            // SAME BEHAVIOR IN JAVA AND C#
            // The class can access its own protected field            
            System.Console.WriteLine(someProtectedFiled);
        }
    }
}

Derived.cs

namespace Protected
{
    public class Derived : Base
    {
        public void TestAccessInDerivedClass()
        {
            // SAME BEHAVIOR IN JAVA AND C#
            // The class can access inherited protected field           
            System.Console.WriteLine(someProtectedFiled);

            // DIFFERENT BEHAVIOR IN JAVA AND C#!
            // For the below to work (instead of CS1540 compilation error) 
            // the field would have to be public            
            // or protected internal (notice access through qualifier of type Base instead of Derived):
            // System.Console.WriteLine(new Base().someProtectedFiled);
        }
    }
}

NotDerived.cs

namespace Protected
{
    public class NotDerived
    {
        public void TestAccessInNotDerivedClass()
        {
            // DIFFERENT BEHAVIOR IN JAVA AND C#!
            // For the below to work (instead of CS0122 compilation error) the field would
            // have to be public or protected internal:
            // System.Console.WriteLine(new Base().someProtectedFiled);
        }
    }
}

DerivedInAnotherAssembly.cs

using Protected;

namespace AnotherAssembly // Namespace doesn't matter
{
    public class DerivedInAnotherAssembly: Base
    {
        public void TestAccessInDerivedClassFromAnotherAssembly()
        {
            // SAME BEHAVIOR IN JAVA AND C#
            // The class can access inherited protected field even from another assembly            
            System.Console.WriteLine(someProtectedFiled);

            // SAME BEHAVIOR IN JAVA AND C#
            // For the below to work (instead of CS1540 compilation error) 
            // the field would have to be public
            // (notice access through qualifier of type Base instead of DerivedInAnotherAssembly):
            // System.Console.WriteLine(new Base().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).

Differences in Java and C#: protected

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.

Posted on 11-01-2018 

Comment:

To comment you must be logged in members.

Files with category

  • Simple Ordering System (Console)

    Simple Ordering System (Console)

    View: 20    Download: 0   Comment: 0

    Category: Java Sun     Fields: none

    I have my original program of this using the concept of stacks however, I find it difficult on updating the elements of the array. Instead of using the same concept, I've modified it and made it just to a static array with fixed elements.

  • Scientific Calculator –Tutorial NetBeans IDE

    Scientific Calculator –Tutorial NetBeans IDE

    View: 21    Download: 0   Comment: 0

    Category: Java Sun     Fields: none

    1. Download the zip file of SCIENTIFIC CALCULATOR–Tutorial. 2. Install your Java NetBeans IDE. 3. Open the install NetBeans. 4. Go to File. 5. Select Import project. 6. Choose from Zip ; since it is a zip file. Contact (+233) 244644700 Mail:...

  • Subnet Calculator

    Subnet Calculator

    View: 27    Download: 0   Comment: 0

    Category: Java Sun     Fields: none

    If your searching about a subnet calculator then you literally know about subnetting.

  • Very Basic Java Fighting Game

    Very Basic Java Fighting Game

    View: 70    Download: 4   Comment: 0

    Category: Java Sun     Fields: none

    Sharing you the source code of this simple fighting game created in java[eclipse]. You will learn character animation, basic interaction and some AI implementation. If you have some questions just spam me at mark[dot]cabalar[at]yahoo[dot]com. Visit...

  • Very Basic Java Game programming - Part 1

    Very Basic Java Game programming - Part 1

    View: 44    Download: 1   Comment: 0

    Category: Java Sun     Fields: none

    I don’t know how exactly to start or make an introduction for this post. I just made in to the concept of designing a simple fighting game tutorial - from doing the character animation to its overall functionality. This tutorial is intended for...

  • Recharge App (Like Paytm)

    Recharge App (Like Paytm)

    View: 46    Download: 2   Comment: 0

    Category: Java Sun     Fields: none

    Hello Dear, This is a recharge app you can recharge your mobile, electricity bill, insurance, gas bill, landline bill, etc. I have wallet functionality which you can add money in your wallet and recharge mobile using wallet amount. I have Admin...

  • Credit Card Number Verifier

    Credit Card Number Verifier

    View: 33    Download: 0   Comment: 0

    Category: Java Sun     Fields: none

    It's a JAVAFXML app that verifies if a number is a valid credit card number. It uses Luhn algorithm to verify. It also predicts the last credit card number given 15 numbers.

  • Cabin Assigner Pro

    Cabin Assigner Pro

    View: 48    Download: 0   Comment: 0

    Category: Java Sun     Fields: none

    This program allows to create and assign cabins at summer camp. There are error checking features including setting a capacity, an alert if a child is assigned to a cabin outside of their age range, it will prohibit assigned a cabin that is set for...

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd