Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1 Can we add destructor in abstract class?
2 Where to use abstract class and interface ?
Please provide example.

Thanks in Advance.
Posted
Updated 27-Jun-11 1:04am
v2

 
Share this answer
 
Comments
ambarishtv 25-Jun-11 10:30am    
:thumbsup: +5
#1:
Of course you can write a destructor in abstract class. It makes perfect sense, because the destructor is inherited be a derived class and will be actually called during its destruction. You cannot literally use it without creating and instantiating non-abstract class somewhere down the hierarchy, because you cannot instantiate an abstract class. Now, writing destructors in .NET makes little sense because of its managed memory and Garbage Collector (GC). This is somewhat non-trivial aspect. GC marks all objects which cannot be accessed from currently running code and eventually destructs them and reclaim their memory, but it does not guarantee anything about the moment of time when it happens. With .NET, there is no much work for destructors. I would recommend not using unless you know what are your doing really well.

#2:
The question is asked many times. I want to show old Answers, because they are interesting, as well as discussion. I already gave my Answer, but see for others as well.

Please see the discussion on how to decide on use of interfaces vs. abstract classes: How to decide to choose Abstract class or an Interface[^].

Also, one fundamental difference is that interfaces allows for multiple inheritance, in contrast to classes (abstract or not). This is called weak form of multiple inheritance and cause serious design implications.

—SA
 
Share this answer
 
v3
Comments
Parwej Ahamad 25-Jun-11 3:50am    
Very good Explanation, my vote is 5
Sergey Alexandrovich Kryukov 25-Jun-11 16:58pm    
Thank you, Parwej.
--SA
Sergey Alexandrovich Kryukov 25-Jun-11 17:17pm    
Are you kidding? You did not ask to override them :-)
Please, why are you not reading standard MSDN help pages?
C# destructors cannot be virtual.
However, as destructors implicitly call virtual method object.Finalize and all types are descendants of System.Object, you can override Finalize. This is shown here:
http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx.
You usually don't need to do that.
--SA
CPallini 25-Jun-11 6:33am    
Very Good, 5+.
--'week form of multiple inheritance'
I suppose it is called 'the week of the inheritance', the famous OOP conference... :-D
Sergey Alexandrovich Kryukov 25-Jun-11 17:02pm    
Thank you very much for pointing out the typo. Why not a week form? Like it runs fine for whole week; when this time is expired it crashes... :-)
Fixed.
--SA
"Plz can u give me the code to override the destructors"

You don't override in the same way as another method - you just add your own finalizer to the derived class
C#
public class DerivedClass : BaseClass
{
    ~DerivedClass()
    {
        // destructor code here
    }
}

Unless there is some unmanaged resource clean up required, you rarely need finalizers. If you are using them I would recommend using the Dispose Pattern.
C#
public abstract class BaseClass : IDisposable
{
    // if not reusable once disposed then need to track the state
    private bool disposed;

    ~BaseClass()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this); 
    }
    protected virtual void Dispose(bool disposing)
    {
        if(!disposed)
        {
            disposed = true;
            if(disposing)
            {
                // managed cleanup
            }
            // unmanaged cleanup
        }
    }
}
 
Share this answer
 
v2
Comments
ambarishtv 25-Jun-11 10:31am    
:thumbsup: +5
In addition to the excellent answers provided here, you can browse through the following links -
Abstract Class versus Interface[^]
http://geekswithblogs.net/mahesh/archive/2006/07/05/84120.aspx[^]
 
Share this answer
 
Comments
ambarishtv 25-Jun-11 10:32am    
:thumbsup: +5

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