Click here to Skip to main content
15,888,109 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
we can use normal class as base class , instead abstract?

if we want to override then we can for the virtual methods ,

then why we go for the abstact classes.
Posted

Sorry your question isn't very clear. Are you asking what the point of abstract classes is? If so, just search Google, I found this as #1 link for the search "why use abstract class":
http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface

Hope this answer your question,
Ed
 
Share this answer
 
Comments
jameschowdaree 30-May-12 14:57pm    
im just asking why dont we use normal class as base class,insted using abstract class.
Ed Nutting 30-May-12 15:08pm    
See the answer belowabove & my comment on it - hopefully it will fully explain to you why you would use an abstract class not a normal class.
Ed
Maciej Los 30-May-12 17:36pm    
Good suggestion, my 5!
Because you can't create an instance of an abstract class, but it forces you to provide implementations of various methods.

It's sort of a cross between and class and an interface - it defines a contract that the derived class must adhere to, but can also provide a default implementation of basic functions (that an interface can't).

For example, in my I have a FlexPanel, with an abstract FlexControl. The FlexControl derived objects are autosized to fit within the FlexPanel as the user shrinks and enlarges them. The FlexPanel holds a number of controls that are based on FlexControl which provides a common look and feel by providing the title, toolbar, and main display element, allowing the derived class to fill the display with it's information. There is no point in allowing a basic FlexControl because it does not know what kind of information to display. If I was writing a video management app then one FlexControl might hold Videos, another Actors and the third Genres for example.

Because an abstract class allows me to provide the basic implementation of the tool bar and display I can handle (for example) drag and drop in the abstract class, calling the derived class overrided method to actually decide what to do with the dropped files - without re-inventing the wheel and implementing the same basic drag and drop code in each.
 
Share this answer
 
Comments
Ed Nutting 30-May-12 15:07pm    
Yes that's a reason to use an abstract class and well explained but I can see why the OP is having difficulty. The OP wants to know why you would use an abstract class as the base class rather then just a normal class as the base class. The answer is something along the lines of "I have generic code for lots of classes but need specific implementation of certain bits for it to all work so I don't want to be able to create an instance of the partial code but also don't want to have to rewrite it so therefore use an abstract class to stop instantiation but still get inheritance of the code throughout the classes" - but I'm not sure if that's the best way to put it... I see you have touched upon it in your example but perhaps you should focus in on it more, since it is the real problem here?

Anyway my 4+ for now, if this solves the OP's problem (or you update) I will boost to a 5 ;)
Ed
Sergey Alexandrovich Kryukov 30-May-12 15:47pm    
Good point.
And I think I can explain it. The keyword here is "fool-proof". Please see my answer.
--SA
Maciej Los 30-May-12 17:35pm    
Good answer, my 5!
Espen Harlinn 31-May-12 15:19pm    
Well answered :-D
Please see the answer by OriginalGriff and an interesting comment by Ed Nutting, who posed a valid interpretation of the problem you have: why making a class abstract when functionally it can be non-abstract?

I can illuminate this matter a bit.

First of all, let's note that you cannot create a non-abstract class if it has abstract methods. This is clear, because it guards a developer from calling a method which does not have implementations. It was possible in early object-oriented systems; typically, an exception was thrown.

You can work-around this by actually implementing all such methods with the keyword virtual instead of abstract. If the body of such method is doing something useful, so there are cases where it should not be overridden, this is quite acceptable and often useful practice. Otherwise, such method is called pseudo-abstract and still can be used, but not so often.

So the question, is, why not making all abstract methods virtual, giving them some implementations, sometimes empty (that is, using pseudo-abstract methods), making all such classes non-abstract and using them where an abstract class could be useful? Here is the answer: functionally, this is quite possible, and, provided you make no mistakes in you code, is fully equivalent to the approach with using abstract classes whenever possible. Why not doing so? Because the approach with using abstract classes whenever possible is always better. Why? "No mistakes" I mentioned above is the key — you cannot guarantee you do not make mistakes like using some base class where the derived class is needed or calling a pseudo-abstract method explicitly where the overridden method is needed — the compiler would not help you to detect your mistake. In other words, the approach based on using those abstract classes is fool-proof. This is the main reason why the abstract classes were introduced.

—SA
 
Share this answer
 
v2
Comments
Maciej Los 30-May-12 17:35pm    
Good answer, my 5!
Sergey Alexandrovich Kryukov 30-May-12 18:30pm    
Thank you, Maciej.
--SA
Espen Harlinn 31-May-12 15:19pm    
Well answered :-D
Sergey Alexandrovich Kryukov 31-May-12 17:18pm    
Thank you, Espen.
--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