I found C# very interesting...but unfortunately (or fortunately ! ) it has many features to implement OOP rules....they have different machanisms and make me sometimes confused....
virtual, new,... modifiers in c# have different rules....so what is the best way or best-practices for learning OOP rules and use them easily...?
so what is the best way or best-practices for learning OOP rules and use them easily...?
The best way to learn is to keep things simple and practice (program) a lot. Regarding virtual/new/override, there are three main cases:
Virtual + override - Use virtual in the base class and override in the derived class, as in:
Abstract + override - This is a variant of the previous case where the base member does not define a body:
No modifier - This is useful when you don't plan on overriding a method:
In the this case, there would be a warning if
OtherMethod
was namedTest
. Indeed, it would clash with the base method. You can get rid of the warning by adding anew
modifier as inHowever, I would recommend avoiding the
new
modifier if possible since it is somewhat confusing.Your best bet is to learn about OOP principles (encapsulation, inheritance and polymorphism) from a fundamental source. And then worry about particular language implementations later. Once you really understand the fundamental concepts, the language specifics become easy to learn, apply and master.