Note: I have moved to wordpress. New URL is "Technology Made Easy"

Wednesday, July 30, 2008

C# : Reference of Abstract class containing derived class instance

You could have seen a object initialization like this


A a = new B();


where B is a derived class of A. But we cannot do the other way around.


B b = new A(); // because A is an abstract class


Take a look into the below example and its output.You can understand how it
works.






using System;
using System.Text;

namespace aruns_code
{
class testAbstract
{
[STAThread]
static void Main(string[] args)
{
der derObj = new der();
derObj.nonAbs();

Console.WriteLine("\n\n");

abs absObj = new der();
absObj.nonAbs();
absObj.absMethod();
}
}

class der : abs
{
public der()
{
Console.Write("initialized der\n");
}

public override void absMethod()
{
Console.Write("inside der\n");
}

public new void nonAbs()
{
Console.Write("inside nonAbs of der\n");
}
}


abstract class abs
{
public abs()
{
Console.Write("initialized abs\n");
}

public abstract void absMethod();

public void nonAbs()
{
Console.Write("inside nonAbs of abs\n");
}
}

}




The following is the output




initialized abs
initialized der
inside nonAbs of der

initialized abs
initialized der
inside nonAbs of abs
inside der

39 comments:

ArdellaJ said...

av聊天室情色文情色文學討論區台灣情色貼圖avdvdavdvavdcp2p論壇av網站av線上直播av線上看av線上短片av線上做愛影片av線上免費av線上免費短片av線上免費試看電影av線上免費影片av線上免費播放av線上影片av線上影片免費試看av美女dvdav美女圖片區av美女寫真av美女影片情色典慾長亂倫貼圖小說亂倫母子小說亂倫亂倫亂倫人妻了天室了天是二手絲襪二人遊戲情色五月天自拍網洲情色亞色情光碟亞州貼圖區亞州影片亞州情色亞州成人亞洲

睡衣 said...

路過--你好嗎..很棒的BLOG.........................................

香蕉哥哥 said...

may the blessing be always with you!! ........................................

哲宇 said...

做愛色情影片免費色情論壇色情網小說色情秀一夜聊天室上床片免費看上床免費看上床美女影片免費試看上床影片免費看上班族天室上班族室上班族聊天(f1)上班族聊天至上班族聊天室(f1)口交電影口焦大乃妹做愛影片女生裸睡圖片一葉聊天tw18禁wii後宮電影院xevideoxex888xvdeosxveidosxvideo 免費影片xvideo 松島xvideo 線上xvideo松島楓xvides金瓶梅成人漫畫金瓶梅情色小說文學漫畫貼圖成人電影

于名于倫 said...

Hello,這裡真是百看不厭的部落格 .........................................

志冠少菁伶義 said...

得意人,聽腳步聲就知道。......................................................

鈺禎 said...

我新來的~大家可交個朋友嗎(・ˍ・)........................................

裕以 said...

wonderful...................................................

長卉LisetteC said...

文章很棒~感謝!!..................................................

OliveWol8888 said...

人生中最重要的是要自尊、自愛、自立、自強、自信。 ....................................................

懿綺 said...

厚 !!!等了好久的更新~~一定要支持的啦(ΘωΘ)........................................

韋于倫成 said...

一個人的價值,應該看他貢獻了什麼,而不是他取得了什麼 ..................................................

逸凡 said...

Better late than never...................................................

EleanoreBla昭彥 said...

Judge not a book by its cover.............................................................

hernande said...

逛到您的部落格讓我忍不住停下來!期待您的新文章!!........................................

亦妮亦妮 said...

看看blog放鬆一下,工作累死了................................................................

李映 said...

初次造訪,安安^^.................................................................

邦雄 said...

河水永遠是相同的,可是每一剎那又都是新的。......................................................................

江婷 said...

知識可以傳授,智慧卻不行。每個人必須成為他自己。......................................................................

靜宸靜宸 said...

當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。............................................................

吳婷婷 said...

人有兩眼一舌,是為了觀察倍於說話的緣故。............................................................

金正偉 said...

今天心情很好..你心情好嗎?..................................................................

恩如 said...

希望我的支持可以帶給你快樂--加油.............................................................

李冠文 said...

脾氣與嘴巴不好,就算心地再好,也不算好人~~~..................................................

隆軒隆hcox軒泉軒泉 said...

我們必須先有哭泣,才有歡笑;也必須先感到人生的悲哀,然後才感到人生的快樂。............................................................

溫緯李娟王季 said...

好文章,希望能一直看到您的PO文......................................................

家榮家榮 said...

希望我的支持可以帶給你快樂--加油...................................................................

rus張se君llol珮ga said...

i consder your artical is so nice!............................................................

李淑成李淑成 said...

成功可招引朋友,挫敗可考驗朋友............................................................

芷李玟 said...

一個人的價值,應該看他貢獻了什麼,而不是他取得了什麼....................................................

李吳俊其彥柏 said...

人生像一杯茶,若一飲而盡,會提早見到杯底..................................................

敬周喜 said...

堅持是為著某種目的或目標,而持續不斷朝向既定方向努力的一種意念。..................................................

文王廷 said...

一個人的價值,應該看他貢獻了什麼,而不是他取得了什麼....................................................

por said...

祝福大家開開心心。............................................................

張王雅竹欣虹 said...

感動 + 感恩 + 感謝(。-ˍ-。 )............................................................

佳張張張張燕張張張張張 said...

期待更新,要保持最佳狀況呦..................................................................

陳智雅威宜 said...

第一次睇你blog,鐘意!............................................................

蕾蕾 said...

路過看到好的blog,來留言湊個熱鬧..................................................................

孫邦柔 said...

如果你批評他人。你就沒有時間付出愛......................................................................

Ratings by outbrain