Characteristics of an Abstract Class

I like to know what makes a class callable as a paragraph class. I suppose the abract keyword definitely makes the class a class, but if you retrieve the keyword, then we can instantiate the class.

In other words, what are the characteristics of an abstract class.

Thanks in advance.

-Harsha

+2
c # oop
May 17 '10 at 12:40
source share
5 answers

An abstract class does not form a specific object in the real world, unlike pure implementation classes. . Annotation, since the name suggests that they contain / define the general behavior of related objects that must be reused / defined independently in all related objects.

Take the Bird example. If you are writing a program that will have something to do with birds, then you will first get an abstract base class, like a Bird, and each bird derived from an abstract base class Bird. Please note that the abstract BIRD class is not a specific real-world object, but a type of related objects that are birds!

Let's start with the class diagram and then the code.

alt text http://ruchitsurati.net/files/birds.png

public abstract class Bird { protected string Name = string.Empty; public Bird(string name) { this.Name = name; } public virtual void Fly() { Console.WriteLine(string.Format("{0} is flying.", this.Name)); } public virtual void Run() { Console.WriteLine(string.Format("{0} cannot run.", this.Name)); } } public class Parrot : Bird { public Parrot() : base("parrot") { } } public class Sparrow : Bird { public Sparrow() : base("sparrow") { } } public class Penguin : Bird { public Penguin() : base("penguin") { } public override void Fly() { Console.WriteLine(string.Format("{0} cannot fly. Some birds do not fly.", this.Name)); } public override void Run() { Console.WriteLine(string.Format("{0} is running. Some birds do run.", this.Name)); } } class Program { static void Main(string[] args) { Parrot p = new Parrot(); Sparrow s = new Sparrow(); Penguin pe = new Penguin(); List<Bird> birds = new List<Bird>(); birds.Add(p); birds.Add(s); birds.Add(pe); foreach (Bird bird in birds) { bird.Fly(); bird.Run(); } Console.ReadLine(); } } 
+8
May 17 '10 at 12:43
source share

An abstract class has one special characteristic: no class instances can be created. Abstract classes are intended to be used as base classes for other abstract and non-abstract classes.

+2
May 17 '10 at 12:42
source share

In order for abstract in a class to have any meaning, it must have abstract elements that need to be implemented, otherwise it is extraneous (if you do not intend to prevent the creation of an instance), and you must delete it.

It may have an abstract property that needs to be declared to the heir, for example

 public abstract int ID { get; } 

or the method that it should implement:

 public abstract void DoSomething(); 

You would use it if the class itself had never been created, but you need more than an interface, such as basic properties, and you want to use it later:

 ((MyAbstractClass)obj).DoSomething(); //All inheritors implemented this 
+1
May 17 '10 at
source share

An abstract class serves two related purposes:

  • You can put abstract members in a class
  • You cannot instantiate an abstract class

The main purpose of an abstract class is to serve as a base class for descendant classes, providing, perhaps, some common functions, but at least providing a common set of methods that descendants must execute.

For example, you can create an abstract Fruit class with an abstract Eat method. Since you cannot actually eat “fruits,” you need to eat a certain type of fruit, the class is abstract, and you will never see an object labeled “Fruits” in the wild.

Instead, you will see objects like Apple, Banana, Orange, etc., which are descendants of Fruit, and which have a real implementation of Eat.

Apart from the two things above, there is nothing special about an abstract class.

+1
May 17 '10 at
source share

I don’t think anyone else said this, but I believe that the distinguishing characteristic of an abstract class is this:

The encoder knows how the X% of the class is implemented and how the (100-X)% of the class should be implemented, but which cannot.

This is what distinguishes an abstract class. Interfaces describe how the class should be implemented, but do not give developers any code or support. Base classes with virtual methods provide standard implementations that descendants can override.

Only an abstract class can provide a partial implementation, but cannot provide a full implementation.

+1
May 17 '10 at 12:56
source share



All Articles