Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to create abstract method?
Posted
Updated 10-Nov-14 5:39am
v3
Comments
CPallini 10-Nov-14 11:40am    
Don't you trust the documentation?

Abstract methods are easy to create:
C#
public abstract class MyBase
   {
   public abstract string MyMethod(string s);
   }

Then when the derived class is created, it must create a definition before the class will compile.
 
Share this answer
 
Comments
Mehdi Gholam 10-Nov-14 11:01am    
:) 5'ed
Manas Bhardwaj 10-Nov-14 11:05am    
Yup 5!
Thomas Daniels 10-Nov-14 11:51am    
+5!
Volynsky Alex 10-Nov-14 16:15pm    
Nice!
Maciej Los 10-Nov-14 16:30pm    
+5
 
Share this answer
 
Comments
Maciej Los 10-Nov-14 16:30pm    
+5
Manas Bhardwaj 10-Nov-14 16:32pm    
thx ;)
Methods can not be abstract, classes can be abstract.
 
Share this answer
 
Comments
OriginalGriff 10-Nov-14 10:53am    
*cough*
Sure?
http://msdn.microsoft.com/en-us/library/aa664435(v=vs.71).aspx
*cough*
Mehdi Gholam 10-Nov-14 11:01am    
:)
Technically you can't have an abstract method outside of an abstract class, and even then the abstract method cannot have a body, so you are not creating an abstract method just defining a placeholder of sorts.
BillWoodruff 10-Nov-14 12:04pm    
Using the same logic, one could say that an abstract class is not a class if it provides no implementations ?
Sergey Alexandrovich Kryukov 10-Nov-14 12:16pm    
Even without spotting this flawed logic, Mehdi's statements are false. I explained it below.
—SA
Sergey Alexandrovich Kryukov 10-Nov-14 12:14pm    
This is not true too. 1) First of all, there are abstract methods and property; "abstractness" of the class is a secondary non-required thing; 2) if you don't mark class "abstract", you still can write abstract methods/properties in it adding an abstract method will require to add "abstract" to the class, too; the compiler will issue an error. But you can mark the class "abstract" without adding abstract members. In both cases, instantiation of an abstract class will be prevented by the compiler.

An abstract class is not a placeholder at all. This is an "incomplete class". Its main purpose is to serve as a base class representing some common features of some set of derived classes which are used in polymorphic sets of object. If virtual methods/property are not used or if they used but late binding is not used, abstract classes defeat the purpose. But the common sense dictate that such abstract classes without abstract members should be allowed. This is the same as allowing methods with empty bodies, and so one. The compiler should not prevent compiling incomplete but formally correct code.

Please, check up thoroughly all the statements you post as "answer", to avoid confusing readers.

—SA

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