Inheritance

535 views
468 views

Published on

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

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
535
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
50
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Inheritance

  1. 1. object Account Saving Account Current Account Bhushan Mulmule bhushan.mulmule@gmail.com bhushan.mulmule@dotnetvideotutorial.com www.dotnetvideotutorial.com
  2. 2.  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 www.dotnetvideotutorial.com
  3. 3. www.dotnetvideotutorial.com
  4. 4. Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. Base class class MyBase { private int field1; public void fun1() { } } Derived class class MyDerived : MyBase { private int field2; public void fun2() { } } Object of MyDerived obj field1 www.dotnetvideotutorial.com field2
  5. 5. www.dotnetvideotutorial.com
  6. 6. OnRollEmployee EmpNo Name Department Designation BasicSalary HRA GrossSalary PF OffRollEmployee EmpNo Name Department Designation PerHourRate WorkedHours NetSalary NetSalary www.dotnetvideotutorial.com
  7. 7. Employee Generalized Base Class EmpNo Name Department Designation NetSalary Specialized Derived Class OffRollEmployee OnRollEmployee BasicSalary PerHourRate HRA WorkedHours GrossSalary PF www.dotnetvideotutorial.com
  8. 8. object object Account StackHolder Client Customer Supplier www.dotnetvideotutorial.com Saving Account Current Account
  9. 9. www.dotnetvideotutorial.com
  10. 10. www.dotnetvideotutorial.com
  11. 11. www.dotnetvideotutorial.com
  12. 12. www.dotnetvideotutorial.com
  13. 13.  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. www.dotnetvideotutorial.com
  14. 14.  Every class directly of indirectly derives from object class  Multiple class Inheritance is not allowed.  Multilevel is obvious!  Multiple interface implementation is possible. www.dotnetvideotutorial.com
  15. 15.  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 www.dotnetvideotutorial.com
  16. 16.   Constructor never get derived Always base class constructor executes first. www.dotnetvideotutorial.com
  17. 17. class MyBase { protected int no1, no2; class MyDerived : MyBase { private int no3; public MyBase() { } public MyDerived() : base() { } public MyBase(int n1,int n2) { this.no1 = n1; this.no2 = n2; } public MyDerived(int n1,int n2,int n3) : base(n1,n2) { this.no3 = n3; } } } Client Code MyDerived obj1 = new MyDerived(); MyDerived obj2 = new MyDerived(10, 20, 30); no1 no2 no3
  18. 18. www.dotnetvideotutorial.com
  19. 19.  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 www.dotnetvideotutorial.com
  20. 20. Access modifiers are keywords used to specify the declared accessibility of a member or a type public • Access is not restricted. private • Access is limited to the containing type. protected internal protected internal • Access is limited to the containing class or types derived from the containing class. • Access is limited to the current assembly. • Access is limited to the current assembly or types derived from the containing class. www.dotnetvideotutorial.com
  21. 21. Assembly - 1 Assembly - 2 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 { } What is accessible where? no1 •A no2 •A, B, D no3 •A, B, C no4 •A, B, C, D no5 •A, B, C, D, E www.dotnetvideotutorial.com
  22. 22. Bhushan Mulmule bhushan.mulmule@dotnetvideotutorial.com www.dotnetvideotutorial.com

×