• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Inheritance in C# .Net

Inheritance

Inheritance is a concept of deriving the features from one class to another class.

Inheritance leads to code reusability.

Inheritance also saves memory.

Observation

Screenshot_47

 

Class EMP

{

P in temp no

(or)

Private int  ENO;

Private String EName;

Private int Sal;

}

 

Class worker (wrk)

{

28 bytes

}

Class manager : wrk

{

12 bytes

}

 

Types of Inheritance

Single Inheritance

 

Class A

{

}

Class B:A

{

}

 

Screenshot_48

 

Class A

{

P1 ()

}

Class B:A

{

P2 ()

}

 

Multiple Inheritance

Screenshot_49

 

Class A Base class

{

}

Class B:A derived class

{

}

Class C:B derived class

{

}

 

Hierarchical Inheritance

 

Screenshot_50

 

 

Multiple Inheritance

 

Screenshot_51

 

 

Class c : A, B

C obj = new c ()

Obj .P1 ()

Not possible  (or)  supported in .net.

 

Types of Inheritance

* Multiple inheritance in not supported directly in C #.net.

*  But a similar structure is supported with the help of Inheritance.

 

Hybrid Inheritance

 

Screenshot_52

 

It is not supported

It contains multiple Inheritance

 

Logical Diagram in Inheritance concept

B obj = new B()

 

Screenshot_53

Screenshot_54

 

 

Private data is accessible only from the class where these are declared.

Protected data is accessible from ALL SUB CLASSES (derived class )also.

Class B : Private A -à C ++ Concept

Public data is accessible in derived class as well as from objects

 

Ex 1:- A company gives Bonus , when they are reaching the target , then calculate total salary.

ENO

SAL

BONUS

%S (Total Salary)

 

Open windows forms application project

Place a button

Code in GD

 

 

Class EMP

{

Private int ENO = 101;

Protected  int Sal = 5000;

Public void print ()

{

Message. Box. Show   (E no + “” + Sal);

}//print

}// class (EMP)

 

Class bonus  : EMP

{

Private int b, ts;

Public void calculate (int n)

{

B = n;

Print ();

Ts = Sal +b;

Message  Box. Show (“Total”  + ts);

}

}//Bonus

Code for Button1_click

{

Dialog Result dr;

 

Dr =  Message Box . show (“R u getting bonus”, “Peers”,

Message Box Buttons. Yes No,

Message Box Icon .Question );

If (dr = = Dialog Result .yes)

{

Bonus obj = new bonus ();

Obj. Calculate (3000);

}

Else

{

EMP X = new EMP ();

print ();

}                     (or) new EMP ().Print (); // name less object

}

 

Working with overriding (Runtime Polymorphism)

Screenshot_56

 

Public void Print ()

{

MB.S (“A”)

}

Public void show ()

{

MB.S (“B”);

Print ();

}

B obj = new B ();

Obj. Show ()

Obj. Print ()

 

Overriding is a concept of having two methods with same name and same arguments in Base and derived classes.

In overriding by default the priority will be given to local class methods.

In overriding, in order to access parent class method s, “base” keyword is required.

”base” keyword can be used with methods, fields and constructors also.

 

Screenshot_57

 

{

MBS (“A”);

}

{

MBS (“B”);

} base. show ();

{

MBS (“C”);

} base. Show ();

New c () .show ();

Example on overriding Fields

arrrowopen window forms application project

arrrowplace a button

arrrowcode in GD

Class A

{

Public int x = 10;

}

Class B :A

{

Private int x = 100;

Public void print (int x)

{

Message Box. Show (This. x + “”) ; //100

Message Box. Show (base. x + “”) ; //10

Message Box. Show (This. x + “”) ; //1000

}

}

Code for Button 1_click

{

B obj = new B ();

Obj. print (1000);

Message Box .Show (obj . x + “”); //10

}

Working with Sealed classes

Sealed classes are not inheritable

Sealed is a keyword

When a classes is providing full functionality as per the requirements , then recommended to declare that classes as “sealed class”

Example on sealed class 

Open windows forms application project

Place a button

Code in GD

 

Sealed class test

{

Public void divide (Sting s)

{

String [] x = s. split (“”);

For (I =0; I < x. length; I ++)

Message Box. Show (x [i]);

}// divide

Class abc : test

{

// gives an error

}

 

Code for Button1 _ click

Private void Button1_click (—-)

{

Test t = new test ();

Divide (“this is for test”);

}

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Inheritance in C# .Net"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.