techmad
techmad

Reputation: 933

Generics and Abstract class

I have implemented Generices as following:

  public class GenericAnimal<T> 
  {
    public int legs;
    public void Walk()
    {
        Console.WriteLine("Animals Walk....");
    }
}

public class Dog 
{
    public void Bark()
    {
        Console.WriteLine("Barking....");
    }
}

public class Cat 
{
    public void Mew()
    {
        Console.WriteLine("Mewing....");
    }
}

Then I am using the class as following:

        GenericAnimal<Dog> dogObj = new GenericAnimal<Dog>();
        GenericAnimal<Cat> catObj = new GenericAnimal<Cat>(); 

In both the dogObj and catObj, I am getting the "Walk" method. This thing I can do using abstract class. Then what is the added advantage of using Generics over Abstract class?

Upvotes: 0

Views: 160

Answers (5)

Michael
Michael

Reputation: 9042

Your specific example is better solved by normal inheritance without generics - both Dog and Cat subclass Animal. However, say you now want a list of animals. As a non-generic solution you might choose an ArrayList, to which you can add cat and dog instances. However, the Add method of ArrayList takes an object as a parameter. Therefore, you could also add a fish to the list, and a fish can't walk. When you process the list to tell all of the animals to walk, you first have to check they are an animal.

This is where generics help. You can define your list using List<Animal> and then only objects that conform to "is Animal" can be added to the list. On the assumption that a fish is not an animal, then if you try to add one you will get a compiler error. Furthermore, when you get the items in the list you get out Animal instances rather than just objects, so you can tell them to walk immediately.

Upvotes: 1

Mayank
Mayank

Reputation: 8852

Generics and Inheritance are two entirely different concepts and have entirely different functions. There no such comparison "what is the added advantage of using Generics over Abstract class?". If you can abstract some common functionality from all your child objects in that sense it makes sense to use Abstract class. Here is more information on Generics.

Upvotes: 1

RredCat
RredCat

Reputation: 5431

Change your code to next

public class GenericAnimal<T> 
{
  public T Instance{get;set;}
  public int legs;
  public void Walk()
  {
      Console.WriteLine("Animals Walk....");
  }
}

And you will be able to use Dog and Cat. But eyossi described better code for such kind of situation.

Upvotes: 0

eyossi
eyossi

Reputation: 4340

try to write that instead:

public class Animal
{
  public int legs;
  public void Walk()
  {
      Console.WriteLine("Animals Walk....");
  }
}

public class Dog : Animal
{
   public void Bark()
    {
      Console.WriteLine("Barking....");
    }
}

public class Cat : Animal
{
   public void Mew()
   {
      Console.WriteLine("Mewing....");
   }
}

and try making two lists: the first one is list of cats only, the second one is a list of dogs only - first without using List<T> and then try using List<T>. i think that it will help you understand it better

Upvotes: 0

Nikola Anusev
Nikola Anusev

Reputation: 7088

In your case, none. Generics make sense in scenarios where you want a code to be reusable with different types. In GenericAnimal<T>, you are not doing anything with T.

Upvotes: 5

Related Questions