Why method hiding and method overriding were developed in C#? I am not asking about their implementation. I am specifically asking about the scenarios which inventors looked to create these two things in language. Why method hiding is useful in different scenarios(code example) and why it is not. Why method overriding works well in some situations and why it is not in others. Practical Examples will be appreciated. Kindly note that I am not asking for implementation.
Need for method hiding and method overriding
462 Views Asked by Abner At
1
There are 1 best solutions below
Related Questions in C#
- How to call a C language function from x86 assembly code?
- What does: "char *argv[]" mean?
- User input sanitization program, which takes a specific amount of arguments and passes the execution to a bash script
- How to crop a BMP image in half using C
- How can I get the difference in minutes between two dates and hours?
- Why will this code compile although it defines two variables with the same name?
- Compiling eBPF program in Docker fails due to missing '__u64' type
- Why can't I use the file pointer after the first read attempt fails?
- #include Header files in C with definition too
- OpenCV2 on CLion
- What is causing the store latency in this program?
- How to refer to the filepath of test data in test sourcecode?
- 9 Digit Addresses in Hexadecimal System in MacOS
- My server TCP doesn't receive messages from the client in C
- Printing the characters obtained from the array s using printf?
Related Questions in OVERRIDING
- can I override c++ operator[] with only one function?
- Hide Product Manufacturer name on Volusion product pages
- Visual Studio 2019 C# "Find All References" hides "Symbols without References" under "Not Applicable"
- Generic "Create" method in .NET Core that creates two entities
- Java reflection returning base type for Scala classes
- What methods inherited from the Object class should usually be overridden?
- How to add anchor to WTForms SubmitField?
- why can't we implement two interfaces that have methods with identical signature which one of them has a default implementation in java?
- Allowing PDF upload in product customization Prestashop 1.6: how to delete the uploaded file
- Override virtual generic method
- CMake: Selectively relinking function calls
- How to make method of subclass inherited from pathlib.Path return Path instead of the subclass
- How to change current date underline color react-dateRangePicker
- Redeclare "==" operator inside an extension type - Dart
- Javascript converting a set into an array returns an empty array caused by a website
Related Questions in METHOD-HIDING
- How method hiding works in Java?
- Why Method hiding is also an example of compile time polymorphism?
- Overriding, redeclaring, hiding and redefining of methods in classes, interfaces and classes implementing interfaces
- What is difference between method hiding and without "new" keyword?
- How compiler deals with method hiding in c#?
- How to hide a method from Stacktrace but not its exception?
- Why does the instance method `hashCode` on `java.lang.Integer` make an extra jump to the static class method to simply return its own integer value?
- Why is only derived class member function called?
- Instance and Static control-flow with inheritance and overriding/method-hiding
- Java non-generic method hiding generic method with intersection types
- Base-class pointer pointing to a derived class cannot access derived-class method
- is the text regarding overriding and hiding methods on docs.oracle.com ambiguous?
- Hiding Implementation Details in C++
- How to hide the private method of base class in C#?
- C# new not working as expected on derived class-inheritence
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
You ask why? The "why" is simple: to allow polymorphism of classes/objects.
Let's say you want to create a class tha will represent a car and another for a truck. You may notice that both of then have a lot of properties that will be common (like they have weels, engine, weight, length and so on) and methods too (let's say "turnON" and "turnOFF" the engine, increase/decrase acceleration etc).
What do you do? Would you write 2 classes with the exact same code, and just add the weagon for the truck?
No, you write a "Car" class, with all the methods and properties of a car, and some of thoses properties you make then "overridable" (depends on the language on how to do it - i'm a C# programer so would be "abstract" ou "virtual", depending on the situation).
Then the "Truck" would override the maximum speed, or the site, or say how many weels it has on the front part and the weagon, if it has a axis between or not etc.
The hiding methods on the other hand, is when the base method canno't be overriten (since, depending on the languaga, if not "made" overridable, it can't be overriten) and so, on declaring a method on the child class, it "hides" the original one.
How it happen? Let's say the Car class is this:
Now, on C#, the "virtual" keyword indicates that the method (or property in this case) CAN be overriten, so the lack of it means it can't. That class allows to overrite the "Length" property, but not the "MaxSpeed".
So, when i write the Truck class, would be like this:
By doing so, the Truck class overrides the "Length", but hides the "MaxSpeed". Since Truck inherits (polymorphism) Car, it can be passed as a value to any variable of Car, so:
This works, because Truck (in our definition) is a Car. So, what's the consequence? If i create a method, that has an argument of the Car type, and i pass a Truck instance, the methods that are overriten will call (always) it's highest implementation, but the hidden methods will call the ones that are for the specific type.
So this:
Would be a problem for a hidden method (MaxSpeed), but not for the overriten method (Length). Since Length will be avaliated for it's highest implementation (it's a virtual method overriten on truck) it will always work as intented to the actual class, but MaxSpeed will be avaliated using the method write in the Car class, even if it's a Truck. It will only be avaliated as intended on the Truck class if the function/method uses Truck as an argument.