Hello everyone! I have some problems with using “abstract/ overrride/ virtual/ protected”.
Honestly, I only used to write C# in unity by using “public class/public void/private void”, and I have never used such as “public abstract class/public virtual void/public override void/protected override void/…”.
I used to find some relative teaching on the Internet but I have not found any really useful for me to clearly understand these things.
Would anyone here like to explain clearly about these things? When and why I need to use them? Any help would be extremely appreciated!!!
You want to separate out the different modifiers. You can use almost any combination of each individual element.
There are access level modifiers. These are public/internal/protected/private. These control how other scripts interact with the member. As a general rule use the most restrictive access level possible.
Then there is the abstract/virtual/override set. This is used for inheritance. abstract indicates that a method must be implemented by a base class. virtual indicates a method can be implemented by a base class. override is used on a derived class to indicate it is replacing a virtual method on the base class.
Then there is the return type, which can be void or any other defined type.
The key to remember is these are all totally independent of each other. You can use any access modifier with any of the inheritance modifiers with any return type.
I’d break the keywords your question down into two answers, one about scope, and another about inheritance.
Scope
The table at the top of this MSDN page does a good job of detailing how scope works in C#. That should help explain the public / protected part of the question. I would just ignore the rows for “friend” , I find it really hard to justify using that scope.
Inheritance
Virtual: Basically, if you mark a method as virtual, any class that extends this class can override it later.
Override: Marks that this method is an override of some definition in a base class.
Thank you. Would you mind to give me some examples to show me the differences between using these and not using these? Sorry about that because I have no idea under what situations do I need to use them, which means some situations I have to use them.
Thank you!
In reality you could just make everything public all the time. If you go that rout, all of your objects can freely access all of the information about all of your other objects all the time. If you leave the keyword off of the type entirely, I believe C# makes it internal scope, which is also very broad. For members / methods, leaving off the scope keyword marks them as private which is very narrow.
In practice, using the broadest scope possible ends up causing a lot of headaches down the line. There are a lot of really good arguments for scoping you’r types and properties down as narrowly as possible.
Seriously, I don’t think this is a good advice. I would suggest picking up Object-oriented principles to understand what the modifiers are for and when you use them.
I think the advice is more along the lines of ‘until you understand the underlying concepts, this is the safest path to follow’. In other words, when TC says ‘I am not sure when to use these abstract/virtual/override’, he unfamiliar with inheritance and thus, shouldn’t be using them at all. There are no places in his code that he really SHOULD have the keywords and doesn’t, which is what he was concerned about.
Though I will agree that you definitely should read up on OOP and learn the techniques - they are all very useful.
Since what ive seen so far from people is not a way to go about things, I will explain. In Unity, the fields that are made private can be accessed inside of the editor. Private may not be, and is used for when your getting objects like the animator of an object or just finding an object, private objects also cannot be access by other scripts. Internal is a combination of both, you can’t access it from inside the editor but any other scripts can access it. and protected cannot be access from in the editor, and can only be accessed by that script and scripts deriving from that base class.
Some smart person at UT thought it would be a good idea to show similar threads when you create a new one. Of course we al know how effective the forums internal search function is.
It’s probably the same person that introduced post tags, despite them being a dismal failure on Unitys other community platform.
I noticed the necro date and didn’t bother to read it… then your comment made me curious and now I’m filled with anger, confusion and regret after reading it.