Using override and virtual in C # or Java override method

Why do we need to explicitly define a method as virtual, and then also specify an override in C # to do the method override, while the same thing is achieved without using both of them in Java keywords. What purpose does he fulfill?

+4
source share
3 answers

in java, there is no need to add any keyword to override the method. But some rules apply:

  • Method overriding cannot be declared more private than a superclass method.
  • Any exceptions declared in the override method must be of the same type as those that were selected by the superclass or subclass of this type.
  • Methods declared as final cannot be overridden.
  • The override method can be declared final, since the final keyword only assumes that this method cannot be overridden.
  • Methods declared as private cannot be overridden because they are not visible outside the class.

font

+5
source

That way, you have tighter control over what is redefined or not. This is the same as in access permissions - you grant the user all rights by default and remove the permissions, or you do not give any, and then add what is required.

+3
source

The override function means "Different methods with the same name with the same arguments." Here I add a little code to override.

Here I use the "Virtual" keyword for the base class. If we want to call a derived class, then we must use the keyword "Override".

To call the base class:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Function_Overriding { public class Program { public virtual void display() { Console.WriteLine("This is Function Overriding"); } public virtual void rnreddy() { Console.WriteLine("This is Possible because of RN Reddy"); } static void Main(string[] args) { Program dc = new Program(); dc.display(); dc.rnreddy(); Console.ReadLine(); } } } 

Derived class call

 class TestOverride { public class Employee { public string name; // Basepay is defined as protected, so that it may be accessed only by this class and derrived classes. protected decimal basepay; // Constructor to set the name and basepay values. public Employee(string name, decimal basepay) { this.name = name; this.basepay = basepay; } // Declared virtual so it can be overridden. public virtual decimal CalculatePay() { return basepay; } } // Derive a new class from Employee. public class SalesEmployee : Employee { // New field that will affect the base pay. private decimal salesbonus; // The constructor calls the base-class version, and initializes the salesbonus field. public SalesEmployee(string name, decimal basepay, decimal salesbonus) : base(name, basepay) { this.salesbonus = salesbonus; } // Override the CalculatePay method to take bonus into account. public override decimal CalculatePay() { return basepay + salesbonus; } } static void Main() { // Create some new employees. SalesEmployee employee1 = new SalesEmployee("Alice", 1000, 500); Employee employee2 = new Employee("Bob", 1200); Console.WriteLine("Employee4 " + employee1.name + " earned: " + employee1.CalculatePay()); Console.WriteLine("Employee4 " + employee2.name + " earned: " + employee2.CalculatePay()); } } /* Output: Employee4 Alice earned: 1500 Employee4 Bob earned: 1200 */ 
0
source

Source: https://habr.com/ru/post/1344217/


All Articles