Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I was reading an article on whether there could be non abstract methods in abstract classes or not.

Then while reading a question pops up into my mind that is vice-verse can also be true i.e., If some one is willing to make a abstract method in a non abstract class then will it be helpful or it will give a compile time error.?

This question might be novice for some but i am a little low in OOPs concepts theoretically.

Thanks

Varun
Posted

1 solution

No. And you can easily check it up by writing code and trying to compile it. The indication that a class is abstract with the use of the keyword "abstract" is a sort of a fool-proof feature. It's pretty much the like the absence of non-declared variables.

You can define an abstract class with no abstract methods/properties. It is safe and may make sense, as it indicates that the class is intended to be a base class for some non-abstract classes and serve as a common interface. It does not require abstract classes and even late binding.

From the other hand, if there is at least one no abstract method/property, it requires the class to be abstract. If the class was not abstract (allowed instantiation), it would be unsafe, because then the call to the abstract method/property would be a disaster.

The keyword "abstract" applied to the class is less important than the rule explained above, as the "abstractness" could be determined only based on the members. Nevertheless, this is extra convenience and more fool-proof syntax, also more readable.

—SA
 
Share this answer
 
Comments
Varun Sareen 10-Apr-13 1:24am    
thanks dear Sergey for such a good explanation to my question.

Though i tried it myself and found the answer.

Thanks once again
Sergey Alexandrovich Kryukov 10-Apr-13 9:47am    
This is what you should have done in first place.
Anyway, are you accepting this answer formally (green button)?
—SA
nv3 10-Apr-13 2:59am    
My 5.
Sergey Alexandrovich Kryukov 10-Apr-13 9:47am    
Thank you.
—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