Question Method Overriding Error

Hashim

New member
Joined
Nov 21, 2018
Messages
2
Programming Experience
Beginner
Hi There.

i am learning C# programing. when i was practicing method overriding concept i got below error.

please any body can help me to address this issue i would be grateful.

My Code:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace PolyMorphism
{
    public class PolyMorphism1
    {
        public int getPolyTotalCost(int intQty)
        {
            return intQty;
        }
        public virtual int getPolyTotalCost(int intQty , int intProduct)
        {
            return intQty * intProduct;
        }
        


    }
    public class PolyMorphism2 : PolyMorphism1
    {
        public override int [COLOR=#ff0000]getPolyTotalCost[/COLOR](int intQty, int intProduct, int intPerProDiscount)  // I am getting error this line...." No suitable method found to override "
        {
            return (getPolyTotalCost(intQty, intProduct) - intPerProDiscount);
        }
    }


}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PolyMorphism;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            PolyMorphism1 Poly1 = new PolyMorphism1();  
            PolyMorphism2 Poly2 = new PolyMorphism2();
            Console.WriteLine("Enter Product Price:"); 
            int a = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine("Enter Product Quantity:");
            int b = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine("Total is:" + Poly1.getPolyTotalCost(a, b));
            Console.WriteLine("Enter Product Discount:");
            int c = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine("The total after discount is");
            Console.WriteLine(Poly2.getPolyTotalCost(a, b, c)); 
            Console.ReadLine();
        }
    }
}
 
Overriding means providing a new implementation for the same method. If you change the signature then it's not the same method. If you change the number or type of the parameters then you're overloading, not overriding.

The point of overriding is that you can call a method without knowing what specific type it is and the correct implementation of the method will be called. In your case, that "overridden" method is specific to the derived class so you couldn't call it on a reference of the base type. The idea is that you should be able to do this:
BaseType myVar = new DerivedType();

myVar.SomeVirtualMethod();

SomeVirtualMethod would be a member of the base class that was overridden in the derived class. In your case though, you couldn't call 'getPolyTotalCost' on a variable of the base type and pass three arguments because the base type has no such method.

By the way, the C# convention is to begin method names with an upper-case letter. Lower-case letters are usually used to start method names in Java and JavaScript. You don't have to follow those same conventions if you don't want to but your code then becomes inconsistent because you have system methods beginning with upper-case letters and your own methods beginning with lower-case letters. Consistency is always a good thing.
 
hi jmcilhinney.

thank you very much for explaining me the concept. im just started learning programming so little bit misunderstood.

also thanks for your advise regarding the conventions, i will follow it from now on..!!
 
I am giving example how to override methods in C#. You may try this code:
// C# program to demonstrate the method overriding 
// without using 'virtual' and 'override' modifiers 

using System; 


// base class name 'baseClass' 
class baseClass 


{ 
	public void show() 
	{ 
		Console.WriteLine("Base class"); 
	} 
} 


// derived class name 'derived' 
// 'baseClass' inherit here 


class derived : baseClass 
{ 
	
	// overriding 
	new public void show() 
	{ 
		Console.WriteLine("Derived class"); 
	} 
} 


class GFG { 
	
	// Main Method 
	public static void Main() 
	{ 
		
		// 'obj' is the object of 
		// class 'baseClass' 
		baseClass obj = new baseClass(); 
		
		
		// invokes the method 'show()' 
		// of class 'baseClass' 
		obj.show(); 
		
		obj = new derived(); 
		
		// it also invokes the method 
		// 'show()' of class 'baseClass' 
		obj.show(); 
		
	} 
}
 
I am giving example how to override methods in C#. You may try this code:

Except what you provided is not overriding. If you had overridden the method then it would have invoked the derived implementation and not the base implementation. Not surprisingly, you need to use the 'override' keyword in order to override, which you can only do if the base method is declared 'virtual'. The 'new' keyword is used to shadow an existing member.
 

Latest posts

Back
Top Bottom