<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>engin güller</title>
	
	<link>http://www.eguller.com</link>
	<description>Hope is a good thing, maybe the best of things, and no good thing ever dies.</description>
	<lastBuildDate>Sun, 30 May 2010 20:06:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/enginguller" /><feedburner:info uri="enginguller" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>A Development Environment Survey</title>
		<link>http://feedproxy.google.com/~r/enginguller/~3/7EkU9p3A-9U/</link>
		<comments>http://www.eguller.com/?p=550#comments</comments>
		<pubDate>Sun, 30 May 2010 20:05:24 +0000</pubDate>
		<dc:creator>engin</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.eguller.com/?p=550</guid>
		<description><![CDATA[We are using Clearcase as source control management, Clarify CRM as issue tracking system in company. We are writing code mostly in eclipse and making builds with ant.  We have made  a what if scenario last week in company.  What can we use instead of Clearcase and Clarify CRM is there any good eclipse and [...]]]></description>
			<content:encoded><![CDATA[<p>We are using Clearcase as source control management, Clarify CRM as issue tracking system in company. We are writing code mostly in eclipse and making builds with ant.  We have made  a what if scenario last week in company.  What can we use instead of Clearcase and Clarify CRM is there any good eclipse and ant alternatives. Code review is also vital for us. We are using a in house developed code review tool. It is tightly integrated with clearcase. Developer add activity ids and send review request to architect, architect add comments etc finally architect approve code changes when everything is ok. Finally developer merge changes.</p>
<p><strong>Issue Tracking Tools</strong></p>
<p><span style="text-decoration: underline;">Trac</span><br />
<a href="trac.edgewall.org" target="_blank"> Trac</a> is a web based project management system, it simple and easy to use. Trac is written in Python. I had a bit hard time while installing trac but if I can install everybody can so I will not say &#8220;installation is hard, it is minus for trac&#8221;.<br />
Trac does not support multiple project directly and it is important for us. I have learnt that people solved this problem by adding some custom code to trac. But adding custom code needs developer to write and somebody maintain it for this reason I found it a drawback of trac.<br />
Subversion integration of trac is very good. Integrated wiki also great actually trac&#8217;s itself is a wiki. We are using Opentext&#8217;s LiveLink for document management. Actually we are writing design documents, feature documents in Microsoft Word and we are keeping them in LiveLink. Writing all design documents in wiki would be great for us because you can make links to issues or subversion commits in your wiki too. Making search in a wiki (html) is more effective than making search in lots of word documents. You can not make link from a word document to some page of another word document. Making all of these are very easy with html so with trac&#8217;s wiki.<br />
Trac has also plugin api. You can extend trac by plugins. There are a lot of trac plugin. Some of them still under development. Stable ones become a part of main trac distribution.<br />
I have read lots of comments about trac and a common opinion for trac is, trac is best for small size projects. I don&#8217;t know why but maybe trac does not scale well when project size increased. We are more than one hundred developer.</p>
<p><span style="text-decoration: underline;">Bugzilla<br />
</span> <a href="http://www.bugzilla.org/" target="_blank">Bugzilla</a> is developed by Mozilla Foundation. Eclipse project, Apache Foundation (moving JIRA) and Mozilla Foundation (creator of Firefox and Thunderbird &#8230;) are notable users of Bugzilla.  Bugzilla is developed since 1998 so it very mature and robust issue tracking tool. Bugzilla does not have integrated wiki or source control explorer like trac. Bugzilla is just a bug tracker but good one.  You can integrate Bugzilla with subversion by using SCM Bug. I have tried SCM Bug and it worked well. You can link subversion commit with a Bugzilla issue if you setup SCM Bug. Commit comments and changes goes under issue as comment in Bugzilla. I have liked Bugzilla its user interface is a bit weird but who cares, you can integrate Bugzilla with eclipse by using Mylyn plugin you don&#8217;t need to open Bugzilla user interface.</p>
<p><span style="text-decoration: underline;">JIRA<br />
</span> Atlassian JIRA is awesome. It is highly customizable, you can easily create a new workflow which satisfies your needs. JIRA is free for open source projects (JIRA cathed a great momentum by this decision) you have to pay for commercial needs. It is 8000$ annually for +100 developer but I did not found it high. It seems we need some custom development to satisfy our needs with Trac or Bugzilla. We have to hire someone for this or we have to do it ourself but I am sure it will cost more than 8000$ to modify and maintain Bugzilla and Trac. Atlassian also has other tools which integrates well with JIRA. For example Confluence for document management, wiki etc. Crubicle for code review. It is another plus for JIRA.</p>
<p>My choice:</p>
<ol>
<li>JIRA</li>
<li>Bugzilla</li>
<li>Trac</li>
</ol>
<p>I will continue survey in Part-2. Version Control, IDE and Build Tools are subject of Part-2.</p>
<img src="http://feeds.feedburner.com/~r/enginguller/~4/7EkU9p3A-9U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.eguller.com/?feed=rss2&amp;p=550</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.eguller.com/?p=550</feedburner:origLink></item>
		<item>
		<title>Programcının Oyunu</title>
		<link>http://feedproxy.google.com/~r/enginguller/~3/q8wiQS2shkA/</link>
		<comments>http://www.eguller.com/?p=546#comments</comments>
		<pubDate>Thu, 13 May 2010 18:11:17 +0000</pubDate>
		<dc:creator>engin</dc:creator>
				<category><![CDATA[Daily Life]]></category>

		<guid isPermaLink="false">http://www.eguller.com/?p=546</guid>
		<description><![CDATA[Bir programcı ve bir mühendis uzun bir uçak yolculuğunda yan yana oturuyorlarmış. Programcı mühendise eğlenceli bir oyun oynamak ister misin diye sormuş? Mühendis uyumak istediği için programcının teklifini kibarca redditmiş ve dönüp uyumaya çalışmış. Programcı oyunun çok eğlenceli olduğunu söylemiş oynamaları konusunda ısrar etmiş ve oyunun kurallarını açıklamaya başlamış.
&#8220;Sana bir soru soracağım eğer bilemezsin bana [...]]]></description>
			<content:encoded><![CDATA[<p>Bir programcı ve bir mühendis uzun bir uçak yolculuğunda yan yana oturuyorlarmış. Programcı mühendise eğlenceli bir oyun oynamak ister misin diye sormuş? Mühendis uyumak istediği için programcının teklifini kibarca redditmiş ve dönüp uyumaya çalışmış. Programcı oyunun çok eğlenceli olduğunu söylemiş oynamaları konusunda ısrar etmiş ve oyunun kurallarını açıklamaya başlamış.<br />
&#8220;Sana bir soru soracağım eğer bilemezsin bana 5$ vereceksin, daha sonra sen bana soracaksın ben bilemezsem sana 5$ ödeyeceğim.&#8221; Mühendis teklifi tekrar kibarca redditmiş ve tekrar uyumaya çalışmış. Programcı ısrar etmiş &#8220;Peki sen soruyu bilemezsen bana 5$ ver eğer ben bilemezsem sana 50$ vereceğim demiş.&#8221; Bu teklif mühendisin dikkatini çekmiş ve oyunu oynamadan programcıdan kurtulamayacığını anlamış ve oyunu oynamayı kabul etmiş. İlk soruyu programcı sormuş<br />
&#8220;Dünya ile ay arasındaki uzaklık nedir ?&#8221;<br />
Mühendis hiç cevap vermeden cüzdanından 5$ çıkarıp programcıya vermiş. Soru sorma sırası mühendise gelmiş.<br />
&#8220;Bir yokuşa üç bacak ile çıkıp dört bacak ile inen şey nedir?&#8221; diye sormuş mühendis.<br />
Programcı bilgisayarını çıkarıp internete bağlanmış ve sorunun cevabını aramış. İnternet üzerinden kütüphanede arama yapmış yine bulamamış arkadaşlarına mail atmış fakat bir cevap bulamamış. Bir saatlik bir uğraş sonucu pes etmiş ve mühendise 50$ vermiş.<br />
Mühendis 50$ almış ve uyumaya devam etmiş. Programcı duruma bozulmuş ve mühendisi uyandırıp &#8220;Peki sorunun cevabı neydi ?&#8221; diye sormuş.<br />
Mühendis cebinden 5$ daha çıkarıp programcıya vermiş ve uykusuna kaldığı yerden devam etmiş.</p>
<img src="http://feeds.feedburner.com/~r/enginguller/~4/q8wiQS2shkA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.eguller.com/?feed=rss2&amp;p=546</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.eguller.com/?p=546</feedburner:origLink></item>
		<item>
		<title>Yazılım Projelerinin Zaman Planmasında 12 Problem</title>
		<link>http://feedproxy.google.com/~r/enginguller/~3/mL41rEMZR-0/</link>
		<comments>http://www.eguller.com/?p=541#comments</comments>
		<pubDate>Tue, 20 Apr 2010 14:14:45 +0000</pubDate>
		<dc:creator>engin</dc:creator>
				<category><![CDATA[Career]]></category>
		<category><![CDATA[Productivity]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Project Planning]]></category>

		<guid isPermaLink="false">http://www.eguller.com/?p=541</guid>
		<description><![CDATA[Bir yazilim projesinde zaman planlamasi yapmak zor bir istir, fakat hala çogu firma bu zaman planlamalarina dayanarak projelerini yürütmeye devam ederler. Bu yazida yazilim projelerinde yapilan zaman planlamalarinda yapilan en temel 12 hataya dikkat çekmeye çalisacagim.

Bilinmeyen üzerine plan yapmak imkansizdir
Çogu yazilim birbirinden farklidir, ve her projede neredeyse daha önce hiç yapilmamis bir ürün gelistirilir. Projede [...]]]></description>
			<content:encoded><![CDATA[<p>Bir yazilim projesinde zaman planlamasi yapmak zor bir istir, fakat hala çogu firma bu zaman planlamalarina dayanarak projelerini yürütmeye devam ederler. Bu yazida yazilim projelerinde yapilan zaman planlamalarinda yapilan en temel 12 hataya dikkat çekmeye çalisacagim.</p>
<ol style="padding-left: 5px;">
<li><strong>Bilinmeyen üzerine plan yapmak imkansizdir</strong></li>
<p>Çogu yazilim birbirinden farklidir, ve her projede neredeyse daha önce hiç yapilmamis bir ürün gelistirilir. Projede yapilacaklar belirli olsa bile, karmasik bir sistemi gelistirmek istenen belli olmasina ragmen bile zordur.<br />
Bir program bir çok farkli yöntem ile yapilabilir. Zaman planlamasi asamasinda net olarak belirlenemeyen dizayn seçimleri, bir yazilimin bitis zamanini dogrudan etkiler.<br />
Yapilan zaman planlarinin, istenildigi sekilde yürüyebilmesi için kabul görmüs belli basli yöntemler vardir fakat, en iyi yöntemle bile yapilan plan, yazilim gelistirme aktif olarak baslamadigi için öngörü ve varsayimdan ibarettir.</p>
<li><strong>Yapilan Planlar Genelde Iyimserdir</strong></li>
<p>Çogu programci planlarini oldukça iyimser varsayimlar üzerine yapar. Proje basladiginda daha üretken olacaklarini düsündüklerinden veya projenin karmasikligini tam olarak anlayamadiklarindan iyimser varsayimlarda bulunmus olabilirler. Bu planlari degerlendiren kisiler genelde bu iyimser planlamari biraz daha genisletip daha gerçekçi hale getirirler. Örnegin kodun yazilmasi bir haftada biter gibi bir tahminde bulunulmussa bunu 2 hafta veya bir aya çikarmakla daha gerçekçi planlar elde edilebilir.</p>
<li><strong>Verilen Zaman Sonuna Kadar Kullanilir</strong></li>
<p>Çogu programci verilen zamani sonuna kadar kullanmaya meyillidir. Yaptiklari isi erken bitirseler bile, kalani zamani projeye katkisi olmayacak kod düzeltmeleri yapmak, kodu birazcik hizlandirmak için harcarlar. Bazende isini bitirdikten sonra verilen zamanin bitmesini bekleyip farkli bir ise baslamazlar.  Bu yaklasimla isler asla planlandigindan çabuk bitmez, ya saydigim nedenlerden ötürü zamaninda biter, ya da isler yolunda gitmez ve gecikir.</p>
<li><strong>Firmalar Zaman Planlamarina Oldukça Fazla Itimat Eder</strong></li>
<p>Ürün çikis tarihleri, fuarlara katilim ve tanitim tarihleri hepsi projenin zaman planmasi baz alinarak yapilir. Satis ve pazarlama birimleri anlasmalarini bu tahminler üzerine yapar ve yapilan planlamada olusacak bir gecikme daha baska yapilan bir çok planinda gecikmesine neden olur.</p>
<li><strong>Önrülemeyen Seyler Üzerine Çok Fazla Zaman Harcanir</strong></li>
<p>Yapilan tahminler mükemmel bile olsalar, proje üzerinde çalisirken karsimiza öngöremedigimiz bir çok problem çikar. Eski versiyonlarda problemler çikar bunlari düzelmek için kaynak ayirmamiz gerekir, çalisanimiz hastalanmis olabilir, proje esnasinda bekledigimiz yeni isler çikmis olabilir. Çogu sirket planlarini yaparken bu gibi durumlar için ekstra zaman ayirir, fakat varsayimlar üzerine yapilmis bir plana varsayimlar üzerine ekstra zaman eklemekle nekadar güvenilir birsey elde etmis oluruz ?</p>
<li><strong>Zaman Planlamasina Tahmi Gözüyle Bakilmamasi</strong></li>
<p>Yapilan zaman planlari aslinda bir tür tahmindir fakat insanlar buna tahmin gözüyle bakmazlar, bu planlara kesinlikle aksamayacak gerçeklesecek gözüyle bakilir ve baska planlarda buna güvenerek yapilir. Ilk planda olacak bir gecikme bu yüzden zincir etkisiyle diger planlari da etkiler.</p>
<li><strong>Programcilar Arasindaki Verimlilik Farklari Gözardi Edilir</strong></li>
<p>Bu durum yapilan zaman planlamarini daha da zorlastirir. Insaat sektöründe duvar ustalarinin ortalama bir duvari örme hizini bilebilirsiniz fakat bir isi yapma hizi programcilar arasinda çok büyük farklilik gösterir. Is yapma hizi ve bir isin bitmesi için gerek efor: bu iki bilinmeyeni kullanarak tahmin yapmaya çalistiginiz da isiniz daha da zorlasir.<br />
Programci kendi yapacagi isin zaman planlamasini kendi yaptiginda isiniz az da olsa kolaylasir çünkü kendi üretkenlikleri hakkinda sizden daha çok bilgileri vardir. Fakat bu tahminleri onlar yerine genelde proje yöneticileri yapar ve bütün programcilara esit üretkenlikteymisler gibi davranir.</p>
<li><strong>Müsterinin Isteklerindeki Degisiklik Proje Planina Yansitilmaz</strong></li>
<p>Proje devam ederken, ortaya ufak ufak birseyler çikmaya basladiginda müsteri ortaya çikan bazi özelliklerden memnun olmaz degistirtmek ister veya memnun kalir ve ekstra bir kaç sey daha eklenmesini ister. Bu gibi degisiklikler proje planina bazen yansitilmaz fakat her gelen istek yapilacak fazladan is oldugu için bu gibi durumlar plana yansitilmak zorundadir.</p>
<li><strong>Gerçek Anlasildiginda Genelde Çok Geç Kalinmistir</strong></li>
<p>Önceki maddelerde belirtmistik programcilar genelde iyimser tahminlerde bulunurlar. Çogu zaman iyimser tahminde bulunduklarina ve projeyi yetistiremeyeceklerine inanmazlar taa ki son gün gelipte daha yapilacak bir çok isin kaldigi anlasilincaya kadar. Fakat artik çok geç kalinmistir, proje gecikmis ürün çikis tarihi ertelenmistir.</p>
<li><strong>Bir Plan Geciktiginde Digerleri de Güncellenmeli</strong></li>
<p>Programcilar genelde projenin planlanan bir kismini zamaninda bitiremediklerinde, diger yapacaklari islerin planlarini güncellemezler. Arada kaybolan zamani sonraki isleri daha hizlari yaparak kapatacaklarina inanirlar fakat genelde tam tersi. Böyle bir durum genelde iyimser tahmin yapildiginda isarettir</p>
<li><strong>Planlar Gerçeklesmediginde Programcilar Suçlanir</strong></li>
<p>Planlar geciktiginde suçlanacak birileri bulunmaya çalisilir ve bu genelde plana ayak uyduramayan programcilar olur. Bu stresli bir çalisma ortami yaratir ve programci sonraki projelerde suçlanmamak için yaptigi zaman planlamarinda genis davranir.</p>
<li><strong>Planlari Yanlis Insanlar Yapar</strong></li>
<p>Programcilar üzerinde çalistiklari konuyla ilgili tahminleri kendileri yapmalilar. Proje yöneticisi bu tahmini onun yerind yaptigi zaman zamanlama konusunda sikintilar yasanir. Eger çalistiginiz yerde MBA yapmis biri sizin yerinize çalistiginiz is üzerinde plan yapiyor ise orada durmayin</ol>
<p><em><a href="http://tuomaspelkonen.com/2010/04/12-problems-with-software-estimation-2/">12 Problems With Software Estimation</a> yazisinin çevirisidir.</em></p>
<img src="http://feeds.feedburner.com/~r/enginguller/~4/mL41rEMZR-0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.eguller.com/?feed=rss2&amp;p=541</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.eguller.com/?p=541</feedburner:origLink></item>
		<item>
		<title>Trenler, Asansörler ve Bilgisayar</title>
		<link>http://feedproxy.google.com/~r/enginguller/~3/2VeKzOAUqV0/</link>
		<comments>http://www.eguller.com/?p=536#comments</comments>
		<pubDate>Tue, 09 Feb 2010 22:25:58 +0000</pubDate>
		<dc:creator>engin</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.eguller.com/?p=536</guid>
		<description><![CDATA[George Westinghouse bir teorisyen değildi fakat 1800&#8242;lerin en büyük mucitlerindendi. En büyük icadı raylı sistemlerde kullanılan havalı frendir.
Bu yazıda Westinghouse&#8217;ın fikirleri ve bilgisayar bilimlerine yapmış olabileceği katkılar üzerinde duracağız.
Tren Frenleri
Atlar tarafından çekilen Wagonways isimli ilk tren 1550&#8242;lerde Almanlar tarafından kullanıldı. 1804 yılında Richard Trevithick buharlı tren ile 9 mil boyunca 10 ton demir ve 70 [...]]]></description>
			<content:encoded><![CDATA[<p>George Westinghouse bir teorisyen değildi fakat 1800&#8242;lerin en büyük mucitlerindendi. En büyük icadı raylı sistemlerde kullanılan havalı frendir.<br />
Bu yazıda Westinghouse&#8217;ın fikirleri ve bilgisayar bilimlerine yapmış olabileceği katkılar üzerinde duracağız.</p>
<p><strong>Tren Frenleri</strong></p>
<p>Atlar tarafından çekilen Wagonways isimli ilk tren 1550&#8242;lerde Almanlar tarafından kullanıldı. 1804 yılında Richard Trevithick buharlı tren ile 9 mil boyunca 10 ton demir ve 70 adam taşıdı. Bu kısa yolculuk modern trenlerin başlangıcıydı.</p>
<p><span id="more-536"></span></p>
<p>Bu olayın hemen ardından trenlerle ilgili gelen ilk problem belirdi. Trenler nasıl durdurulacaktı. Sadece motoru durdurmak uzun ve hızla giden trenleri durdurmak için yetersizdi. Çözüm olarak her vagona bir adam yerleştirildi tren durdurulmak istendiğinde vagon görevlileri bir el freni yardımıyla tren tekerleklerini sıkıştırıyorlar böylece trenin durması sağlanıyordu. Daha sonra bu sistem geliştirilerek, vagon görevlerinin yaptığı iş sıkıltırılmış havayla yapılmaya başlandı. Makinist treni durdurmak istediği zaman bir valf yardımıyla sıkıştırılmış su buharını serbest bırakıyor, sıkıştırılmış buharın gücüyle her vagonlardaki frenler çalıştırılıyordu. Oldukça temiz bir çözümdü.</p>
<p>Fakat nekadar güvenilebilirdi ? Ya sıkıştırılmış su buharı tankı boşsa veya tanklardaki basınç düşükse yada su buharını frenlere ileten boru sisteminde bi kaçak varsa basınç yüksek tutulamayacak trenin durması sağlanamayacaktı.</p>
<p>Westinghouse çok zeki biriydi ve bu sorunu ters havalı fren sistemiyle çözdü. Westinghouse &#8216;ın sistemine göre buhar basıncı treni durdurmak için değil hareket ettirmek için kullanılacaktı.  Bu sisteme göre her vagondaki tekerlekler güçlü yaylar tarafından sıkıştırılacaktı böylece trenin hareket etmesi mümkün olmayacaktı. Makinist treni hareket ettirmek istediğinde buhar basıncını frenlere gönderecek, basınç yayları esnetip treni serbest bırakacaktı.</p>
<p>Bu  akıllıca bir çözümdü. Tanklardaki basınç düştüğünde, yada borularda bir kaçak olduğunda yaylara uygulanan kuvvet ortadan kalkacak serbest kalan yaylar vagon tekerleklerini sıkıştırarak trenin durmasını sağlayacaktı. Ters giden birşey olduğunda trenin güvenli bir şekilde durması sağlanacaktı. Bu sistem bugün hala kullanılmaktadır.</p>
<p><strong>Asansörler</strong></p>
<p>Asansörler trenlerden de eskidir, antik çağlara dayanan geçmişleri vardır. 1800 lerin ortalarına gelinipte yüksek binalar inşa edilmeye başlandığında, asansör güvenliği önemli bir haline geldi. Altı katlı veya daha alçak binalarda asansör güvenliği çok kritik bir konu değildir. Fakat daha yüksek binalarda asansörler güvenli olmanında ötesinde güvenli görünmelidirler de aksi takdirde insanlar asansörlere binmekten korkarlar.</p>
<p>1853 Elisha Otis asansörlerdeki güvenlik sorununu çözmeyi başardı. Elisha Otis, asansörün ipleri kopsa bile asansörün düşmesini engelleyecek bir sistem geliştirdi. Bu sistemde eğer asansörün ipleri gerginse asansörün düşmesini engelleyen frenler devre dışı kalır. Eğer asansörü taşıyan ip gevşerse asansör freni devreye girer ve asansörün düşmesini engeller.</p>
<p>Otis&#8217;in bu buluşunu hemen başarıya ulaşmadı, bu yüzden sistemin çalıştığını ve güvenli olduğunu insanlara ispatlaması gerekiyordu. 1854 yılında Amerika&#8217;daki bir fuarda içi görülebilir bir asansör boşluğu inşa etti. Gün içinde bir kaç defa bu asansöre binerek yukarıya tırmandı ve asansör yukarıda asılıyken ipi kesti ve asansör frenleri aşağıya düşmesini engelledi. Her denemesinde aşağıdaki izleyicilere</p>
<p><em>&#8220;Tamanen güvenli beyler, tamamen güvenli&#8221;</em> diye sesleniyordu.</p>
<p>Etrafı açık olan bu sistem sayesinde insanlar onu rahatlıkla izleyebildi ve sisteminin güvenli olduğuna ikna oldular.  Bu halka açık gösteri sayesinde Otis&#8217;in şirketi başarıya ulaştı ve çok yüksek binaların yapımı da mümkün oldu.</p>
<p><strong>Genel Prensip</strong></p>
<p>Bana göre tren ve asansörlerin güvenlik sisteminde güçlü bir prensip kullanıldı. İkisindede güvenlik sisteminin devreye girmesi için bir müdaheleye gerek yoktu. Bir sorun olduğunda ne makinistin fren yapması gerekiyordu nede asansördeki kişinin fren kolu gibi birşeyi çekmesi. İki sistemde de bir sorun olduğunda sistem doğrudan güvenli moda geçme eğilimindeydi.</p>
<ul>
<li> Trende: borularda basınç yoksa, yaylar tekerlekleri sıkıştırıp treni durduruyor.</li>
<li>Asansörde: Asansörü tutan ip kopmuşsa,  asansör frenleri devreye girer.</li>
</ul>
<p>Ana prensip şudur, güvenliği bir eyleme bağımlı kılmayın, güvenlik sistemin kendisinden gelsin. Sistemdeki hiç bir birim çalışmadığında sistem pasif ve güvenli durumda olsun, birimler çalışmaya başladığında güvenlik sistemini kaldırıp kendi görevlerini yerine getirsinler. Fakat görevlerini yerine getirilirken sorun oluşursa, güvenlik sistemini devredışı bırakan birimler devre dışı kaldığından, güvenlik sistemi otomatik olarak devreye girecektir, yani varsayılan haline dönmüş olacaktır.</p>
<p>Acaba benzer prensibi kullanarak bilgisayar dünyasını daha güvenli hale getirebilir miyiz. Yukarıda anlatılan prensibi kullanan virüslerden etkilenmeyen etkilendiğinde güvenli mode geçip zarar görmemizi engelleyecek sistemler geliştirebilir miyiz ?</p>
<p>Yazının orjinali için <a href="http://rjlipton.wordpress.com/2010/02/08/trains-elevators-and-computer-science/" target="_blank">tıklayınız&#8230;</a></p>
<img src="http://feeds.feedburner.com/~r/enginguller/~4/2VeKzOAUqV0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.eguller.com/?feed=rss2&amp;p=536</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.eguller.com/?p=536</feedburner:origLink></item>
		<item>
		<title>Object Orientation</title>
		<link>http://feedproxy.google.com/~r/enginguller/~3/uCBBwxxI3M4/</link>
		<comments>http://www.eguller.com/?p=478#comments</comments>
		<pubDate>Sun, 07 Feb 2010 01:30:11 +0000</pubDate>
		<dc:creator>engin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Abstract]]></category>
		<category><![CDATA[Abstract Class]]></category>
		<category><![CDATA[Interface]]></category>
		<category><![CDATA[Object Oriented]]></category>

		<guid isPermaLink="false">http://www.eguller.com/?p=478</guid>
		<description><![CDATA[Encapsulation
Do not expose class variables, instead of doing this, use setter and getter methods and keep class variables private.

public class Car &#123;
   private int price;
   private int maxSpeed;
   public int getPrice&#40;&#41; &#123;
      return price;
   &#125;
   public void setPrice&#40;int price&#41; &#123;
 [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Encapsulation</strong><br />
Do not expose class variables, instead of doing this, use setter and getter methods and keep class variables private.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Car <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> price;
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> maxSpeed;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getPrice<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> price;
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setPrice<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> price<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">price</span> <span style="color: #339933;">=</span> price;
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getMaxSpeed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> maxSpeed;
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMaxSpeed<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> maxSpeed<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">maxSpeed</span> <span style="color: #339933;">=</span> maxSpeed;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>What is the advantage of previous usage, first of all flexibility. For example you have changed your mind, decide to keep max speed read only. So we can just remove setMaxSpeed method or change it to private for internal use.<br />
Another advantage is validation, if user want to set negative value for car price, so you can add a validation in setPrice that is it.</p>
<p><strong>Is-A and Has-A Relationship</strong><br />
So, Ferrari is a Car, Cow is an animal lets codify these statements.<br />
<span id="more-478"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Car<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Ferrari <span style="color: #000000; font-weight: bold;">extends</span> Car<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Animal<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Cow <span style="color: #000000; font-weight: bold;">extends</span> Animal<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Water is drinkable</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Drinkable<span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> drink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Water <span style="color: #000000; font-weight: bold;">implements</span> Drinkable<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> drink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Car has tires.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tire<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Car<span style="color: #009900;">&#123;</span>
  Tire tires<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Polymorphism</strong><br />
If an object can pass more than one is-a (instance of) test, it is polymorphic.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Bat <span style="color: #000000; font-weight: bold;">extends</span> Bird,Mammal <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//False a class can only extend one class.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Some languages like C++ supports multiple inheritance, but sometimes multiple inheritance causes confusion. For example both Bird and Mammal have giveBirth() method, when we call giveBirth in Bat class, which giveBirth will work ? Will bat give birth like mammals or birds. As a result Java does not support multiple inheritance to prevent this confusion.<br />
A proper structure could be like that</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Bat <span style="color: #000000; font-weight: bold;">extends</span> Bird <span style="color: #000000; font-weight: bold;">implements</span> Mammal <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So, Bat is an object<br />
Bat is a bird<br />
Bat is a mammal.</p>
<p>It&#8217;s a Bird…It&#8217;s a Plane…It&#8217;s Superman</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Flyable<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> fly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Bird <span style="color: #000000; font-weight: bold;">implements</span> Flyable <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> fly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Plane <span style="color: #000000; font-weight: bold;">implements</span> Flyable <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> fly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Superman <span style="color: #000000; font-weight: bold;">implements</span> Flyable <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> fly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Look at the following code</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Playable <span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Player <span style="color: #000000; font-weight: bold;">implements</span> Playable <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am a player&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FootballPlayer <span style="color: #000000; font-weight: bold;">extends</span> Player <span style="color: #000000; font-weight: bold;">implements</span> Playable<span style="color: #009900;">&#123;</span>
   @Override
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am football player&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      FootballPlayer footballPlayer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FootballPlayer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
      Playable playable <span style="color: #339933;">=</span> footballPlayer;
      Player player <span style="color: #339933;">=</span> footballPlayer;
      footballPlayer.<span style="color: #006633;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
      playable.<span style="color: #006633;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
      player.<span style="color: #006633;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Output is :<br />
I am football player<br />
I am football player<br />
I am football player</p>
<p>We have called play method on three different types but results are same, because actual object behind the instance is important, football player, player and playable are all in different types but actual object behind the types is FootballPlayer object.</p>
<p><strong>Overriding / Overloading</strong><br />
If you want to change behavior of subclass you can use overriding. We override play method in the following example. Player is more general it says &#8220;I am a player&#8221; but FootballPlayer is more specific, and it says &#8220;I am a football player&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Player <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am a player&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FootballPlayer <span style="color: #000000; font-weight: bold;">extends</span> Player<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am a football player&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> kickTheBall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I kick the ball&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In abstract classes you have to override abstract methods, but while extending concrete classes you do not have to override.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">Player player <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Player<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
Player footballPlayer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FootballPlayer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
player.<span style="color: #006633;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
footballPlayer.<span style="color: #006633;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
player.<span style="color: #006633;">kickTheBall</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// illegal,compiler error</span>
footballPlayer.<span style="color: #006633;">kickTheBall</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// illegal, compiler error</span></pre></div></div>

<p>Output is:<br />
I am a player<br />
I am a football player</p>
<p>What about this code, does it compile ?</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Player <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am a player&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FootballPlayer <span style="color: #000000; font-weight: bold;">extends</span> Player<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am a football player&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>No it does not compile, because FootballPlayer&#8217;s play method is private. If you override a method in a subclass you cannot be more restrictive.</p>
<table border="1">
<tbody>
<tr>
<td>Player.play()</td>
<td>FootballPlayer.play()</td>
<td>Result</td>
</tr>
<tr>
<td>private</td>
<td>private</td>
<td>Legal but not an override, overridden method is not visible</td>
</tr>
<tr>
<td>protected</td>
<td>private</td>
<td>Illegal because, FootballPlayer.play can not be more restrictive.</td>
</tr>
<tr>
<td>protected</td>
<td>protected</td>
<td>Legal, access restrictions are at same level.</td>
</tr>
<tr>
<td>protected</td>
<td>public</td>
<td>Legal, FootballPlayer.play is less restrictive than Player.play.</td>
</tr>
<tr>
<td>public</td>
<td>private</td>
<td>Illegal.</td>
</tr>
<tr>
<td>public</td>
<td>protected</td>
<td>Illegal.</td>
</tr>
<tr>
<td>public</td>
<td>public</td>
<td>Legal.</td>
</tr>
</tbody>
</table>
<p><strong>Rules of Overriding</strong></p>
<ul>
<li>Argument list must match.</li>
<li>Return type must match.</li>
<li>Subclass&#8217;s method can not be more restrictive than super-class&#8217;s overridden method. See table above.</li>
<li>Overriding method can throw any Runtime exception.

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FootballPlayer <span style="color: #000000; font-weight: bold;">extends</span> Player<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//Legal</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am a football player&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">SQLException</span><span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//Illegal. In this situation Player's play method should throw broader exception</span>
      <span style="color: #666666; font-style: italic;">//like java.lang.Exception</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am a football player&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li>
<table border="1">
<tbody>
<tr>
<td>Player.play()</td>
<td>FootballPlayer.play()</td>
<td>Result</td>
</tr>
<tr>
<td>throws SQLException</td>
<td>throws Exception</td>
<td>illegal, overriding method cannot throw a broader exception.</td>
</tr>
<tr>
<td>throws SQLException</td>
<td>throws SQLException</td>
<td>Legal.</td>
</tr>
<tr>
<td>throws Exception</td>
<td>throws SQLException</td>
<td>Legal.</td>
</tr>
<tr>
<td>no exception</td>
<td>throws SQLException</td>
<td>Illegal.</td>
</tr>
<tr>
<td>no exception</td>
<td>throws NumberFormatException</td>
<td>Legal because NumberFormatException is a RuntimeException.</td>
</tr>
<tr>
<td>no exception</td>
<td>throws RuntimeException</td>
<td>Legal.</td>
</tr>
</tbody>
</table>
</li>
<li> You can not override final methods.</li>
<li> You can not override static methods.</li>
</ul>
<p>How can you call a overridden method in a super-class.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Player <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am a player&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FootballPlayer <span style="color: #000000; font-weight: bold;">extends</span> Player<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am a football player&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Output:<br />
I am a player.<br />
I am a football player.</p>
<p><strong>Overloaded Methods</strong><br />
Overloading a method means, you can user same method name in a class more than once. But method parameters and optionally return types must be different of overloaded methods. A sub class can overload a method of superclass.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Player <span style="color: #009900;">&#123;</span>
   <span style="color: #003399;">String</span> playerType <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span>;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setPlayerType<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> playerType<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">playerType</span> <span style="color: #339933;">=</span> playerType;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FootballPlayer <span style="color: #000000; font-weight: bold;">extends</span> Player<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">String</span> playerType <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span>;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setPlayerType<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">playerType</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;football player&quot;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here setPlayerType(String playerType) method is overloaded in FootballPlayer class.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Player <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am a player&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FootballPlayer <span style="color: #000000; font-weight: bold;">extends</span> Player <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span>  <span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am football player&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> overLoaded<span style="color: #009900;">&#40;</span>Player pl<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Player method&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> overLoaded<span style="color: #009900;">&#40;</span>FootballPlayer footballPl<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Football player method&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     Player player <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Player<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
     Player footballPlayer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FootballPlayer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
     FootballPlayer.<span style="color: #006633;">overLoaded</span><span style="color: #009900;">&#40;</span>player<span style="color: #009900;">&#41;</span>;
     FootballPlayer.<span style="color: #006633;">overLoaded</span><span style="color: #009900;">&#40;</span>footballPlayer<span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We have created two objects FootballPlayer and Player but using same type name Player. What does previous code print.</p>
<p>Output is :<br />
Player method<br />
Player method</p>
<p>Underlying object of footballPlayer variable is FootballPlayer at runtime but its type is Player. Overloaded method is called according to type, not underlying object. Because decision of which overloaded method will be called is made  at compile time not at Runtime.</p>
<p>So it is important:<br />
Decisions about overridden methods are done at runtime.<br />
Decisions about overloaded methods are done at compile time.</p>
<p>Polymorphism cannot be applied overloaded methods because polymorphism is a runtime issue.</p>
<p>Can change</p>
<table border="1">
<tbody>
<tr>
<td></td>
<td>Overloaded Methods</td>
<td>Overridden Methods</td>
</tr>
<tr>
<td>Arguments</td>
<td>Need to change</td>
<td>Does not need</td>
</tr>
<tr>
<td>Return Type</td>
<td>Can change</td>
<td>Cannot change</td>
</tr>
<tr>
<td>Exceptions</td>
<td></td>
<td>Must be reduced or eliminated.</td>
</tr>
<tr>
<td>Access</td>
<td>Can change</td>
<td>Cannot be more restrictive</td>
</tr>
<tr>
<td>Invocation</td>
<td>Based on variable type, decided in compile time</td>
<td>Decided according to underlying object type, decided in runtime</td>
</tr>
</tbody>
</table>
<p><strong>Reference Variable Casting</strong><br />
Think about below scenario</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Car <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> Ferrari <span style="color: #000000; font-weight: bold;">extends</span> Car <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> enableRaceMode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Race mode enabled&quot;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    Car c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Ferrari<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>c <span style="color: #000000; font-weight: bold;">instanceof</span> Ferrari<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      c.<span style="color: #006633;">enableRaceMode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;  <span style="color: #666666; font-style: italic;">//compilation fails.</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Actually variable &#8220;c&#8221; refers to a Ferrari object, its type is Car and Car does not have a &#8220;enableRaceMode&#8221; method so how ca we make it compile.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    Car c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Ferrari<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>c <span style="color: #000000; font-weight: bold;">instanceof</span> Ferrari<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      Ferrari f <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Ferrari<span style="color: #009900;">&#41;</span>c;
      f.<span style="color: #006633;">enableRaceMode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;  <span style="color: #666666; font-style: italic;">//compilation fails.</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here we made a down casting, Car object casted to Ferrari. What happens &#8220;Car  c&#8221; refer to a Ford object and if we try to down cast &#8220;Car c&#8221; to Ford.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Ford <span style="color: #000000; font-weight: bold;">extends</span> Car<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
Car c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Ford<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
Ferrari f <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Ferrari<span style="color: #009900;">&#41;</span>c;</pre></div></div>

<p>Code compiles fine but throws ClassCastException at runtime thats why it is good to make an instanceof check before make a down casting.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">Car c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Ford<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> c <span style="color: #000000; font-weight: bold;">instanceof</span> Ferrari<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  Ferrari f <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Ferrari<span style="color: #009900;">&#41;</span>c; <span style="color: #666666; font-style: italic;">// never executed</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you know, Java does not support multiple inheritance, so you don&#8217;t need to declare superclass&#8217; name at up casting. But superclass&#8217; can have multiple subclasses so we need to write down casting to what ?</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">Ferrari f <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Ferrari<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
Car c <span style="color: #339933;">=</span> f; <span style="color: #666666; font-style: italic;">// no need to Car c = (Car)f;</span>
c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Ferrari<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
Ferrari f <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Ferrari<span style="color: #009900;">&#41;</span>c; <span style="color: #666666; font-style: italic;">//Down casting to Ferrari</span></pre></div></div>

<p><strong>Interfaces</strong><br />
Most important point about interfaces is you should implement all methods in your class , what you have defined in implemented interface. We have fly method in Flyable interface so all classes which are implement Flyable interface should have fly method. But why there is a strict rule like this.<br />
Think about that:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">Flyable flyable <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pigeon<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
flyable.<span style="color: #006633;">fly</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>For example Penguen class implements Flyable and does not have a fly method. (This is compiler error but let assume that this is valid in Java).<br />
When we run</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">Flyable penguin <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Penguin<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
penguen.<span style="color: #006633;">fly</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>In this situation jvm looks for &#8220;fly&#8221; method in Penguin class, but it does not exists, crash! But this was an u topic world, this kind of statement is already a compiler error.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> Flyable<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> fly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> Pigeon <span style="color: #000000; font-weight: bold;">implements</span> Flyable<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> fly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am pigeon, I can fly&quot;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Abstract classes and interfaces have similarities, abstract classes have abstract methods and these methods does not have body like interfaces. Lets look at combination of abstract classes and interfaces.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> Bird <span style="color: #000000; font-weight: bold;">implements</span> Flyable<span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000066; font-weight: bold;">void</span> eat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You see, abstract class does contains fly method. Is this situation a compiler error ? No it is not, you can pass implementation first concrete subclass of Bird class.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Cuckoo <span style="color: #000000; font-weight: bold;">extends</span> Bird <span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> eat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> fly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So a common interview question, why do we implements Runnable while we can already do same thing by extending Thread. Because Java does not support multiple inheritance, for example you have a frame which extends JFrame, and you want to add a background thread to your class. In this example you need to say extends JFrame,Thread but it is a illegal statement, you should use Runnable interface here like &#8220;extends JFrame implements Runnable&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Playable<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Game<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Soccer <span style="color: #000000; font-weight: bold;">extends</span> Playable, Game <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AmericanSoccer <span style="color: #000000; font-weight: bold;">implements</span> Soccer<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Interfaces can extends more than one class unlike concrete or abstract classes.</p>
<p>There are two kinds of methods according to return type. If method does not return anything you should use &#8220;void myMethod(parameters)&#8221;, if method returns a value you should use &#8220;ReturnType myMethod(parameters)&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Dog<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> bark<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;woof, woof&quot;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Cat<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getRemainingLives<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">9</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Return types of overloaded methods</strong><br />
The similarity between overloaded methods is same as similarity between Java and JavaScript and this means there is no similarity. Overloaded methods are totally different methods except their names. When you changed signature of a method you obtain an overloaded method. You can also freely change overloaded methods return types.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #003399;">String</span> play<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> playCount<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Following code is illegal. If you</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">void</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #003399;">String</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Return Type of Overriding Methods</strong><br />
Return type and signature of a overriding method must be same, but overriding method can return a subclass of overridden method.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Car<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> Car createNewCar<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> parm<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Car<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> Ferrari <span style="color: #000000; font-weight: bold;">extends</span> Car<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> Ferrari createNewCar<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> parm<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Ferrari<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Above feature enabled at Java 5. If you compile source with a 1.4 compile you will get a &#8220;attempting to use incompatible return type&#8221; error.</p>
<p>You can return null in any method except void return type.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> Ferrari createNewCar<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> parm<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span>; <span style="color: #666666; font-style: italic;">//return null object reference.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can return Array.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> Ferrari createNewCars<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> parm<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Ferrari<span style="color: #009900;">&#91;</span>parm<span style="color: #009900;">&#93;</span>; <span style="color: #666666; font-style: italic;">//return null object reference. then instantiate references.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For primitive type, you can return any variable that can be convert declared type.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">char</span> c <span style="color: #339933;">=</span> <span style="color: #0000ff;">'c'</span>;
   <span style="color: #000000; font-weight: bold;">return</span> c;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can return any value or variable that can be explicitly cast to declared type.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">float</span> x <span style="color: #339933;">=</span> 3.3f;
   <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span>x;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can return any object reference that can be implicitly cast to the declared return type.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> Car getCar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Ferrari<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can return arrays as Object return type.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> getCar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;1&quot;</span>,<span style="color: #0000ff;">&quot;2&quot;</span>,<span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #009900;">&#125;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Return type can be an interface while returning value is an object which implements interface at return type.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Flyable<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> Superman <span style="color: #000000; font-weight: bold;">implements</span> Flyable<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> Flyable getFlyable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Superman<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Constructors</strong></p>
<p>Every class and abstract class should have a constructor. Constructors are called when we use &#8220;new&#8221; keyword. Every class should contains a constructor but probably you see definitions like,</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Car<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Car class does not have a constructor definition but it does not mean that, Car class does not have a constructor. If no constructor is defined in class a default one is created. Default constructors are constructors with zero parameter.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Car<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> Car<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// constructor.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Constructors are similar to methods but they do not have return type. No &#8220;void&#8221;,&#8221;int&#8221;,&#8221;String&#8221;,&#8221;Object&#8221; &#8230; And constructor name must be same as class name. If class name is Car constructors can be Car(), Car(int param1), Car(String param1,Object param2) &#8230;</p>
<p>We can overload constructors as we did in methods.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Car<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> Car<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// constructor no parameter</span>
  <span style="color: #000000; font-weight: bold;">public</span> Car<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> param1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">//constructor one parameter</span>
  <span style="color: #000000; font-weight: bold;">public</span> Car<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> param1,<span style="color: #000066; font-weight: bold;">int</span> param2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">//constructor with two parameter</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Usage:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">Car c1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Car<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
Car c2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Car<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;
Car c3 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Car<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Race Car&quot;</span>,<span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>How constructors&#8217; call flow occurs for following code.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Car <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> Ferrari <span style="color: #000000; font-weight: bold;">extends</span> Car<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>When we run &#8220;new Ferrari()&#8221; code block,</p>
<ol>
<li>Ferrari&#8217;s default constructor calls Car&#8217;s default constructor</li>
<li>Car&#8217;s default constructor calls Object&#8217;s default constructor.</li>
<li>Object instance variables are given their explicit values.</li>
<li>Object&#8217;s constructor is completed.</li>
<li>Car instance variables are given their explicit values.</li>
<li>Car&#8217;s constructor is completed</li>
<li>Ferrari instances variables are given their explicit values.</li>
<li>Ferrari&#8217;s constructor is completed.</li>
</ol>
<p><strong>Default Constructor</strong><br />
Follow the next three example to understand that when a default constructor will be created.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Ferrari<span style="color: #009900;">&#123;</span>
 Ferrari<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
 Ferrari<span style="color: #009900;">&#40;</span><span style="color: #003399;">Color</span> color<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Default constructor will not be created for above example because Ferrari class already has a constructor.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Ferrari<span style="color: #009900;">&#123;</span>
 Ferrari<span style="color: #009900;">&#40;</span><span style="color: #003399;">Color</span> color<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Again a default constructor will not be created as in previous example. Because Ferrari class already has a constructor.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Ferrari<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;</span>pre<span style="color: #339933;">&gt;</span>Yes a <span style="color: #000000; font-weight: bold;">default</span> constructor will be created <span style="color: #000000; font-weight: bold;">this</span> time because we have not defined any constructor <span style="color: #000000; font-weight: bold;">for</span> Ferrari <span style="color: #000000; font-weight: bold;">class</span>.
&nbsp;
<span style="color: #006633;">There</span> is only one rule you should remember about <span style="color: #000000; font-weight: bold;">default</span> constructor. <span style="color: #000000; font-weight: bold;">If</span> you have not defined any constructor in your <span style="color: #000000; font-weight: bold;">class</span> a <span style="color: #000000; font-weight: bold;">default</span> constructor will be created by compiler.
&nbsp;
<span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>Overloaded Constructors<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;</span>
You can overload constructors as in methods. <span style="color: #006633;">You</span> can define more than one constructors with different argument type<span style="color: #339933;">/</span>count.
<span style="color: #339933;">&lt;</span>pre lang<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span><span style="color: #339933;">&gt;</span>class Ferrari<span style="color: #009900;">&#123;</span>
 Ferrari<span style="color: #009900;">&#40;</span><span style="color: #003399;">Color</span> color<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
 Ferrari<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> model<span style="color: #009900;">&#41;</span>
 Ferrari<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Statics</strong><br />
If your method&#8217;s behavior does not depend on class&#8217; instance variables, you should mark this method as static. All methods in java.lang.Math are static because return value of these methods do not depend on any instance variable. Usually utility classes have static methods.<br />
You should use instance variables instead of static variables, but in some situations you can not. For example you want to know how many times an instance of a class is created. You cannot use an instance variable in this situation because an instance variable is always set to its default value at initialization. But value of a static variable does not change until you change it, value does not change even if a new instance created.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Ferrari<span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> INSTANCE_COUNT<span style="color: #339933;">=</span>0;
  <span style="color: #000000; font-weight: bold;">public</span> Ferrari<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     INSTANCE_COUNT++;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">new</span> Ferrari<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">new</span> Ferrari<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">new</span> Ferrari<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">new</span> Ferrari<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>Ferrari.<span style="color: #006633;">INSTANCE_COUNT</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Result is 4 for above example.</pre>
</pre>
<img src="http://feeds.feedburner.com/~r/enginguller/~4/uCBBwxxI3M4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.eguller.com/?feed=rss2&amp;p=478</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.eguller.com/?p=478</feedburner:origLink></item>
	</channel>
</rss>
