Вы находитесь на странице: 1из 4

Features of Abstract Class

1. An abstract class cannot be instantiated.


2. An abstract class contain abstract members as well as non-abstract members.
3. An abstract class cannot be a sealed class because the sealed modifier prevents a class
from being inherited and the abstract modifier requires a class to be inherited.
4. A non-abstract class which is derived from an abstract class must include actual
implementations of all the abstract members of parent abstract class.
5. An abstract class can be inherited from a class and one or more interfaces.
6. An Abstract class can has access modifiers like private, protected, internal with class
members. But abstract members cannot have private access modifier.
7. An Abstract class can has instance variables (like constants and fields).
8. An abstract class can has constructors and destructor.
9. An abstract method is implicitly a virtual method.
10. Abstract properties behave like abstract methods.
11. An abstract class cannot be inherited by structures.
12. An abstract class cannot support multiple inheritance.
Abstract classes, marked by the keyword abstract in the class definition, are typically used to
define a base class in the hierarchy. What's special about them, is that you can't create an instance
of them - if you try, you will get a compile error. Instead, you have to subclass them, as taught in
the chapter on inheritance, and create an instance of your subclass. So when do you need an
abstract class? It really depends on what you do.

To be honest, you can go a long way without needing an abstract class, but they are great for
specific things, like frameworks, which is why you will find quite a bit of abstract classes within
the .NET framework it self. A good rule of thumb is that the name actually makes really good
sense - abstract classes are very often, if not always, used to describe something abstract,
something that is more of a concept than a real thing.

In this example, we will create a base class for four legged animals and then create a Dog class,
which inherits from it, like this:
namespace AbstractClasses
{

class Program
{

static void Main(string[] args)


{

Dog dog = new Dog();


Console.WriteLine(dog.Describe());
Console.ReadKey();

abstract class FourLeggedAnimal


{

public virtual string Describe()


{
}

return "Not much is known about this four legged animal!";


}

class Dog : FourLeggedAnimal {

}}

If you compare it with the examples in the chapter about inheritance, you won't see a big
difference. In fact, the abstract keyword in front of the FourLeggedAnimal definition is the
biggest difference. As you can see, we create a new instance of the Dog class and then call the
inherited Describe() method from the FourLeggedAnimal class. Now try creating an instance of
the FourLeggedAnimal class instead:
FourLeggedAnimal someAnimal = new FourLeggedAnimal();

You will get this fine compiler error:


Cannot

create

an

instance

of

the

abstract

class

or

interface

'AbstractClasses.FourLeggedAnimal'
Now, as you can see, we just inherited the Describe() method, but it isn't very useful in it's
current form, for our Dog class. Let's override it:
class Dog : FourLeggedAnimal
{

public override string Describe()


{

return "This four legged animal is a Dog!";

}}
In this case, we do a complete override, but in some cases, you might want to use the behavior
from the base class in addition to new functionality. This can be done by using the base keyword,
which refers to the class we inherit from:
abstract class FourLeggedAnimal
{

public virtual string Describe()


{

return "This animal has four legs.";

}}
class Dog : FourLeggedAnimal
{
public override string Describe()
{
string result = base.Describe();
result += " In fact, it's a dog!";
return result;
}}
Now obviously, you can create other subclasses of the FourLeggedAnimal class - perhaps a cat
or a lion? In the next chapter, we will do a more advanced example and introduce abstract
methods as well. Read on.
Common design guidelines for Abstract Class

1. Don't define public constructors within abstract class. Since abstract class cannot be
instantiate and constructors with public access modifiers provides visibility to the classes
which can be instantiated.
2. Define a protected or an internal constructor within an abstract class. Since a protected
constructor allows the base class to do its own initialization when sub-classes are created
and an internal constructor can be used to limit concrete implementations of the abstract
class to the assembly which contains that class.
When to use

1. Need to create multiple versions of your component since versioning is not a problem
with abstract class. You can add properties or methods to an abstract class without
breaking the code and all inheriting classes are automatically updated with the change.
2. Need to to provide default behaviors as well as common behaviors that multiple derived
classes can share and override.

Вам также может понравиться