You are on page 1of 15

Object Oriented Concepts in C#

Page 1 of 15

Total Online Users: 1 9 7 6 3

Silverlight Host ing Providers

H om e | Forum s | V ide os | Adve rt ise | Cert ifica t ions | D ow nloads | Blogs | I n t er view s | Jobs | Beginne rs | Training | Consult ing Login Sea rch : H om e C# La ngua ge Obj e ct Orient ed Conce pt s in C# Rem em ber Me Sign I n Re gist er Forgot Pa ssw or d Forgot User na m e W hy Regist er Jum p t o Te chn ology W e bsit e Sponsor ed by Print Em ail t o a friend Post a com m ent Book m ark Sim ilar Art icles Aut hor's other art icles Share Go

Submit an Article Submit a Blog

Advan ced Sea rch

Obj e ct Or ie n t e d Conce pt s in C#
By t usha r k a nt .a ga r w a l D e ce m be r 2 4 , 2 0 0 3
I n t his art icle we will discuss key concept s of obj ect orient ation with t heir pract ical im plem ent at ion in C# . We will discuss here basics of OOPS including I nt erfaces, Access Modifier s, inher it ance, polym orphism et c.

Page View s Dow n loads Rat in g Le ve l

: 270114 :0 : Rat e it : Beg in n e r

Like

59
Sponsored by

Becom e a Sponsor

Sim ilar Art icles

Most Read

Top Rat ed

Lat est

I nherit ance in C# An I nt roduct ion t o C# Com pilat ion and Runt im e Execut ion of a C- Shar p Program I nherit ance Versus I nt erfaces PERST: A Sim ple, Fast , Conv enient Obj ect Orient ed Dat abase More... Sponsored by

Becom e a Sponsor

Becom e a Sponsor Re sou rces About Us ASP.N ET Host ing Aut h ors Book Chapt ers Book Review s C# Consult ing C# Tra in in g D ow nloa ds M edia Kit N ew s & Even t s Prizes Tag Cloud

C# Corner on Facebook
Like 2,473

Abst r act Class Ar rayList Box ing Con sole Const ruct ors enum Ev ent Han dler Ex t en sion Met h ods FileSt ream Hash t able inherit ance

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 2 of 15

Product Review s Resource s Tips Tools Use r Groups Ou r N et w or k .N ET H eaven C# Corn er D bTalk s I nt ervie w Cor ner Longh orn Corn er M in dcra cke r V B.N ET H eaven

Ea r ly bin din g m eans t hat our code dir ect ly int er act s w it h t he obj ect , by direct ly calling it s m et hods. Since t he com piler k nows t he obj ect 's dat a t ype ahead of t im e, it can dir ect ly com pile code t o invoke t he m et hods on t he obj ect . Ear ly binding also allow s t he I DE t o use I nt elliSense t o aid our developm ent effor t s; it allows t he com piler t o ensure t hat w e are refer encing m et hods t hat do exist and t hat we are prov iding t he proper par am et er v alues.

I nt erfaces List obj ect dat asource OOP Sor tin g St ream Writ er Class St rings Sy st em .Tex t
u nbox in g User Con t rols

visual st udio

Visual St udio 2005

Thread

St ream Reader

La t e bin d in g m eans t hat our code int er act s w it h an obj ect dynam ically at run - t im e. This prov ides a gr eat deal of flexibilit y since our code lit erally doesn't care what t ype of obj ect it is int eract ing w it h as long as t he obj ect support s t he m et hods we w ant t o call. Because t he t y pe of t he obj ect isn't known by t he I DE or La t est Jobs com piler, neit her I nt elliSense nor com pile- t im e synt ax check ing is Apply ing for j ob possible but w e get unprecedent ed flex ibilit y in exchange. I f w e enable st rict t y pe check ing by using Opt ion St rict On at t he t op of our code m odules, t hen t he I DE and com piler w ill enfor ce early binding behavior. By default , Opt ion St r ict is t ur ned off and so w e hav e easy access t o t he use of lat e binding w it hin our code. Acce ss M odifie r s Access Modifiers are k eyw ords used t o specify t he declar ed accessibilit y of a m em ber of a t y pe. Pu b lic is v isible t o ev ery one. A public m em ber can be accessed using an inst ance of a class, by a class's int er nal code, and by any descendant s of a class.

Metr o St yle App Dev eloper .NET Developer WPF or Silver light exp Bost on MA ASP .Net Developer s Split Test ing Designer .NET Developer Web Developer Applicat ion Engineer Ex pert .NET Developer Applicat ion Dev eloper More ...

La t est I nt erview Quest ions Applicat ion_St art in ASP.NET How t o pass dynam ically creat ed into St ored procedure t hrough SQL par am et er s in C# .NET? How can we conv ert .I ntofloat like conv ert .int oint32 Difference bet ween Access specifiers and access m odifiers Hi ev ery one, How t o scan a docum ent in c# using windows form s in .net fram ework and i am using windows 7( 64- bit) OS ......... Please help m e Can we declare a variable const and volatile both? What ex act ly t he t erm cookies refer t o? How t o display Alert in Jav aScript on ASP.NET Code- behind What is t he difference bet ween a Web Server and a Web Browser? How t o open Child window in par ent window,C# windows Applicat ion More ...

Pr iv a t e is hidden and usable only by t he class it self. No code using a class inst ance can access a priv at e m em ber dir ect ly and neit her can a descendant class.

Pr ot e ct e d m em bers are sim ilar t o pr iv at e ones in t hat t hey ar e accessible only by t he cont aining class. How ever , prot ect ed m em bers also m ay be used by a descendant class. So m em bers t hat ar e likely t o be needed by a descendant class should be m arked prot ect ed.

I n t e r n a l/ Fr ie n d is public t o t he ent ir e applicat ion but pr ivat e t o any out side applicat ions. I nt er nal is useful when y ou w ant t o allow a class t o be used by ot her applicat ions but reserv e special funct ionalit y for t he applicat ion t hat cont ains t he class. I nt ernal is used by C# and Fr iend by VB .NET.

Pr ot e ct e d I n t e r n a l m ay be accessed only by a descendant class t hat 's cont ained in t he sam e applicat ion as it s base class. You use prot ect ed int ernal in sit uat ions w here you w ant t o deny access t o par t s of a class funct ionalit y t o any descendant classes found in ot her applicat ions.

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 3 of 15

Com posit ion of a n OBJECT We use an int erface t o get access t o an obj ect 's dat a and behavior . The obj ect 's dat a and behav ior s ar e cont ained w it hin t he obj ect , so a client applicat ion can t r eat t he obj ect lik e a black box accessible only t hrough it s int erface. This is a k ey obj ect - orient ed concept called Encapsulat ion. The idea is t hat any pr ogr am s t hat m ak e use of t his obj ect w on't have direct access t o t he behav iors or dat a- but r at her t hose pr ogram s m ust m ake use of our obj ect 's int er face. There are t hree m ain part s of Obj ect : 1. I nt erface 2. I m plem ent at ion or Behav ior 3. Mem ber or I nst ance v ariables I n t e r fa ce The int erface is defined as a set of m et hods ( Sub and Funct ion r out ines) , pr oper t ies ( Pr oper t y r out ines) , ev ent s, and fields ( v ariables or at t ribut es) t hat ar e declar ed Public in scope. I m ple m e n t a t ion or Be h a v ior The code inside of a m et hod is called t he im plem ent at ion. Som et im es it is also called behav ior since it is t his code t hat act ually m ak es t he obj ect do useful w or k. Client applicat ions can use our obj ect even if we change t he im plem ent at ion- as long as w e don't change t he int er face. As long as our m et hod nam e and it s par am et er list and r et urn dat a t ype r em ain unchanged, we can change t he im plem ent at ion all w e want . So Met hod Signat ure depends on: Met hod nam e Dat a t y pes of param et ers Eit her Param et er is passed ByVal or ByRef. Ret urn t y pe of m et hod. I t is im port ant t o k eep in m ind t hat encapsulat ion is a synt act ic t ool- it allows our code t o cont inue t o r un w it hout change. Howev er, it is not sem ant ic- m eaning t hat , j ust because our code cont inues t o run, doesn't m ean it cont inues t o do w hat w e act ually want ed it t o do. M e m be r or I n st a n ce V a r ia ble s The t hird key part of an obj ect is it s dat a, or st at e. Ever y inst ance of a class is absolut ely ident ical in t er m s of it s int erface and it s im plem ent at ion- t he only t hing t hat can var y at all is t he dat a cont ained wit hin t hat part icular obj ect . Mem ber v ariables are t hose declared so t hat t hey ar e available t o all code wit hin our class. Ty pically m em ber v ariables are Priv at e in scope- av ailable only t o t he code in our class it self. They ar e also som et im es r efer r ed t o as inst ance v ariables or as at t ribut es. The .NET Fr am ework also r efer s t o t hem as fields. We shouldn't confuse inst ance variables wit h pr oper t ies. A Pr oper t y is a t y pe of m et hod t hat is gear ed ar ound r et r ieving and set t ing v alues, while an inst ance v ar iable is a var iable w it hin t he class t hat m ay hold t he value exposed by a Propert y . I nt erface look s like a class, but has no im plem ent at ion. The only t hing it cont ains is definit ions of event s, indexer s, m et hods and/ or propert ies. The r eason int er faces only prov ide definit ions is because t hey are inher it ed by classes and st ruct s, which m ust pr ovide an im plem ent at ion for each int erface m em ber defined. So, what are int er faces good for if t hey don't im plem ent funct ionalit y? They'r e gr eat for put t ing t oget her plug- n- play like archit ect ures w here com ponent s can be int er changed at will. Since all int erchangeable com ponent s im plem ent t he sam e int erface, t hey can be used w it hout any ex t ra pr ogram m ing. The int er face for ces each com ponent t o ex pose specific public m em bers t hat will be used in a cer t ain w ay . Because int erfaces m ust be defined by inher it ing classes and st r uct s, t hey define a cont r act . For inst ance, if class foo inherit s from t he I Disposable int erface, it is m ak ing a st at em ent t hat it guar ant ees it has t he Dispose( ) m et hod, which is t he only m em ber of t he I Disposable int erface. Any code t hat wishes t o use class foo m ay check t o see if class foo inherit s I Disposable. When t he answ er is t rue, t hen t he code know s t hat it can call foo.Dispose( ) . Defining an I nt erface: My I nt erface.c int erface I MyI nt erface { v oid Met hodToI m plem ent ( ) ; } Abov e list ing shows defines an int er face nam ed I My I nt er face. A com m on nam ing convent ion is t o pr efix all int er face nam es w it h a capit al "I ", but t his is not m andat or y . This int erface has a single m et hod nam ed Met hodToI m plem ent ( ) . This could have been any t y pe of m et hod declar at ion w it h differ ent param et ers and ret ur n t y pes. Not ice t hat t his m et hod does not hav e an im plem ent at ion ( inst ruct ions bet w een curly br aces- { } ) , but inst ead ends w it h a sem i- colon, " ; ". This is because t he int erface only specifies t he signat ure of m et hods t hat an inher it ing class or st r uct m ust im plem ent . All t he m et hods of I nt erface are public by default and no access m odifier s ( like pr ivat e, public) ar e allow ed w it h any m et hod of I nt erface.

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 4 of 15

Using an I nt erface: I nt erfaceI m plem ent er .cs

class I nt erfaceI m plem ent er : I My I nt erface { public v oid Met hodToI m plem ent ( ) { Console.Writ eLine( "Met hodToI m plem ent ( ) called." ) ; } }

The I nt erfaceI m plem ent er class in abov e list ing im plem ent s t he I MyI nt er face int er face. I ndicat ing t hat a class inherit s an int erface is t he sam e as inherit ing a class. I n t his case, t he follow ing sy nt ax is used: class I nt erfaceI m plem ent er : I My I nt erface Not e t hat t his class inherit s t he I MyI nt er face int er face; it m ust im plem ent it s all m em bers. While im plem ent ing int erface m et hods all t hose needs t o be declar ed public only. I t does t his by im plem ent ing t he Met hodToI m plem ent ( ) m et hod. Not ice t hat t his m et hod im plem ent at ion has t he exact sam e signat ur e, par am et er s and m et hod nam e, as defined in t he I MyI nt erface int erface. Any difference will cause a com piler er ror . I nt erfaces m ay also inher it ot her int er faces. Follow ing list ing shows how inherit ed int erfaces ar e im plem ent ed. I nt erface I nherit ance: I nt erfaceI nher it ance.cs

using Syst em ; int erface I Parent I nt erface { v oid Parent I nt erfaceMet hod( ) ; } int erface I MyI nt erface : I Parent I nt er face { v oid Met hodToI m plem ent ( ) ; } class I nt erfaceI m plem ent er : I My I nt erface { public v oid Met hodToI m plem ent ( ) { Console.Writ eLine( "Met hodToI m plem ent ( ) called." ) ; } public v oid Parent I nt erfaceMet hod( ) { Console.Writ eLine( "Parent I nt erfaceMet hod( ) called." ) ; } }

The code in abov e list ing cont ains t w o int er faces: I My I nt er face and t he int er face it inher it s, I Par ent I nt er face. When one int erface inherit s anot her, any im plem ent ing class or st r uct m ust im plem ent ev er y int erface m em ber in t he ent ir e inherit ance chain. Since t he I nt erfaceI m plem ent er class in abov e list ing inher it s fr om I My I nt erface, it also inherit s I Parent I nt erface. Therefore, t he I nt er faceI m plem ent er class m ust im plem ent t he Met hodToI m plem ent ( ) m et hod specified in t he I My I nt erface int erface and t he Parent I nt er faceMet hod( ) m et hod specified in t he I Par ent I nt er face int er face. I n sum m ary , y ou can im plem ent an int erface and use it in a class. I nt erfaces m ay also be inher it ed by ot her int erface. Any class or st ruct t hat inherit s an int er face m ust also im plem ent all m em ber s in t he ent ir e int erface inher it ance chain. I n h e r it a n ce is t he idea t hat one class, called a subclass, can be based on anot her class, called a base class. I nherit ance provides a m echanism for creat ing hier ar chies of obj ect s. I nherit ance is t he abilit y t o apply anot her class's int er face and code t o y our ow n class. Norm al base classes m ay be inst ant iat ed t hem selv es, or inher it ed. Der ived classes can inherit base class m em ber s m ark ed w it h prot ect ed or great er access. The deriv ed class is specialized t o provide m or e funct ionalit y , in addit ion t o w hat it s base class prov ides. I nherit ing base class m em ber s in der iv ed class is not m andat or y . Acce ss Ke y w or ds base - > Access t he m em bers of t he base class. t his - > Refer t o t he current obj ect for w hich a m et hod is called. The base k ey word is used t o access m em ber s of t he base class from w it hin a der ived class: Call a m et hod on t he base class t hat has been over r idden by anot her m et hod. Specify which base- class const r uct or should be called w hen creat ing inst ances of t he der ived class. A base class access is per m it t ed only in a const r uct or, an inst ance m et hod, or an inst ance propert y accessor . I n follow ing exam ple, bot h t he base class, Per son, and t he deriv ed class, Em ploy ee, hav e a m et hod nam ed Get info. By using t he base k ey word, it is possible t o call t he Get info m et hod on t he base class, fr om w it hin t he der ived class. / / Accessing base class m em bers

using Syst em ; public class Person { prot ect ed st ring ssn = " 444- 55- 6666" ; prot ect ed st ring nam e = " John L. Malgr aine" ; public v irt ual v oid Get I nfo( ) { Console.Writ eLine( "Nam e: { 0} " , nam e) ; Console.Writ eLine( "SSN: { 0} " , ssn) ; } } class Em ployee: Person { public st ring id = " ABC567EFG" ;

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 5 of 15

public override void Get I nfo( ) { / / Calling t he base class Get I nfo m et hod: base.Get I nfo( ) ; Console.Writ eLine( "Em ployee I D: { 0} ", id) ; } } class Test Class { public st at ic v oid Main( ) { Em ploy ee E = new Em ployee( ) ; E.Get I nfo( ) ; } } Out put Nam e: John L. Malgraine SSN: 444- 55- 6666 Em ploy ee I D: ABC567EFG Base class const ruct ors can be called fr om der iv ed classes. To call a base class const r uct or , use t he base( ) const r uct or reference. This is desirable when it 's necessar y t o init ialize a base class appr opr iat ely. Here's an exam ple t hat show s t he der ived class const r uct or wit h an addr ess par am et er: abst ract public class Cont act { priv at e st ring address; public Cont act ( st ring b_address) { t his.address = b_address; } } public class Cust om er : Cont act { public Cust om er( st ring c_address) : base( C_address) { } } I n t his code, t he Cust om er class does not hav e an addr ess, so it passes t he par am et er t o it s base class const r uct or by adding a colon and t he base k ey w or d wit h t he par am et er t o it s declar at ion. This calls t he Cont act const r uct or w it h t he address param et er, w here t he addr ess field in Cont act is init ialized. One m ore exam ple which shows how base- class const r uct or is called w hen cr eat ing inst ances of a der iv ed class: using Syst em ; public class My Base { int num ; public My Base( ) { Console.Writ eLine( "I n MyBase( ) ") ; } public My Base( int i) { num = i; Console.Writ eLine( "in My Base( int i) ") ; } public int Get Num ( ) { ret urn num ; } } public class My Derived : MyBase { st at ic int i = 32; / / This const ruct or w ill call My Base.My Base( ) public My Deriv ed( int ii) : base( ) { } / / This const ruct or w ill call My Base.My Base( int i) public My Deriv ed( ) : base( i) { } public st at ic v oid Main( ) { My Deriv ed m d = new My Deriv ed( ) ; / / calls public MyDer iv ed( ) : base( i) and / / passes i= 32 in base class My Deriv ed m d1 = new My Deriv ed( 1) ; / / call public MyDer iv ed( ) : base( i) } } Out put in My Base( int i) in My Base( ) The follow ing ex am ple will not com pile. I t illust r at es t he effect s of not including a default const ruct or in a class definit ion: abst ract public class Cont act { priv at e st ring address; public Cont act ( st ring address)

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 6 of 15

{ t his.address = address; } } public class Cust om er : Cont act { public Cust om er( st ring address) { } } I n t his ex am ple, t he Cust om er const r uct or does not call t he base class const r uct or . This is obv iously a bug, since t he address field w ill nev er be init ialized. When a class has no ex plicit const ruct or , t he syst em assigns a default const ruct or . The default const r uct or aut om at ically calls a default or param et erless base const r uct or . Her e's an ex am ple of aut om at ic default const r uct or gener at ion t hat w ould occur for t he preceding ex am ple: public Cust om er( ) : Cont act ( ) { } When a class does not declare any const ruct or s, t he code in t his exam ple is aut om at ically gener at ed. The default base class const ruct or is called im plicit ly w hen no derived class const ruct or s are defined. Once a der iv ed class const r uct or is defined, whet her or not it has param et ers, a default const ruct or will not be aut om at ically defined, as t he preceding code show ed. Ca llin g Ba se Cla ss M e m be r s Derived classes can access t he m em bers of t heir base class if t hose m em bers have pr ot ect ed or gr eat er access. Sim ply use t he m em ber nam e in t he appropr iat e cont ex t , j ust as if t hat m em ber w er e a part of t he der ived class it self. Here's an ex am ple: abst ract public class Cont act { priv at e st ring address; priv at e st ring cit y ; priv at e st ring st at e; priv at e st ring zip; public st ring FullAddress( ) { st ring fullAddress = address + '\ n' + cit y + ',' + st at e + ' ' + zip; ret urn fullAddress; } } public class Cust om er : Cont act { public st ring Generat eReport ( ) { st ring fullAddress = FullAddress( ) ; / / do som e ot her st uff... ret urn fullAddress; } } I n above exam ple, t he Generat eReport ( ) m et hod of t he Cust om er class calls t he FullAddr ess( ) m et hod in it s base class, Cont act . All classes hav e full access t o t heir ow n m em ber s w it hout qualificat ion. Qualificat ion r efer s t o using a class nam e w it h t he dot operat or t o access a class m em ber - My Obj ect .Som eMet hod( ) , for inst ance. This show s t hat a deriv ed class can access it s base class m em bers in t he sam e m anner as it s own. More Tips regarding I nherit ance: A st at ic m em ber cannot be m ar k ed as ov err ide, v ir t ual, or abst r act . So follow ing is an err or: public st at ic v irt ual v oid Get SSN( ) You can't call st at ic m et hods of base class from der ived class using base k ey w or d. I n above exam ple if you declar e a st at ic m et hod as follow s:

public class Person { prot ect ed st ring ssn = " 444- 55- 6666" ; prot ect ed st ring nam e = " John L. Malgr aine" ; public st at ic v oid Get I nfo( ) { / / I m plem ent at ion } } now you can't call t his m et hod using base.Get I nfo( ) from der ived class inst ead you have t o call Person.Get I nfo( ) from derived class. I nside St at ic m em bers w e can access only st at ic fields, m et hods et c. Follow ing exam ple w ill give error, because w e can't access nam e in Get I nfo( ) because nam e is not st at ic. public class Person {

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 7 of 15

prot ect ed st ring ssn = " 444- 55- 6666" ; prot ect ed st ring nam e = " John L. Malgr aine" ; public st at ic v oid Get I nfo( ) { Console.Writ eLine( "Nam e: { 0} " , nam e) ; Console.Writ eLine( "SSN: { 0} " , ssn) ; } }

Virt ual or abst ract m em bers cannot be priv at e. I f y ou are not ov erriding a vir t ual m et hod of base class in deriv ed class, y ou can't use base class m et hod by using base key word in derived class. Also w hen you w ill cr eat e an inst ance of der iv ed class, it w ill call der iv ed class m et hod and you w ill only be able t o access base class m et hod w hen you w ill cr eat e inst ance of base class. You can't decrease access lev el of a m et hod in der ived class when y ou ar e ov err iding a base class m et hod in derived class, v ice versa is possible. Means y ou can m ake prot ect ed m et hod of base class t o public in derived class. The " t his" k eyw ord refers t o: t he current inst ance for which a m et hod is called. St at ic m em ber funct ions do not have a t his point er . The t his k eyw ord can be used t o access m em ber s fr om w it hin const r uct ors, inst ance m et hods, and inst ance accessors. The follow ing are com m on uses of t his:

To qualify m em bers hidden by sim ilar nam es, for exam ple: public Em ploy ee( st ring nam e, st ring alias) { t his.nam e = nam e; t his.alias = alias; }

I n above exam ple, t his.nam e refers t o pr ivat e var iable nam e in t he class. I f we wr it e nam e = nam e, t hen t his w ill refer t o argum ent nam e of t he const r uct or Em ployee and not t o pr ivat e var iable nam e in t he class. I n t his case privat e v ariable nam e w ill nev er be init ialized. To pass an obj ect as a param et er t o ot her m et hods, for exam ple: CalcTax( t his) ; To declare index ers, for exam ple:

public int t his [ int param ] { get { ret urn array [ param ] ; } set { array[ param ] = value; } }

I t is an error t o refer t o t his in a st at ic m et hod, st at ic pr oper t y accessor, or v ar iable init ializer of a field declarat ion. I n t his ex am ple, t his is used t o qualify t he Em ployee class m em ber s, nam e and alias, which ar e hidden by sim ilar nam es. I t is also used t o pass an obj ect t o t he m et hod CalcTax , w hich belongs t o anot her class. / / keyw ords_t his.cs / / t his ex am ple using Syst em ; public class Em ployee { public st ring nam e; public st ring alias; public decim al salary = 3000.00m ; / / Const ruct or: public Em ploy ee( st ring nam e, st ring alias) { / / Use t his t o qualify t he fields, nam e and alias: t his.nam e = nam e; t his.alias = alias; } / / Print ing m et hod: public v oid print Em ploy ee( ) { Console.Writ eLine( "Nam e: { 0} \ nAlias: { 1} " , nam e, alias) ; / / Passing t he obj ect t o t he CalcTax m et hod by using t his: Console.Writ eLine( "Taxes: { 0: C} ", Tax .CalcTax( t his) ) ; } } public class Tax { public st at ic decim al CalcTax ( Em ploy ee E) { ret urn ( 0.08m * ( E.salary ) ) ;

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 8 of 15

} } public class MainClass { public st at ic v oid Main( ) { / / Creat e obj ect s: Em ploy ee E1 = new Em ploy ee ( " John M. Trainer" , "j t r ainer" ) ; / / Display result s: E1.print Em ployee( ) ; } }

Out put Nam e: John M. Trainer Alias: j t rainer Tax es: $240.00 Abst r a ct Cla sse s Abst ract classes are a special t ype of base classes. I n addit ion t o nor m al class m em ber s, t hey hav e abst r act class m em bers. These Abst ract class m em ber s ar e m et hods and pr oper t ies t hat ar e declar ed w it hout an im plem ent at ion. All classes deriv ed direct ly from abst ract classes m ust im plem ent all of t hese abst r act m et hods and pr oper t ies. Abst ract classes can nev er be inst ant iat ed. This w ould be illogical, because of t he m em ber s w it hout im plem ent at ions.So w hat good is a class t hat can't be inst ant iat ed? Lot s! Abst r act classes sit t ow ar d t he t op of a class hierar chy. They est ablish st ruct ure and m eaning t o code. They m ak e fr am ew or k s easier t o build. This is possible because abst r act classes hav e inform at ion and behav ior com m on t o all der ived classes in a fram ew or k. Tak e a look at t he following ex am ple: abst ract public class Cont act / / Abst ract Class Cont act . { prot ect ed st ring nam e; public Cont act ( ) { / / st at em ent s... } public abst ract void generat eReport ( ) ; abst ract public st ring Nam e { get ; set ; } }

Cont act , is an abst ract class. Cont act has t w o abst ract m em ber s, and it has an abst r act m et hod nam ed gener at eReport ( ) . This m et hod is declared wit h t he abst r act m odifier in fr ont of t he m et hod declarat ion. I t has no im plem ent at ion ( no braces) and is t erm inat ed w it h a sem icolon. The Nam e pr oper t y is also declar ed abst r act . The accessor s of pr oper t ies are t erm inat ed w it h sem icolons. public class Cust om er : Cont act / / Cust om er I nher it s Abst ract Class Cont act . { st ring gender; decim al incom e; int num berOfVisit s; public Cust om er( ) { / / st at em ent s } public override void generat eReport ( ) { / / unique report } public override st ring Nam e { get { num berOfVisit s+ + ; ret urn nam e; } set { nam e = value; num berOfVisit s = 0; } } } public class Sit eOwner : Cont act { int sit eHit s; st ring m y Sit e; public Sit eOw ner( ) { / / st at em ent s } public override void generat eReport ( ) { / / unique report } public override st ring Nam e

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 9 of 15

{ get { sit eHit s+ + ; ret urn nam e; } set { nam e = value; sit eHit s = 0; } } }

The abst ract base class Cont act has t w o der iv ed classes, Cust om er and Sit eOw ner . Bot h of t hese der ived classes im plem ent t he abst ract m em bers of t he Cont act class. The generat eReport ( ) m et hod in each der ived class has an override m odifier in it s declarat ion. Lik ewise, t he Nam e declar at ion cont ains an ov err ide m odifier in bot h Cust om er and Sit eOw ner. C# requires explicit declarat ion of int ent w hen ov err iding m et hods. This feat ur e prom ot es safe code by avoiding t he accident al ov erriding of base class m et hods, w hich is w hat act ually does happen in ot her languages. Leav ing out t he override m odifier generat es an error . Sim ilar ly, adding a new m odifier also generat es an er r or . Abst r act m et hods m ust be overridden and cannot be hidden, w hich t he new m odifier or t he lack of a m odifier would be t ry ing t o do. The m ost fam ous of all abst ract classes is t he Obj ect class. I t m ay be r efer r ed t o as obj ect or Obj ect , but it 's st ill t he sam e class. Obj ect is t he base class for all ot her classes in C# . I t 's also t he default base class when a base class is not specified. The follow ing class declar at ions pr oduce t he sam e exact r esult s: abst ract public class Cont act : Obj ect { / / class m em bers } abst ract public class Cont act { / / class m em bers } Obj ect is im plicit ly included as a base class if it is not alr eady declar ed. Besides pr oviding t he abst ract glue t o hold t oget her t he C# class fram ew ork , obj ect includes built - in funct ionalit y, som e of which is useful for der iv ed classes t o im plem ent . D iffe r e n ce b e t w e e n I n t e r fa ce a n d Abst r a ct Cla ss I nt erfaces are closely relat ed t o abst r act classes t hat hav e all m em bers abst r act . For an abst ract class, at least one m et hod of t he class m ust be an abst r act m et hod t hat m eans it m ay have concret e m et hods. For an int erface, all t he m et hods m ust be abst r act Class t hat im plem ent s an int erface m uch provide concr et e im plem ent at ion of all t he m et hods definit ion in an int erface or else m ust be declar e an abst r act class I n C# , m ult iple inherit ance is possible only t hr ough im plem ent at ion of m ult iple int erfaces. Abst r act class can only be deriv ed once. An int erface defines a cont ract and can only cont ains four ent it ies viz m et hods, pr oper t ies, event s and index es. An int erface t hus cannot cont ain const ant s, fields, oper at or s, const r uct or s, dest r uct ors, st at ic const r uct ors, or t y pes. Also an int erface cannot cont ain st at ic m em bers of any kind. The m odifier s abst ract , public, pr ot ect ed, int er nal, privat e, v irt ual, ov erride is disallowed, as t hey m ake no sense in t his cont ex t . Class m em bers t hat im plem ent t he int er face m em bers m ust be publicly accessible. Ove r r idin g Su m m e r y: A derived class m ay ov erride a v irt ual m et hod of t he base class w it h t he keyw or d ov er ride. The follow ing r est r ict ions m ust be followed. Key word override is used in t he definit ion of child class m et hod t hat is going t o over r ide t he base class's vir t ual m et hod. The ret urn t y pe m ust be t he sam e as t he v irt ual m et hod have in base class. The nam e of t he m et hod should also be sam e. The param et er - list m ust also be sam e in order , num ber and t ype of par am et er s. The accessibilit y of t he overriding m et hod should not be m ore r est r ict ed t han t hat of t he accessibilit y defined wit h v irt ual m et hod of t he base class. This accessibilit y eit her be t he sam e or less rest r ict ed. The v irt ual m et hods can be sealed in t he child or der iv ed classes t o pr event fur t her m odificat ions in t he im plem ent at ion of t he v irt ual m et hod in t he deriv ed classes, by declar ing t hem as sealed m et hods.

H id in g Ba se Cla ss M e m be r s Som et im es derived class m em bers hav e t he sam e nam e as a cor r esponding base class m em ber . I n t his case, t he derived m em ber is said t o be "hiding" t he base class m em ber . When hiding occurs, t he deriv ed m em ber is m ask ing t he funct ionalit y of t he base class m em ber . User s of t he der ived class w on't be able t o see t he hidden m em ber ; t hey 'll see only t he der ived class m em ber . The follow ing code shows how hiding a base class m em ber w ork s. abst ract public class Cont act { priv at e st ring address; priv at e st ring cit y ; priv at e st ring st at e; priv at e st ring zip; public st ring FullAddress( ) { st ring fullAddress = address + '\ n' + cit y + ',' + st at e + ' ' + zip; ret urn fullAddress;

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 10 of 15

} } public class Sit eOwner : Cont act { public st ring FullAddress( ) { st ring fullAddress; / / creat e an address... ret urn fullAddress; } }

I n t his ex am ple, bot h Sit eOw ner and it s base class, Cont act , hav e a m et hod nam ed FullAddress( ) . The FullAddr ess( ) m et hod in t he Sit eOw ner class hides t he FullAddr ess( ) m et hod in t he Cont act class. This m eans t hat when an inst ance of a Sit eOw ner class is invoked w it h a call t o t he FullAddr ess( ) m et hod, it is t he Sit eOw ner class FullAddr ess( ) m et hod t hat is called, not t he FullAddress( ) m et hod of t he Cont act class. Alt hough a base class m em ber m ay be hidden, t he derived class can st ill access it . I t does t his t hrough t he base ident ifier. Som et im es t his is desirable. I t is oft en useful t o t ake advant age of t he base class funct ionalit y and t hen add t o it wit h t he deriv ed class code. The nex t ex am ple shows how t o r efer t o a base class m et hod from t he derived class. abst ract public class Cont act { priv at e st ring address; priv at e st ring cit y ; priv at e st ring st at e; priv at e st ring zip; public st ring FullAddress( ) { st ring fullAddress = address + '\ n' + cit y + ',' + st at e + ' ' + zip; ret urn fullAddress; } } public class Sit eOwner : Cont act { public st ring FullAddress( ) { st ring fullAddress = base.FullAddress( ) ; / / do som e ot her st uff... ret urn fullAddress; } }

I n t his part icular ex am ple, t he FullAddr ess( ) m et hod of t he Cont act class is called from w it hin t he FullAddr ess( ) m et hod of t he Sit eOw ner class. This is accom plished wit h a base class r efer ence. This pr ov ides anot her w ay t o r euse code and add on t o it wit h cust om ized behav ior. V e r sion in g Versioning, in t he cont ext of inherit ance, is a C# m echanism t hat allow s m odificat ion of classes ( cr eat ing new ver sions) w it hout accident ally changing t he m eaning of t he code. Hiding a base class m em ber w it h t he m et hods pr ev iously described generat es a w arning m essage from t he com piler . This is because of t he C# v ersioning policy. I t 's designed t o elim inat e a class of problem s associat ed w it h m odificat ions t o base classes. Here's t he scenario: A developer cr eat es a class t hat inher it s from a t hird - par t y libr ar y . For t he pur poses of t his discussion, w e assum e t hat t he Cont act class r epr esent s t he t hir d- par t y libr ary . Her e's t he ex am ple: public class Cont act { / / does not include FullAddress( ) m et hod } public class Sit eOwner : Cont act { public st ring FullAddress( ) { st ring fullAddress = m ySit e.ToSt ring( ) ; ret urn fullAddress; } }

I n t his ex am ple, t he FullAddress( ) m et hod does not ex ist in t he base class. Ther e is no pr oblem y et . Lat er on, t he creat ors of t he t hird- part y library updat e t heir code. Par t of t his updat e includes a new m em ber in a base class w it h t he ex act sam e nam e as t he derived class: public class Cont act { priv at e st ring address; priv at e st ring cit y ; priv at e st ring st at e; priv at e st ring zip; public st ring FullAddress( ) { st ring fullAddress = address + '\ n' + cit y + ',' + st at e + ' ' + zip; ret urn fullAddress; } } public class Sit eOwner : Cont act

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 11 of 15

{ public st ring FullAddress( ) { st ring fullAddress = m ySit e.ToSt ring( ) ; ret urn fullAddress; } } I n t his code, t he base class m et hod FullAddr ess( ) cont ains differ ent funct ionalit y t han t he der ived class m et hod. I n ot her languages, t his scenario w ould break t he code because of im plicit polym or phism . How ev er, t his does not br eak any code in C# because w hen t he FullAddress( ) m et hod is called on Sit eOwner , it is st ill t he Sit eOw ner class m et hod t hat get s called. This scenario generat es a w arning m essage. One w ay t o elim inat e t he w ar ning m essage is t o place a new m odifier in front of t he deriv ed class m et hod nam e, as t he follow ing exam ple show s: using Syst em ; public class WebSit e { public st ring Sit eNam e; public st ring URL; public st ring Descript ion; public WebSit e( ) { } public WebSit e( st ring st rSit eNam e, st r ing st r URL, st r ing st rDescr ipt ion ) { Sit eNam e = st rSit eNam e; URL = st rURL; Descript ion = st rDescript ion; } public override st ring ToSt ring( ) { ret urn Sit eNam e + " , " + URL + ", " + Descript ion; } } public class Cont act { public st ring address; public st ring cit y; public st ring st at e; public st ring zip; public st ring FullAddress( ) { st ring fullAddress = address + '\ n' + cit y + ',' + st at e + ' ' + zip; ret urn fullAddress; } } public class Sit eOwner : Cont act { int sit eHit s; st ring nam e; WebSit e m y Sit e; public Sit eOw ner( ) { m y Sit e = new WebSit e( ) ; sit eHit s = 0; } public Sit eOw ner( st ring aNam e, WebSit e aSit e) { m y Sit e = new WebSit e( aSit e.Sit eNam e,aSit e.URL,aSit e.Descr ipt ion) ; Nam e = aNam e; } new public st ring FullAddress( ) { st ring fullAddress = m ySit e.ToSt ring( ) ; ret urn fullAddress; } public st ring Nam e { get { sit eHit s+ + ; ret urn nam e; } set { nam e = value; sit eHit s = 0; } } } public class Test { public st at ic v oid Main( ) { WebSit e m y Sit e = new WebSit e( "Le Financier ","ht t p: / / w ww .LeFinancier.com ","Fancy Financial Sit e") ; Sit eOw ner anOwner = new Sit eOw ner ( " John Doe", m y Sit e) ; st ring address; anOw ner.address = " 123 Lane Lane" ; anOw ner.cit y = "Som e Tow n"; anOw ner.st at e = " HI " ;

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 12 of 15

anOw ner.zip = " 45678" ; address = anOwner.FullAddress( ) ; / / Differ ent Result s Console.Writ eLine( "Address: \ n{ 0} \ n" , addr ess) ; } } Here's t he out put : Address: Le Financier, ht t p: / / w ww .LeFinancier.com , Fancy Financial Sit e This has t he effect of explicit ly let t ing t he com piler know t he dev eloper 's int ent . Placing t he new m odifier in front of t he derived class m em ber st at es t hat t he dev eloper s k now t here is a base class m et hod wit h t he sam e nam e, and t hey definit ely want t o hide t hat m em ber . This pr event s br eakage of exist ing code t hat depends on t he im plem ent at ion of t he derived class m em ber. Wit h C# , t he m et hod in t he der ived class is called when an obj ect of t he der iv ed class t y pe is used. Lik ewise, t he m et hod in t he base class is called w hen an obj ect of t he Base class t y pe is called. Anot her pr oblem t his present s is t hat t he base class m ay pr esent som e desir able new feat ur es t hat wouldn't be available t hrough t he derived class. To use t hese new feat ures requires one of a few differ ent wor kar ounds. One opt ion w ould be t o r enam e t he der ived class m em ber, which w ould allow program s t o use a base class m et hod t hrough a der iv ed class m em ber . The dr awback t o t his opt ion w ould be if t here w ere ot her classes relying upon t he im plem ent at ion of t he der iv ed class m em ber w it h t he sam e nam e. This scenario w ill break code and, for t his r eason, is consider ed ex t r em ely bad for m . Anot her opt ion is t o define a new m et hod in t he deriv ed class t hat called t he base class m et hod. This allow s users of t he derived class t o have t he new funct ionalit y of t he base class, yet r et ain t heir ex ist ing funct ionalit y wit h t he deriv ed class. While t his w ould work, t here are m aint ainabilit y concer ns for t he deriv ed class. Se a le d Cla sse s Sealed classes are classes t hat can't be derived fr om . To pr event ot her classes fr om inherit ing fr om a class, m ake it a sealed class. There are a couple good r easons t o cr eat e sealed classes, including opt im izat ion and securit y . Sealing a class av oids t he sy st em ov erhead associat ed w it h vir t ual m et hods. This allow s t he com piler t o per for m cert ain opt im izat ions t hat are ot herwise unavailable w it h nor m al classes. Anot her good reason t o seal a class is for secur it y. I nher it ance, by it s ver y nat ure, dict at es a cer t ain am ount of pr ot ect ed access t o t he int ernals of a pot ent ial base class. Sealing a class does aw ay w it h t he possibilit y of cor r upt ion by deriv ed classes. A good exam ple of a sealed class is t he St ring class. The following exam ple show s how t o cr eat e a sealed class: public sealed class Cust om erSt at s { st ring gender; decim al incom e; int num berOfVisit s; public Cust om erSt at s( ) { } } public class Cust om erI nfo : Cust om er St at s / / er r or { } This exam ple generat es a com piler er r or . Since t he Cust om er St at s class is sealed, it can't be inher it ed by t he Cust om erI nfo class.The Cust om erSt at s class was m eant t o be used as an encapsulat ed obj ect in anot her class. This is show n by t he declarat ion of a Cust om er St at s obj ect in t he Cust om er class. public class Cust om er { Cust om erSt at s m ySt at s; / / ok ay } Poly m or ph ism Polym orphism is reflect ed in t he abilit y t o w r it e one r out ine t hat can oper at e on obj ect s fr om m or e t han one classt reat ing different obj ect s from differ ent classes in ex act ly t he sam e way . For inst ance, if bot h Cust om er and Vendor obj ect s hav e a Nam e propert y , and we can w r it e a r out ine t hat calls t he Nam e pr oper t y r egar dless of w het her we're using a Cust om er or Vendor obj ect , t hen w e have poly m or phism . A vehicle is a good exam ple of polym or phism . A vehicle int er face w ould only hav e t hose pr oper t ies and m et hods t hat all v ehicles hav e, a few of w hich m ight include paint color, num ber of door s, accelerat or , and ignit ion. These pr opert ies and m et hods would apply t o all t ypes of vehicles including car s, t r ucks, and sem i- t r uck s. Polym orphism w ill not im plem ent code behind t he vehicle's pr opert ies and m et hods. I nst ead, poly m or phism is t he im plem ent at ion of an int erface. I f t he car , t r uck , and sem it ruck all im plem ent t he sam e vehicle int erface, t hen t he client code for all t hree classes can be exact ly t he sam e. C# gives us poly m orphism t hrough inher it ance. C# provides a k ey w or d vir t ual t hat is used in t he definit ion of a m et hod t o support polym orphism . Child class are now free t o prov ide t heir ow n im plem ent at ion of t his v ir t ual m et hod, t hat is called ov er r iding. The follow ing point s are im port ant regar ding v ir t ual k eyw or d: I f t he m et hod is not v irt ual, t he com piler sim ply uses t he r efer ence t ype t o invok e t he appr opriat e m et hod. I f t he m et hod is v irt ual, t he com piler w ill generat e code t o check up t he r efer ence t y pe at r unt im e it is act ually denot ing t o, t hen t he appropriat e m et hod is called fr om t he class of t he r efer ence t ype. When a virt ual m et hod is called, runt im e check ( lat e m et hod binding) is m ade t o ident ify t he obj ect and appr opr iat e m et hod is invoked, all t his is done at r unt im e.

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 13 of 15

I n case of non- v irt ual m et hods, t his inform at ion is av ailable at com pile t im e, so no r unt im e check t o ident ify t he obj ect is m ade, so slight ly efficient in t he w ay non- v ir t ual m et hods are called. But t he behav ior of v ir t ual m et hod is useful in m any w ay s; t he funct ionalit y t hey prov ide is fair enough t o bear t his slight loss of perform ance. I m ple m e n t in g Polym or ph ism The k ey fact or here is t he abilit y t o dy nam ically invoke m et hods in a class based on t heir t y pe. Essent ially, a pr ogram w ould hav e a group of obj ect s, ex am ine t he t ype of each one, and execut e t he appropr iat e m et hod. Her e's an ex am ple: using Syst em ; public class WebSit e { public st ring Sit eNam e; public st ring URL; public st ring Descript ion; public WebSit e( ) { } public WebSit e( st ring st rSit eNam e, st r ing st r URL, st r ing st rDescr ipt ion ) { Sit eNam e = st rSit eNam e; URL = st rURL; Descript ion = st rDescript ion; } public override st ring ToSt ring( ) { ret urn Sit eNam e + " , " + URL + ", " + Descript ion; } } When w e inherit above class, w e have t w o choices t o invoke const r uct or of t he class. So t his is an exam ple of design t im e polym orphism . Here at design t im e w e hav e t o decide which m et hod we need t o inv ok e w hile inher it ing t he class. Polym orphism is t he capabilit y of a progr am t o car r y out dy nam ic oper at ions by im plem ent ing m et hods of m ult iple derived classes t hrough a com m on base class r efer ence. Anot her definit ion of poly m or phism is t he abilit y t o t reat different obj ect s t he sam e w ay. This m eans t hat t he runt im e t ype of an obj ect det erm ines it s behavior r at her t han t he com pile- t im e t ype of it s reference. Su m m e r y : I t was a long t rip, but I t ried t o explain all t he basics of Obj ect Or ient at ion in C# w it h som e pr act ical ex am ples. I f you are int erest ed t o read m y prev ious art icle " Mem ory m anagem ent in .NET" , you can find it at : ht t p: / / w ww .csharpcorner.com / UploadFile/ t k agar wal/ Mem or yManagem ent I nNet 11232005064832AM/ Mem or yManagem ent I nNet .aspx

Com m e nt Re qu est !

Thank you for reading t his post . Please post y our feedback , quest ion, or com m ent s about t his post Her e.
Login t o add your cont ent s and source code t o t his art icle [ Top] Abou t t he a ut hor t ush ar ka nt .aga rw al Ra t e t his art icle Look in g for C# Consu lt in g? C# Consulting is founded in 2002 by t he founders of C# Corner . Unlike a t radit ional consult ing com pany, our consult ant s are well- k nown expert s in .NET and m any of t hem ar e MVPs, aut hors, and t rainers. We specialize in Microsoft .NET developm ent and ut ilize Agile Developm ent and Ex t rem e Program m ing pr act ices t o provide fast pace quick t urnaround result s. Our soft ware dev elopm ent m odel is a m ix of Agile Developm ent , tr aditional SDLC, and Wat erfall m odels. Click her e t o lear n m ore about C# Consult ing. 1 2 3 4 5 Rat e it

I nt r oducing Ma xV - one click . in fin it e con t rol. H ype r - V Host ing from Ma xim u m ASP. Finally a vir tual platfor m t hat deliver s next - generat ion Windows Serv er 2008 Hyper - V virt ualizat ion t echnology from a m anaged hosting part ner y ou can t ruly depend on. Visit www.m axim um asp.com / m ax for a FREE 30 day t rial. Hur ry offer ends soon. Clim b aboard t he MaxV plat form and take advant age of High Availabilit y, I nt elligent Monit oring, Recurrent Backups, and Scalabilit y wit h no hassle or hidden fees. As a m anaged host ing part ner focused solely on Micr osoft t echnologies since 2000, Max im um ASP is uniquely qualified t o pr ov ide t he superior support t hat our business is built on. Unparalleled expert ise wit h Micr osoft technologies lead to work ing direct ly wit h Microsoft as first t o offer I I S 7 and SQL 2008 bet as in a host ed environm ent; part ner ing in the Go Live Program for Hyper - V; and product co- launches built on WS 2008 wit h Hyper - V t echnology.

Post a Fe e dba ck , Com m e n t , or Qu e st ion a b ou t t h is a r t icle


Subj e ct : Com m en t :

Sponsored by

Subm it

Becom e a Sponsor

Com m e nt s Polymorphism by Mahendra On February 9, 2009

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 14 of 15

Hi any one give me more I dea about polymorphism

Re ply | Em a il | M odify

Clarification by vimal On March 10, 2010

Hi , // Once a deriv ed class const r uct or is defined, w het her or not it has param et ers, a default const r uct or w ill not be aut om at ically defined, as t he pr eceding code showed.

My suggest ion is Default const r uct or w ill be t here it w ill be aut om at ically be cr eat ed. class Cust om er: Cont ract _Base_Test { public Cust om er( ) : base( " Hii" ) { } public st at ic v oid Main( ) { Cust om er cus = new Cust om er( ) ; }

class Cont ract _Base_Test { privat e st ring address; public Cont ract _Base_Test ( st r ing addr ess) { t his.address = address; Console.Writ eLine( address) ; } public Cont ract _Base_Test ( ) { Console.Writ eLine( " Hiiii") ; } } How t his class works fine.. please clar ify
Re ply | Em a il | M odify

Comment

Very good article to understand oops concept in c#

by dileep On March 16, 2010

Re ply | Em a il | M odify

Excillent

by keyur On August 16, 2010 I ts very very good article........ excillant

Re ply | Em a il | M odify

Re: Excillent

dai poi solladha...wt u understand sollu pakalam ???

by jhn On April 14, 2011

Re ply | Em a il | M odify

very good by biju On November 4, 2010


very good article.

Re ply | Em a il | M odify

Re: Protected I nternal picture in w rong by Chandu On November 21, 2010


Dear Author, I thing Pr ot e ct e d I n t e r n a l pict ur e is wr ong. The m et hods in t he Base class can be accessible t o t he child class of ot her assem bly .

Regards, Chandu
Re ply | Em a il | M odify

Re: Re: Protected I nternal picture in wrong by Abhilash On November 7, 2011

Hi Chandu, I agree. In Protected Internal, the Internal part allows the member to be accessed by all classes in the assembly and the Protected part allows access to the member to derived classes in other assemblies. Regards, Abhilash

Re ply | Em a il | M odify

Popular article? by Kare On January 24, 2011

http:/ / dashingboy.wordpress.com/ 2009/ 03/ 14/ object-oriented-concepts-in-c/

http:/ /mahendran

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012

Object Oriented Concepts in C#

Page 15 of 15

gateway.blogspot.com/ 2010/ 06/ inheritance.html http:/ / bbpwebdeveloper.blogspot.com/ 2010/ 07/ object-orientedprogramming.html http:/ / sharepointweblog.blogspot.com/ 2010_01_01_archive.html http:/ /vikramksohpaul.blogspot.com/ http:/ / naveensoftwaretesting.blogspot.com/ 2010/ 08/oops-concepts-in-c.html (line shifts don't seem to work)
Re ply | Em a il | M odify

Nice !!!!!!! by jhn On April 14, 2011


mokka topic..Try to I mprove!!!

Re ply | Em a il | M odify

protected I nternal by sammit

Good explanation, Great Article with pictures. But your protected internal definition is completely wrong. I am little bit worried as the interview candidates may recite your definition and answer wrong. ProtectedInternal can be accessed with in the assembly anywhere by Inherting/ Creating instance AND accessed outside the assembly by only Inherting the Contained Class so protectedI nternal behaves like both protected or internal. see a good article at: http:/ /kossovsky.net/index.php/ 2009/ 06/ csharprotected-internal/
Re ply | Em a il | M odify

On August 4, 2011

Object Oriented Concepts in C#


Very Good

by Samar On September 21, 2011

Re ply | Em a il | M odify

Very Helpful - nice job! by Jeff On December 6, 2011

Thanks for putting this together, this was better than other OOP articles I've recently been reading, and helped me to better understand Interfaces, Abstract Classes, and modes of inheritance.

Re ply | Em a il | M odify

nice by Jignesh On January 16, 2012


very good article and it very helpful

Re ply | Em a il | M odify

I ntroduction to OOP by godw in On March 3, 2012


Excellent presentation

Re ply | Em a il | M odify

comments about interface by Webtech On March 26, 2012

I nterfaces consist of methods, properties, events, indexers, or any combination of those four member types. < bold> An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. < bold>

Re ply | Em a il | M odify

Excellent

excellent article for basic concepts

by muhammad On March 27, 2012

Re ply | Em a il | M odify

Hosted by Maxim um ASP | Found a br ok en link ? | Cont act Us | Term s & condit ions | Privacy Policy | Sit e Map | Suggest an I dea | Adver tise With Us Cur rent Version: 5.2011.8.6 2012 cont ent s copyright of t heir aut hors. Rest everyt hing copyright Mindcracker. All right s reserved.

Ch an nels: Sponsors:

Jobs | I nt erv iews | Consulting | Training | Phot os | Author s | Tips | Forum s | Blogs C# | Visual Basic | ASP.NET & Web Developm ent | C+ + | Other .NET Languages | Windows Vist a | XAML | Tut orials ASP.NET Host ing | Dynam icPDF | Nevron | Team Foundation Host ing

Progra m m ing:

http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11... 20-04-2012