Java vs. C# access modifiers looked at by a C# programmer

I’m looking into the SCJP 6 as I don’t have hands on experience on Java projects (only academic apps).

I had bookmarked long time ago a great C# vs. Java comparison Dave Obasanjo made:

A COMPARISON OF MICROSOFT’S C# PROGRAMMING LANGUAGE TO SUN MICROSYSTEMS’ JAVA PROGRAMMING LANGUAGE



I noticed tonight that the member’s access modifiers could use some tuning and show:

C# access modifier

Java access modifier

private

private

public

public

internal

Default (package-private)

protected

N/A

internal protected

protected



In Java, a protected member can only be accessed through classes on the same package and through subclasses whether they are on the same package or not.

The protected modifier specifies that the member can only be accessed within its own package (as with package-private or default) and, in addition, by a subclass of its class in another package. Package + Kids access.

The default access modifier in Java happens when a class member has no modifier (the default is also known as package-private). This means the member is only accessible by a class defined within the package.

Adding an empty item to an IList collection at runtime.

How can I add a new empty item to a datasource (BindingSource) if the collection that it contains has objects of unknown type at design time:

if (dataSource.GetType() == typeof(BindingSource))
{
IEnumerator en = dataSource.GetEnumerator();
en.MoveNext();
Type t = en.Current.GetType();

object o = System.Activator.CreateInstance(t, false);

dataSource.Add(o);
}

where dataSource is of type IList.

This was a request to add an empty line to a windows forms combobox and to a listbox.

BindingSource can be bound to any of the following:

  • Object
  • System.Type
  • IEnumerable
  • ICollection
  • IList
  • IListSource
  • IBindingList
  • IBindingListView