<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Daily Morn by Raymond Li &#187; Java</title>
	<atom:link href="http://rayli.net/blog/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://rayli.net/blog</link>
	<description></description>
	<lastBuildDate>Sun, 22 Jan 2012 02:52:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Why does an abstract class need to implement interface methods?</title>
		<link>http://rayli.net/blog/2008/09/why-does-an-abstract-class-need-to-implement-interface-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=why-does-an-abstract-class-need-to-implement-interface-methods</link>
		<comments>http://rayli.net/blog/2008/09/why-does-an-abstract-class-need-to-implement-interface-methods/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 15:28:17 +0000</pubDate>
		<dc:creator>Raymond Li</dc:creator>
				<category><![CDATA[Popular]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://rayli.net/blog/?p=77</guid>
		<description><![CDATA[In a comment for a previous post (Top 10 differences between Java and C#), John P. Wood wrote: As a (primarily) Java developer, I’ve also noticed that C# handles abstract classes that implement interfaces differently. In Java, an abstract class can implement an interface, and not provide implementations of all of the interface’s methods.  It [...]]]></description>
			<content:encoded><![CDATA[<p>In a comment for a previous post (<a title="Top 10 differences between Java and C#" href="http://rayli.net/blog/2008/04/top-10-differences-between-java-and-c/">Top 10 differences between Java and C#</a>), John P. Wood wrote:</p>
<blockquote><p>As a (primarily) Java developer, I’ve also noticed that C# handles abstract classes that implement interfaces differently. In Java, an abstract class can implement an interface, and not provide implementations of all of the interface’s methods.  It is the responsibility of the first concrete class that has that abstract class as an ancestor to implement all of the methods in the interface.</p>
<p><span id="more-77"></span>C# on the other hand seems to require that the abstract class provide implementations for all of the methods on the interface. Even if that implementation is just a method signature.</p></blockquote>
<p>Like these 3 posts (<a title="Interfaces (C# Programming Guide)" href="http://msdn.microsoft.com/en-us/library/ms173156.aspx" target="_self">1</a> (bottom), <a title="Abstract/Interface question" href="http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/f1f8106c-d2e8-436c-9ee9-6b3e6731d185/" target="_self">2</a>, <a title="Why do abstract classes need to   implement interface members?" href="http://jelle.druyts.net/CommentView.aspx?guid=fe5e801e-71e5-474e-8235-01b2ad14ff68" target="_self">3</a>), I am struggling with why the Microsoft Team chose to do this.  Why ask the developer to write a little stub for the interface method, when the abstract class has no intention of implementing it at all?</p>
<p>I believe it&#8217;s a natural artifact of virtual and non-virtual methods in C# (<a title="Gotcha #2 - I can’t override!?" href="http://rayli.net/blog/2008/04/top-10-differences-between-java-and-c/#override">gotcha #2</a>).  In Java, methods are virtual by default (as far as I&#8217;m aware, Java doesn&#8217;t support non-virtual methods).  However, C# (being derived from C++) does support non-virtual methods.  When an abstract class implements an interface in C#, you are given the chance to describe the interface methods as abstract, virtual or non-virtual.  Here&#8217;s exactly what each options means:</p>
<ol>
<li><strong>abstract </strong>- No attempt at an implementation is made in the abstract class.  It&#8217;s up to the first concrete class to provide an implementation.  As a side note, defining a method as <em>abstract </em>implicitly defines it as <em>virtual</em>.  If this weren&#8217;t the case, you wouldn&#8217;t be able to override it in the child class which defeats the purpose of an abstract method.</li>
<li><strong>virtual </strong>- An attempt was made in the abstract class to implement this method, but the child class has the option of overriding it and providing its own implementation.</li>
<li><strong>non-virtual</strong> &#8211; Again, an attempt was made at an implementation in the abstract class.  However, the child class cannot override this method.  If a child class defines a method with the same name, the method will not be associated with the interface implemented in the abstract class.  The method associated with the interface is the <em>non-virtual</em> method defined by the abstract class.</li>
</ol>
<p>Java can do options 1 and 2, but it lacks option 3.  Java could define the method as <em>final</em> to prevent it from being overriden, but this is slightly different than <em>non-virtual</em>.  The <em>final </em>keyword is more along the lines of <em>seal</em> in C#.  By allowing an option 3, C# provides finer control over how interfaces are inherited.  If C# went the route of Java and permitted abstract classes to put off interface method definitions, these methods would need to be <em>virtual </em>by default.  However, this would be inconsistent with C# methods being <em>non-virtual</em> by default.</p>
<p>In a nutshell, the C# developer is forced in the abstract class to decide exactly which of the three options to assign to the interface method.  Anyway, that&#8217;s my best guess&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://rayli.net/blog/2008/09/why-does-an-abstract-class-need-to-implement-interface-methods/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Top 10 differences between Java and C#</title>
		<link>http://rayli.net/blog/2008/04/top-10-differences-between-java-and-c/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=top-10-differences-between-java-and-c</link>
		<comments>http://rayli.net/blog/2008/04/top-10-differences-between-java-and-c/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 22:10:25 +0000</pubDate>
		<dc:creator>Raymond Li</dc:creator>
				<category><![CDATA[Popular]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://rayli.net/blog/?p=56</guid>
		<description><![CDATA[My latest transition from Java to C# left me scratching my head and scrambling to find the differences.  Don&#8217;t get me wrong &#8212; they are very similar, but some key syntax and philosophical differences set these two languages apart.  Below are my top 10 differences that I wish someone told me before I pulled out [...]]]></description>
			<content:encoded><![CDATA[<p>My latest transition from Java to C# left me scratching my head and scrambling to find the differences.  Don&#8217;t get me wrong &#8212; they are very similar, but some key syntax and philosophical differences set these two languages apart.  Below are my top 10 differences that I wish someone told me before I pulled out yet more hair.</p>
<p><a href="#standard-output"><span id="more-56"></span>Gotcha #10 &#8211; Give me my standard output!</a><br />
<a href="#namespaces">Gotcha #9 &#8211; Namespaces == Freedom</a><br />
<a href="#super">Gotcha #8 &#8211; What happened to <em>super</em>?</a><br />
<a href="#constructors">Gotcha #7 &#8211; Chaining constructors to a base constructor</a><br />
<a href="#inherit">Gotcha #6 &#8211; Dagnabit, how do I inherit?</a><br />
<a href="#constants">Gotcha #5 &#8211; Why don&#8217;t constants remain constant?</a><br />
<a href="#hashtable">Gotcha #4 &#8211; Where is ArrayList, Vector or Hashtable?</a><br />
<a href="#properties">Gotcha #3 &#8211; Of Accessors and Mutators (Getters and Setters)</a><br />
<a href="#override">Gotcha #2 &#8211; I can&#8217;t override!?</a><br />
<a href="#objects">And the #1 gotcha&#8230;</a></p>
<h3><a name="standard-output"></a>Gotcha #10 &#8211; Give me my standard output!</h3>
<p>This may not seem like a big deal, but when I&#8217;m first getting my head in a language, I want to be able to debug.  With everything so new and shiny, I don&#8217;t want to hear about the debugger yet, I don&#8217;t care about the fancy message boxes, just tell me how to get something on standard output!</p>
<p>In C#, the code looks like this:</p>
<pre class="brush: csharp; title: ; notranslate">Console.WriteLine(&quot;your string here&quot;);</pre>
<h3><a name="namespaces"></a>Gotcha #9 &#8211; Namespaces ==  Freedom</h3>
<p>In Java, the package hierarchical structure mirrored the directory hierarchical structure.  This made sense to a degree, but then why didn&#8217;t Sun just auto-derive the package structure?  Anyway, Microsoft has freed us from this constraint.  The C# package structure is defined using namespaces (just like Java), but the namespaces do NOT have to reflect the directory structure.</p>
<h3><a name="super"></a>Gotcha #8 &#8211; What happened to <em>super</em>?</h3>
<p>Slight renaming of keywords drives me bonkers!  Substitute Java&#8217;s <em>super</em> keyword with <em>base</em>.</p>
<h3><a name="constructors"></a>Gotcha #7 &#8211; Chaining constructors to a base constructor</h3>
<p>In Java, I often created a set of constructors with differing parameters that all reference a common all-powerful constructor.  In other words, I created a set of overloaded constructors that basically just called a constructor that did the bulk of the work.  This is called constructor chaining and normally leads to easier to maintain code.  In Java, the <em>this(&#8230;)</em> statement is used to call the constructor.  In C#, this is done right up in the method signature.</p>
<pre class="brush: csharp; title: ; notranslate">public MyConstructor(int i) : this(i, -1)
{
    ...
}

public MyConstructor(int i, int j)
{
    ...
}
</pre>
<h3><a name="inherit"></a>Gotcha #6 &#8211; Dagnabit, how do I inherit?</h3>
<p>In Java, the keyword <em>extends </em>is used to inherit from a parent class, and the keyword <em>implements </em>is used to inherit an interface.  In C#, the distinction does not exist and it&#8217;s all done in the method signature.</p>
<pre class="brush: csharp; title: ; notranslate">public class MyClass : MyParentClass
{
    ...
}</pre>
<h3><a name="constants"></a>Gotcha #5 &#8211; Why don&#8217;t constants remain constant?</h3>
<p>Well of course they do!  Just not among different languages.  In Java, I normally defined global constants using <em>public static final</em>.  In C#, the <em>static</em> keyword does exist (<em>final</em> does not), but you can define constants like this:</p>
<pre class="brush: csharp; title: ; notranslate">public const MyConstant = 101;</pre>
<p>When the constant needs to be initialized at run-time (for example in a constructor), use the <em>readonly</em> keyword instead of <em>const</em>.</p>
<h3><a name="hashtable"></a>Gotcha #4 &#8211; Where is ArrayList, Vector or Hashtable?<strong><br />
</strong></h3>
<p>In Java, these guys were my staples.  Pre-built dynamic arrays and look-up tables decrease development time and makes my life a whole lot easier, but where are they in C#? C# has an <em>ArrayList</em>, but it doesn&#8217;t make use of generics. Instead, I like to use:</p>
<pre class="brush: csharp; title: ; notranslate">System.Collections.Generic.List&lt;&gt;</pre>
<p>Unfortunately, <em>List&lt;&gt;</em> is not thread-safe (C#&#8217;s <em>ArrayList</em> and Java&#8217;s <em>Vector</em> are thread-safe). C# also has a <em>Hashtable</em>; the generic version is:</p>
<pre class="brush: csharp; title: ; notranslate">System.Collections.Generic.Dictionary&lt;&gt;</pre>
<h3><a name="properties"></a>Gotcha #3 &#8211; Of Accessors and Mutators (Getters and Setters)</h3>
<p>As long as methods exist in classes, accessors and mutators will exist.  However, C# largely replaces them with class properties. Instead of the traditional <em>getSomeInteger()</em> and <em>setSomeInteger()</em>, we use properties!</p>
<pre class="brush: csharp; title: ; notranslate">public class SomeClass
{
    int someInteger = 42;

    public int SomeInteger
    {
        get { return this.someInteger; }
        set { this.someInteger = value; }
    }

    public void Main(string[] args)
    {
        SomeClass c = new SomeClass();
        Console.WriteLine(c.SomeInteger);
        c.SomeInteger = 420;
    }
}</pre>
<p><em>this.someInteger</em> is a class variable, and <em>value</em> is the new integer.</p>
<h3><a name="override"></a>Gotcha #2 &#8211; I can&#8217;t override!?</h3>
<p>At this point, we&#8217;ve transcended the syntax differences and we&#8217;ve entered into more philosophical choices that the Microsoft team made. In Java, all methods are by default virtual and you can override them. In C#, all methods are non-virtual. To override a method in the parent class, make sure the method of the parent class is defined as virtual using the <em>virtual</em> keyword.</p>
<pre class="brush: csharp; title: ; notranslate">class MyParentClass
{
    public virtual void MyMethod() { ... }
}</pre>
<p>In the child class, the method must use the <em>override</em> keyword.</p>
<pre class="brush: csharp; title: ; notranslate">class MyChildClass : MyParentClass
{
    public override void MyMethod() { ... }
}</pre>
<h3><a name="objects"></a>And the #1 gotcha&#8230;</h3>
<p>I remember when I first learned Java, and everyone was saying how Java made everything an object. How Java forced you to make classes for everything. This turns out to be partially true. It is necessary to make classes.  However, primitives are not objects. In C#, even what were considered primitives in Java are objects in C#.  Absolutely everything descends from System.Object.  This choice by the C# team allows for greater consistency and for such things as a cleaner implementation of automatic boxing/unboxing.</p>
]]></content:encoded>
			<wfw:commentRss>http://rayli.net/blog/2008/04/top-10-differences-between-java-and-c/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Project Java 2000</title>
		<link>http://rayli.net/blog/2000/02/project-java-2000/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=project-java-2000</link>
		<comments>http://rayli.net/blog/2000/02/project-java-2000/#comments</comments>
		<pubDate>Mon, 28 Feb 2000 14:05:57 +0000</pubDate>
		<dc:creator>Raymond Li</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[SUNYSB]]></category>

		<guid isPermaLink="false">http://rayli.net/blog/?p=9</guid>
		<description><![CDATA[Project Java 2000 &#8211; Chemistry Team is the first step towards a rapidly, distributable system for the Chemistry student as well as the teacher. The system is comprised of activities and tools for developing the Chemistry student&#8217;s skills as a learner while learning introductory college Chemistry.]]></description>
			<content:encoded><![CDATA[<p><a href="http://rayli.net/lisa">Project Java 2000 &#8211;  Chemistry Team</a> is the first step towards a rapidly, distributable  system for the Chemistry student as well as the teacher. The system is  comprised of activities and tools for developing the Chemistry student&#8217;s  skills as a learner while learning introductory college Chemistry.</p>
<p><span id="more-9"></span></p>
]]></content:encoded>
			<wfw:commentRss>http://rayli.net/blog/2000/02/project-java-2000/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

