Click here to Skip to main content
15,178,008 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a code below that has an Interface, abstract class and a class. I want to disable the implementation of the abstract method Print() in the FreeCustomer Class. Is this possible? Thank you very much.

C#
public interface ICustomer
{
    string CustomerName { get; set; }
    double Amount { get; set; }
    string Print();
}

public abstract class Customer : ICustomer
{
    public string CustomerName { get; set; }
    public double Amount { get; set; }
    public abstract string Print();
}

public class GoldCustomer : Customer
{
    public override string Print() {
        return "You are a Gold Customer: " + CustomerName;
    }
}

public class FreeCustomer : Customer
{

}
Posted

1 solution

Yes it is possible. Just use the new keyword. It will hide the implementation of the base class method.

C#
public class GoldCustomer : Customer
    {
        public new string Print() {
            return "You are a Gold Customer: " + CustomerName;
        }
    }



<edit> I think I understood your question wrong. You must use one of new/override keywords if you have described a method to be abstract. It must have an implementation in the derived class.
   
v2
Comments
akosidab 16-Jul-12 2:03am
   
Yes exactly.

But I'm thinking if the FreeCustomer Class can disable the Implementation of the abstract method Print()?

<pre code="C#">
public class FreeCustomer : Customer
{

}
<pre>
Abhinav S 16-Jul-12 2:09am
   
Well...in a way yes. You have already created an override method for Print in the Customer implementation, so there will be no need for you to forcibly implement the Print method.

Why don't you compile the code you have written above and see what happens?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900