Inheritance

24
Inheritance Bhushan Mulmule [email protected] [email protected] www.dotnetvideotutorial.com object Account Saving Account Current Account

description

What is Inheritance, When to use it. Constructor flow in inheritance, Access modifiers.

Transcript of Inheritance

Page 1: Inheritance

InheritanceBhushan Mulmule

[email protected]

[email protected]

www.dotnetvideotutorial.com

object

Account

SavingAccount

CurrentAccount

Page 2: Inheritance

For video visit www.dotnetvideotutorial.com

Page 3: Inheritance

Part I◦ Inheritance: When, Why, How◦ Inheritance in .NET Framework◦ Inheritance Demo Example

Part II◦ Constructor Flow in Inheritance

Part III◦ Access Modifiers: private, public, protected, internal, protected internal

Agenda

www.dotnetvideotutorial.com

Page 4: Inheritance

ImplementationInheritanceInterface

ImplimentationImplementation

InheritanceInterface

Implementation

www.dotnetvideotutorial.com

Page 5: Inheritance

class MyBase{ private int field1; public void fun1() { }}

class MyDerived : MyBase{ private int field2; public void fun2() { }}

Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other

classes.

Base class

Derived class

field1 field2obj

Object of MyDerived

www.dotnetvideotutorial.com

Page 6: Inheritance

When to use inheritance?

www.dotnetvideotutorial.com

Page 7: Inheritance

OnRollEmployee

EmpNo

Name

Department

Designation

BasicSalary

HRA

GrossSalary

PF

NetSalary

OffRollEmployee

EmpNo

Name

Department

Designation

PerHourRate

WorkedHours

NetSalary

www.dotnetvideotutorial.com

Page 8: Inheritance

Employee

EmpNo

Name

Department

Designation

NetSalary

OnRollEmployee

BasicSalary

HRA

GrossSalary

PF

OffRollEmployee

PerHourRate

WorkedHours

Generalized Base Class

Specialized Derived Class

www.dotnetvideotutorial.com

Page 9: Inheritance

object

StackHolder

Client Customer

Supplier

object

Account

SavingAccount

CurrentAccount

Few more examples…

www.dotnetvideotutorial.com

Page 10: Inheritance

Inheritance in .NET Framework

www.dotnetvideotutorial.com

Page 11: Inheritance

www.dotnetvideotutorial.com

Page 12: Inheritance

www.dotnetvideotutorial.com

Page 13: Inheritance
Page 14: Inheritance

www.dotnetvideotutorial.com

Page 15: Inheritance

inheritance hierarchy represents an "is-a" relationship and not

a "has-a" relationship.

can reuse code from the base classes.

need to apply the same class and methods to different data

types.

The class hierarchy is reasonably shallow, and other

developers are not likely to add many more levels.

want to make global changes to derived classes by

changing a base class.

Inheritance is a good choice when

www.dotnetvideotutorial.com

Page 16: Inheritance

Every class directly of indirectly derives from

object class

Multiple class Inheritance is not allowed.

Multilevel is obvious!

Multiple interface implementation is possible.

Important

www.dotnetvideotutorial.com

Page 17: Inheritance

Part I◦ Inheritance: When, Why, How◦ Shadowing◦ Inheritance Demo Example

Part II◦ Constructor Flow in Inheritance

Part III◦ Access Modifiers: private, public, protected, internal, protected internal

Agenda

www.dotnetvideotutorial.com

Page 18: Inheritance

Constructor never get derived Always base class constructor executes

first.

www.dotnetvideotutorial.com

Page 19: Inheritance

class MyBase{ protected int no1, no2;

public MyBase() { }

public MyBase(int n1,int n2) { this.no1 = n1; this.no2 = n2; }}

class MyDerived : MyBase{ private int no3;

public MyDerived() : base() { }

public MyDerived(int n1,int n2,int n3)

: base(n1,n2) { this.no3 = n3; }}

MyDerived obj1 = new MyDerived();

MyDerived obj2 = new MyDerived(10, 20, 30);

no1 no2 no3

Client Code

Page 20: Inheritance

www.dotnetvideotutorial.com

Page 21: Inheritance

Part I◦ Inheritance: When, Why, How◦ Shadowing◦ Inheritance Demo Example

Part II◦ Constructor Flow in Inheritance

Part III◦ Access Modifiers: private, public, protected, internal, protected internal

Agenda

www.dotnetvideotutorial.com

Page 22: Inheritance

Access modifiers are keywords used to specify the declared accessibility of a member or a type

•Access is not restricted.public

•Access is limited to the containing type.private

•Access is limited to the containing class or types derived from the containing class.protected

•Access is limited to the current assembly.internal

•Access is limited to the current assembly or types derived from the containing class.protected internal

www.dotnetvideotutorial.com

Page 23: Inheritance

class A{ private int no1; protected int no2; internal int no3; protected internal int no4; public int no5;} class B : A{}

class C{}

class D : A{}

class E{}

Assembly - 1 Assembly - 2

•Ano1

•A, B, Dno2

•A, B, Cno3

•A, B, C, Dno4

•A, B, C, D, Eno5

What is accessible where?

www.dotnetvideotutorial.com