4

Why do we need to explicitly define a method as virtual and then also specify override in C# to accomplish method overriding whereas the same thing is achieved without using both of these in keywords in Java. What purpose does it serve?

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
user544079
  • 16,109
  • 42
  • 115
  • 171
  • possible duplicate of [Why C# implements methods as non-virtual by default?](http://stackoverflow.com/questions/814934/why-c-implements-methods-as-non-virtual-by-default) – Adam Robinson Mar 18 '11 at 03:09

3 Answers3

6

in java, there's no need to add any keyword to override a method. But some rules apply:

  • Methods overriding cannot be declared more private than the super class method.
  • Any exceptions declared in overriding method must be of the same type as those thrown by the super class, or a subclass of that type.
  • Methods declared as final cannot be overridden.
  • An overriding method can be declared as final as the keyword final only suggests that this method cannot be further overridden.
  • Methods declared as private cannot be overridden as they are not visible outside the class.

font

bluefoot
  • 10,220
  • 11
  • 43
  • 56
3

This way you have tighter control over what's overrideable or not. It's the same as in access permissions - do you give a user all rights by default and remove permissions, or do you give none and then add what's required.

Andrey
  • 20,487
  • 26
  • 108
  • 176
0

Function Overriding Means "Different Methods with the Same Name with the same arguments". Here i attach a small code about overriding.

Here i am using "Virtual" Keyword for Base class. If we want to invoke derived class then we have to use "Override" Keyword.

To Invoke 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();
        }
    }
}

To Invoke Derived Class

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
*/
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
Arjun
  • 1