I have some basic questions about abstract classes/methods.I know basic use of abstract classes is to create templates for future classes. But are there any more uses of them ? When should you prefer them over interfaces and when not ? Also when are abstract methods useful ?
相关问题
- Anonymous classes
- why abstract class instantiation isn't runtime
- Why I can not use abstract class in std::vector?
- Attribute, interface, or abstract class
- In Python, how do I determine if an iterable has s
相关文章
- Alternatives to abstract classes in Ruby?
- C++ abstract class parameter error workaround
- MVC3 View With Abstract Property
- Android - Can we extends OnClickListener another C
- instantiating an abstract class in JAVA?
- Abstract base class model vs Proxy model in Django
- Abstract Class Design: Why not define public const
- What's the advantage of this indirect function
At a very high level:
Abstraction of any kind comes down to separating concerns. "Client" code of an abstraction doesn't care how the contract exposed by the abstraction is fulfilled. You usually don't care if a string class uses a null-terminated or buffer-length-tracked internal storage implementation, for example. Encapsulation hides the details, but by making classes/methods/etc. abstract, you allow the implementation to change or for new implementations to be added without affecting the client code.
Not only can you define a template for children, but Abstract Classes offer the added benefit of letting you define functionality that your child classes can utilize later.
You can't provide an implementation for an Interface.
Abstract Classes are a good fit if you want to provide implementation details to your children but don't want to allow an instance of your class to be directly instantiated (which allows you to partially define a class).
If you want to simply define a contract for Objects to follow, then use an Interface.
Abstract methods are useful in the same way that defining methods in an Interface is useful. It's a way for the designer of the Abstract class to say "any child of mine MUST implement this method".
Abstract classes/methods are generally used when a class provides some high level functionality but leaves out certain details to be implemented by derived classes. Making the class/method abstract ensures that it cannot be used on its own, but must be specialized to define the details that have been left out of the high level implementation. This is most often used with the template method pattern:
http://en.wikipedia.org/wiki/Template_method_pattern
Typically one uses an abstract class to provide some incomplete functionality that will be fleshed out by concrete subclasses. It may provide methods that are used by its subclasses; it may also represent an intermediate node in the class hierarchy, to represent a common grouping of concrete subclasses, distinguishing them in some way from other subclasses of its superclass. Since an interface can't derive from a class, this is another situation where a class (abstract or otherwise) would be necessary, versus an interface.
A good rule of thumb is that only leaf nodes of a class hierarchy should ever be instantiated. Making non-leaf nodes abstract is an easy way of ensuring that.
If you are looking for good 'Software Engineering' and 'Design documentation' then i advice you to have a look there:
https://prof.hti.bfh.ch/index.php?id=3494&L=2
About abstract classes introduction:
https://prof.hti.bfh.ch/fileadmin/home/due1/uml_dp/script/udp-advancedclassmodeling-200910.pdf
read the following article http://mycodelines.wordpress.com/2009/09/01/in-which-scenario-we-use-abstract-classes-and-interfaces/