Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Why do we use abstract and non abstract method?
Implemened and unimplemened method?
Posted
Updated 30-Mar-15 14:41pm
v2
Comments
Sergey Alexandrovich Kryukov 30-Mar-15 20:32pm    
Questions about "difference" like that are incorrect. How would you define the notion of "difference"? You just need to learn this declaration and understand its purpose.
—SA

Abstract method (as well as a property) is one special kind virtual method: there is no implementation, so it is used as a prototype for overriding methods in derived classes. The purpose of this feature is simple: to indicate that the method makes no sense unless it is overridden, and, hence, its declared class only can be used as a base class, but cannot be instantiated, therefore, it is also required to be abstract.

In contrast, you can create abstract or non-abstract class with some virtual method which perhaps does not do anything, but there is some implementation which can be called. It means that overriding of this method is intended, but not required for all derived classes. Such method usually called "pseudo-abstract". Pseudo-abstract notion has nothing to do with language or platform rules (because such rules are not needed for anything) — this is a pure code design technique.

—SA
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 30-Mar-15 21:39pm    
+5
Sergey Alexandrovich Kryukov 30-Mar-15 21:40pm    
Thank you, Afzaal.
—SA
Sascha Lefèvre 30-Mar-15 21:53pm    
My 5 as well.
Sergey Alexandrovich Kryukov 30-Mar-15 21:55pm    
Thank you, Sascha.

By the way, I would like to invite your to see my new 1 of April publication and have some fun:
Some Programming Approaches to "Neuro-Linguistic Programming".
Participation in this game in Comments and Discussions is especially encouraged.

Thank you.
—SA
An Abstract method is one which cannot be declared in the base class with any implementation: it must be implemented in the derived class.

This forces derived classes to implement the method - or it will not compile. It's used for when you know that all derived classes will need a function, but you have no idea how they will actually implement it.
For example, a Person class may add an abstract ChargeForGoods method, which the derived classes (Employee and Customer) implement differently: the Employee gets an automatic 25% discount, and it is deducted from their wages, while the Customer will be invoiced.

C#
public abstract class Person
   {
   public abstract void ChargeForGoods(Item item);

   public void BuyItem(Person purchaser, Item item)
      {
      purchacer.ChargeForGoods(item);
      }
   }
The Person class doesn't need to know how the method is implemented, just that it is!
 
Share this answer
 
v2
You usually use an abstract method in an abstract base class that implements a functionality partially and requires the abstract method for that but the implementation of that method can vary depending on the concrete class that inherits from the abstract base class.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Mar-15 20:30pm    
Sorry, one fact, which is not true, spoils the whole thing totally: abstract class absolutely does not require abstract method. Even from the practical standpoint, having an abstract method is most usually important, but not always: there are practical cases when you don't need it. It's abstract method requires abstract class.

You also failed to mention the most important feature of the abstract class: such declaration prevents instantiation of the class, even if there are not abstract members. Since you started to talk about abstract methods: not only methods can be abstract.

Sorry,
—SA
Sascha Lefèvre 30-Mar-15 21:42pm    
I meant it like this: An abstract class (could) require an abstract method for an intended partial functionality; not that an abstract class has to have an abstract method. But re-reading my answer I have to agree that I could have worded it better and explained in more detail, judging the inquirers experience from his question.
/Sascha
Afzaal Ahmad Zeeshan 30-Mar-15 21:41pm    
Please reply to their comment in order to notify them of your message.
Sascha Lefèvre 30-Mar-15 21:42pm    
oops. Thank you for notifying me :)

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