Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know what is Abstract class but this time i want to know how i can use abstract class. please give me some examples or articles . i want to work on this class ...please guide me.....
Posted

Why not you try Google Search.


Hope this help!
 
Share this answer
 
C#
public abstract class Duck//Abstract class
{
    public Duck()//Constructor
    {
    }
    
     public abstract void display();//abstract method
}



An abstract class will have a method that will be an abstract method. This means a child class that will inherit from "Duck" will have the same abstract method called "Display"

C#
public class Mallack:Duck
{
    public Mallack()//Constructor
    {
    }
     
    public override void display()
    {
        //Place the code that changes every time HERE
    }
}



The Mallack is a child class that inherits from Duck. Remember all clases that will inherit from Duck will have the method "display"




Hope it helps...
 
Share this answer
 
v2
Comments
[no name] 23-Mar-12 3:52am    
Code format improved.
Sergey Alexandrovich Kryukov 23-Mar-12 4:06am    
How can you answer explain this: why not using just the class Mallard without Duck and "override"?

Or, why not making the class Duck non-abstract and Duck.display just virtual, but no abstract? -- it would work just the same way.

I basically answered, please see...
--SA
Anele Ngqandu 23-Mar-12 4:15am    
True but The question says "how i can use abstract class"
Sergey Alexandrovich Kryukov 23-Mar-12 5:29am    
I see. And what is you answer? "Use it for duck hunting"? :-)
--SA
Sergey Alexandrovich Kryukov 23-Mar-12 5:43am    
I commented your example below. You could only illustrate the rationale behind it if you added more derived classes and some user class. With just two classes, the use of abstract would not have practical meaning.
--SA
Apparently, if you know what is that, you know that you cannot instantiate such class. You also should know, that if you have a single abstract method or property (which is always virtual and without implementation), the syntax will force you to declare the whole class as abstract.

Now, let's speculate logically. If you cannot create the class instance, what you can do? You can create some derived classes of this abstract class. Some derived classes can be abstract, but ultimately you will need to create some terminal classes which are not abstract and can be instantiated, otherwise the whole activity could be useless.

So, you abstract class would be the abstract base for all the hierarchy. And the non-abstract classes from the hierarchy should override all abstract methods/properties, otherwise you would not be able to make them non abstract. The methods will be dynamically dispatched through the mechanism of virtual method table, which is the essence of late binding.

Now, you should understand the difference between run-time and compile-time types. You can declare a variable of any type, including the abstract class. When you instantiate the variable, its run-time type could not be of the abstract class (remember, it cannot be instantiated), so its run-time type should be different from compile-time type, and should be one of the derived classes, and only of a non-abstract one. When you call an abstract method (from the standpoint of the compile-time), the dynamically dispatched run-time method will always be the one implemented in the derived class, always non-abstract one. Why? Because 1) you cannot have an abstract method in a non-abstract class, 2) you cannot instantiate a non-abstract class. The call will always be properly resolved.

Same thing will happens with a polymorphous set. The compile-time type can be an abstract class (for example, the actual generic parameter of System.Collections.Generic.List), but all run-time types of the objects actually added to this container… yes, as described in the previous paragraph.

So, everything works for late binding and polymorphism. Now, the question is: could it work of we never used abstract class. Yes, it would work in the exact same way. Only we would need to implement all the methods which would otherwise be abstract using the keyword virtual instead. For example, such methods would have empty bodies (we would need some bodies anyway, if we want them non-abstract). Usually such methods are called pseudo-abstract. What would be wrong with that?

The answer is: nothing wrong but — declaring abstract class would work as a fool-proof feature. A pseudo-abstract methods are only good if they still could participate in normal functionality. For example, a pseudo-abstract method ShowMessage could show nothing "by default", but this could be customized in derived classes. But there are more cases where having no certain definitive behavior of one or more method would really screw up the functionality without some specific implementation. Hence, an accidental instantiation and use of such object could screw up the functionality of the system.

Without abstract classes, what could be done? (Yearly OOP languages really did not have them.) We could explain that this and this class (such as System.IO.Stream) should never be instantiated and is only used as a base of the following classes… and also user-defined classes, and list them — in documentation. But preventing such accidental misuse just by syntax is much better.

You can find many examples of abstract classes in the libraries which come with .NET and placed in GAC. Try to think about their use; and you will clearly understand it.

—SA
 
Share this answer
 
abstract class usually used base class.
child class inherits from base class.
 
Share this answer
 

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