Okay it's time to learn the Principals of Object Oriented Programming(OOP). Basically, in here, I will concise this topic on C# programming as I'm currently learning them in my University. I'll try my best to make it clear on how it goes! So stay with me okay! :D
First thing first let's go through the C# programming language itself. Below I attached a video on How to program in C# by Brackeys. I've went through several videos from him where I found it's very helpful for beginners (like me).
First thing first let's go through the C# programming language itself. Below I attached a video on How to program in C# by Brackeys. I've went through several videos from him where I found it's very helpful for beginners (like me).
ENCAPSULATION
Figure 1. Encapsulation details
This principal called "encapsulation" covers up or hide the data in order to prevent and protect them from accidental corruption due to error that made up by programmers. It is a restricted access to member of a class where it prevents the user of the given class manipulate the objects as the programmer not intended them to do so.
Advantages of encapsulation in OOP is:
- Protect the data and prevent from accidental corruption
- Accessibility specification for member of the class that the code is outside of the class
- Flexibility and extensibility of the code
Figure 2. Count class in C# programming
ABSTRACTION
Abstraction provided a common definition of a base class that can be shared by many derived class. In other hand, an abstract class cannot be instantiated where it means by the object of the class couldn't be created. Implementation was made in derived class while the methods will be in the abstract class.
Figure 3. Abstract class where derived class do the implementation
Figure 4. Abstract methods of base classes
INHERITANCE
When we talk about "inheritance", things that came across to our mind was the parent and child. In this case, child class can override from parent class. In other word, it extends the functionality and state the "parent" in derived class or "child".
Figure 5. Tell rectangle class to inherit from Shape class
Figure 6. Override Draw method from Shape class
Figure 7. Alex and his dad
This principal combined two words, which is:
poly > many
morph > forms
Polymorphism was performed where it can performs multiple implementation with same method name. There were two types of polymorphism.
- Static or Compile time Polymorphism
- overload methods that has the same name but having different signature
public class Class1
{
public void NumbersAdd(int a, int b)
{
Console.WriteLine(a + b);
}
public void NumbersAdd(int a, int b, int c)
{
Console.WriteLine(a + b + c);
}
}
- Dynamic or Run time Polymorphism
- having same name as well as the signature but different implementation
/Base Class
public class Bclass
{
public virtual void Sample1()
{
Console.WriteLine("Base Class");
}
}
// Derived Class
public class DClass : Bclass
{
public override void Sample1()
{
Console.WriteLine("Derived Class");
}
}
// Using base and derived class
class Program
{
static void Main(string[] args)
{
// calling the overriden method
DClass objDc = new DClass();
objDc.Sample1();
// calling the base class method
Bclass objBc = new DClass();
objBc.Sample1();
}
}
Figure 8. Duck,cat and dog were animals but differently described
References
- http://csharp.net-tutorials.com/classes/inheritance/
- https://msdn.microsoft.com/en-us/library/ms173149.aspx
- http://www.spiroprojects.com/blog/cat-view-more.php?blogname=What-is-Abstract-Class?&id=351
- http://www.aspdotnet-suresh.com/2013/09/polymorphism-in-c-with-example-types-of-polymorphism.html
- http://www.codeproject.com/Articles/602141/Polymorphism-in-NET
- https://en.wikibooks.org/wiki/C_Sharp_Programming/Inheritance