Inheritance

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

An inheritance leads to code reusability.

Inheritance also saves memory.

 

Observation

Screenshot_47

These core tutorials will help you to learn the fundamentals of .NET. For an in-depth understanding and practical experience, explore Online" .NET Training"

  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 inheritances is 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 SUBCLASSES (derived class )also.

Class B : Private A -à C ++ Concept

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

Ex 1:-

A company gives Bonus, when they are reaching the target, then calculate the 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 the same name and the 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,

the “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 class is providing full functionality as per the requirements, then recommended to declare that classes as “sealed class”

Example of a 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”);

}