Abstract class and Interface

C# Design Patterns and SOLID Principles Understanding of Design Patterns
6 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.99
List Price:  €92.85
You save:  €27.85
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$105.96
List Price:  A$151.38
You save:  A$45.42
S$94.48
List Price:  S$134.98
You save:  S$40.50
HK$546.78
List Price:  HK$781.15
You save:  HK$234.36
CHF 63.33
List Price:  CHF 90.48
You save:  CHF 27.14
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.44
List Price:  NZ$166.35
You save:  NZ$49.91
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$355.02
List Price:  B$507.19
You save:  B$152.17
R1,295.44
List Price:  R1,850.71
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪260.15
List Price:  ₪371.66
You save:  ₪111.51
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,713.01
List Price:  ¥15,304.96
You save:  ¥4,591.95
MX$1,187.73
List Price:  MX$1,696.83
You save:  MX$509.10
QR254.83
List Price:  QR364.06
You save:  QR109.23
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.65
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,773.40
List Price:  ISK kr13,962.60
You save:  ISK kr4,189.20
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.26
List Price:  ден5,712.05
You save:  ден1,713.78
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,334.28
List Price:  Ft36,193.38
You save:  Ft10,859.10
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
Already have an account? Log In

Transcript

Now before we start any design pattern, there are two important things which are going to be useful for you. If you want to learn any design pattern out of this list. These two important things are abstract classes, and interfaces. I know that most of you guys are going to be familiar with these two things. Because you guys are already familiar with C sharp programming language. There are chances that most of you are not having a very good hands on experience on C sharp, but still, I'm sure that most of you have learned asset classes and interfaces, maybe in your college days, or maybe in some particular C sharp basic screenings.

So in this particular course, because I'm going to use this abstract classes and interfaces in depth, I want to show you some basic features of abstract classes. Especially for those who are not familiar with this. Now, if I want to explain abstract classes, I'm going to say that actually, a word abstract means an idea. And that's what an abstract class is doing. In most of the projects, when we have lots and lots of code to write, we always have some functionality, which is a common. There are certain facilities, which we always use couple of times in our project for this kind of base functionality if we use asset classes.

Now this abstract class is going to be a special kind of class that has no implementation at all. It cannot be instantiated, so you cannot create an object of that. Its implementation logic is provided by the classes that are derived from it. It can have both abstract as well as non abstract methods. It means once you create an abstract class, maybe that is going to have an abstract methods, which you are going to override in the concrete implementation of that. Or maybe it's going to have a non additive methods which are going to be directly called with a concrete implementation object.

With an abstract class we can provide some kind of default for sanity or I can say a base identity for all the derived classes to extend from this one is going to be that functionality, which we are not going to change in future and we are going to just use that thing as it is. Now, this is going to be useful to avoid duplication in many cases in our project. Now, if I compare this abstract class things with interface, then first in real life when we are talking about interface is going to be the something like a medium which is going to be useful to interact between two different things. Now, similarly, in our programming language also, the interface is going to be a contract and the use of this contract is to interact between multiple modules of our project. If a class wants to communicate with an interface, then first a class has Implement the interface.

And whatever members which are defined inside the interface are going to be used inside the class. The key factor is this, that interface is not going to have any implementation of its own. Like abstract class, we're having some basic implementation inside this, this is not going to have an implementation of its own. It can only be implemented by a class or a struct. Any of the two that implement the interface must provide the definitions to the members specified in the interface. And this members are mostly going to be a method signatures.

So that's why we call this thing a contract for all the derived classes to follow. And once they implement that thing, they're going to follow this thing. There are 13 rules which you need to follow when you're dealing with interfaces, that an interface cannot contain fields, constant members, constructors destructors, and study members inside that and that's why we call that interfaces. On a class, it contains only method signatures, and it has no implementation of its own and cannot be instantiated. So, now, if you compare abstract classes with an interface, the one common thing is this that both cannot be instantiated, we cannot create an object of both. But there are very differences also, its implementation logic is provided by the classes that are derived from it.

While abstract class can have some base implementation inside the methods, which we need to override. So, the one key point is this, that interface cannot have any logic inside that it's just having signatures while abstract classes can have an interface is mostly considered as a pure abstract class, because the pure pure abstract class means is not having any metal logic inside that however, there are the advantages of using interfaces also, or an abstract class and that in advantage is multiple units. And suppose if I have a class, which is going to be implement, now suppose assume that we have a class which wants to implement two or three interfaces in that that kind of multiple inheritance is possible in interfaces, run in C sharp, if I have a two classes, either it's an asset class or a concrete class, if I want to have multiple inheritance in classes, is going to create an empty QC.

And that's why it's not going to be possible. So this is one big advantage on interfaces. That interfaces are going to support multiple inheritance. And that's why we can implement multiple interfaces into one particular class. Now, I hope that if you are a C sharp developer, then some where you have heard of word WC F, which is a modern service, the architecture of Microsoft. Now, if you have ever implemented service oriented architecture with WCS Everywhere in WC if people use interfaces for contract, and that contract is giving that loosely coupled thing in all the service instances, so that every time when we use that thing from the client side is going to instantiate the class, not the actual contract.

That's the actual more realistic use of interface. And that's shows the power of interface in our programming language. In this particular course, I'm going to use interfaces and abstract classes in different different design patterns. There are some design patterns in which we can use both. But there are a few in which we have to use one of them. During this course, I'm going to explain that why I'm using interfaces or why I'm using extra classes in such scenarios.

Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.