Finished Chaining Constructors

Jason Zhu 2021-02-22 22:58:49 +11:00
parent 8fc9379a84
commit 1137d1b922

@ -0,0 +1,57 @@
# Module 8. Object-oriented Programming with C#
## The Pillars of OOP
3 pillars
* **Encapsulation**: hide details.
* Methods; Properties; Access modifiers;
* **Inheritence**: Reuse code
* **Polymorphism**: Have object behave differently
## Deriving from a Base Class
Reference:
* [Inheritance in C#](https://dotnettutorials.net/lesson/inheritance-c-sharp/)
* [Inheritance in C# and .NET (C# guide)](https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/inheritance)
Syntax:
```csharp
public class ChildClass : ParentClass
{
//...
}
```
## Chaining Constructions
Reference:
* [Call Chain of Constructors in C#](https://app.pluralsight.com/guides/call-chain-constructors-csharp)
When constructor of parent class has parameter, we can chain the constructor of child class using `base(param1, ...)`
Syntax:
```csharp
public class ParentClass
{
public ParentClass(string name)
{
Name = name;
}
public string Name
{
get;
set;
}
}
public class ChildClass : ParentClass
{
public ChildClass(string name) : base(name)
{
Name = name;
}
}
```