Ahhh.. finally i have time to write again after took long leave days. I hope next week i can pay all of my debt (tutorial every week) 😀 . This time i will share you the basic Object Oriented implementation in C#. I believe people who use .Net understand the basic of OOP, but most people don’t utilize all of the advantage of OOP. Have you tried to use aggregation? Have you tried to use abstraction? Have you tried to use interface? If you are not, it’s the good time to you to get new knowledge about that. I have written an example of C# console program that make you easier to understand, once you debug this code will give you better knowledge in OOP.
Here is the C# console program codes :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
using System; namespace OOPImplementation { public class Program { static void Main(string[] args) { Lamp lamp = new Lamp(); // instantiation for aggregation Car Avanza = new Car(lamp) // instantiation and send object parameter for aggregation { Color = "green", TopSpeed = "360km/h", Gear = 5, Seat = 6, Price = 1000 }; Avanza.StartMachine(); Avanza.Accelerate(); Avanza.Brake(); Avanza.ChangeGear(1); // Message Passing -> to send parameter Avanza.ChangeGear(4, 5); // Message Passing -> to send parameter Avanza.TotalPrice(); Console.Read(); } } public interface IVehicle // interface declaration { void Accelerate(); void TotalPrice(); // method invocation } public class GroundVehicle : IVehicle // implementation of interface { #region properties public string Color { get; set; } public string TopSpeed { get; set; } public int Gear { get; set; } private int _price; // encapsulation -> modifier private & information hiding protected int _tax = 10; // encapsulation -> modifier protected & information hiding public int Price { get { return _price; } set { _price = value; } // set value of private properties } #endregion #region method implementation public virtual void Brake() // virtual method -> can be override { Console.WriteLine("Brake"); } // Example of Overloading public void ChangeGear(int gear) // Message Passing -> to receive parameter { Console.WriteLine("Change to Gear " + gear); } public void ChangeGear(int fromGear, int toGear) // Polymorphism -> Overloading Method Change Gear { Console.WriteLine("Change From Gear " + fromGear + " to Gear " + toGear); } public void Accelerate() // implementation of interface { Console.WriteLine("Accelerate"); } public void TotalPrice() // implementation of interface { Console.WriteLine("Total Price : " + (_price + (_price / _tax))); } #endregion } public class Car : GroundVehicle // Inheritance -> extend of base class { Electricity _electricity; public Car(Electricity electricity) // Agregation -> inject the class in constructor { _electricity = electricity; } public int Seat { get; set; } // new property of this class public override void Brake() // overriding Brake() method { Console.WriteLine("(override) Brake"); } public void StartMachine() { Console.WriteLine("Start Machine"); _electricity.TurnOnLamp(); // call method in aggregation } } public abstract class Electricity // Abstract Declaration { public abstract void TurnOnLamp(); // Abstract Method -> Should be override public void TurnOffLamp() // void method in abstract automatically implemented in inherit class { Console.WriteLine("Turn Off Lamp"); } } public class Lamp : Electricity // Extend Abstract Class { public override void TurnOnLamp() // Pholymorphism -> Overriding base abstract method { Console.WriteLine("Turn On Lamp"); } } } |
You will get this result after you run this program :
I have written the comment on each important line to make you easy to understand what are codes for. Please waiting for the next OOP explanation. 🙂 cheers.
A passionate software engineer who wants to share anything that he learns and some of his life experiences. Want to know more about him? see Fatkhan Fauzi Profile
Too confusing
Why don’t you categorized your sample code topic by topic, ex :
1. Aggregation
– example code
– explanation
2. Abstraction
– example code
– explanation
and so forth
Nope, i emphasize the combination among them.
Once we debug this code we will understand the correlation of each line codes.
I have written the comment on each line, and i think it will give the better knowledge on what is code for. 😀
I’m the reader and you are not because you are the author, and i feel it is too confusing, period
This a comment from your reader
Okay, good suggestion..
I will try to be more wisely on separating the topic. 🙂
Thanks..