Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Types vs Abstract class/Interfaces

Suppose we are creating a generic control in .NET. E.g. a tree. I don't understand why people use this generic type definition

Control<T>

when in Object Oriented Programming I can use an abstract class or an interface:

Control<IItem> or Control<BaseClass>

So the only thing to do is that, their types must derive from that base class or implement the interface. Does it mean, that generic types are more convenient, because you I don't have to implement or inherit anything?

like image 918
theSpyCry Avatar asked Jul 08 '09 09:07

theSpyCry


People also ask

What is the difference between abstract class and interface?

Abstract class and interface both can't be instantiated. But there are many differences between abstract class and interface that are given below. 1) Abstract class can have abstract and non-abstract methods.

Can interface icustomer3 inherit to abstract class Customer1?

Therefore, interface ICustomer3 can not inherit to abstract class Customer1. Now, I believe you will be able to know the key difference between Abstract Class and Interface in C#.

What is the difference between implementation and abstraction in Java?

Inheritance vs Abstraction: A Java interface can be implemented using the keyword “implements” and an abstract class can be extended using the keyword “extends”. Multiple implementations: An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.

What is an abstract class in Java?

An abstract class is nothing but a class that is declared using the abstract keyword. It also allows us to declare method signatures using the abstract keyword (abstract method) and forces its subclasses to implement all the declared methods. Suppose if a class has a method that is abstract, then the class itself must be abstract.


2 Answers

I think you a little bit confused. Generic types and abstract classes/interfaces serve different goals for different approaches in application design. Abstract classes/interfaces are for generalization of common functionality of a group of entities. Later on this API could be implemented differently, but since polymorphism is involved it will not impact anyone.

On other hand, sometimes you have a very similar implementation of something and the only difference is the type of the objects you are working with. Here you will need a generic. You could use polymorphism, but there is no need. For that purpose it's much clearer just to define an interface, make an implementation, and let the end user decide which kind of object to use.

The best example is List, where the main purpose of list is to store elements. The List implementor should not care which type of objects you are going to use, so later you will be able just define List and make use of integer list.

like image 158
Artem Barger Avatar answered Oct 10 '22 12:10

Artem Barger


Because in your case of a tree control, defining a generic Tree control would mean that Tree items can be of any type (you can also add certain constrains).

When instantiating a control, you would of course have to declare your item type (like in your second code examples with IItem and BaseClass).

If your Tree control wouldn't be a generic type, you would have to create several controls for each item type.

Why not just use interface/abstractBase type?
If you would just use an interface/abstract as your Item concrete class, you would be constrained by it's definition. You'd only see it's properties and methods. With generic Tree control and whatever item type, you're still able to access all item's properties and methods regardless of its interface implementation or parent class inheritance...

like image 24
Robert Koritnik Avatar answered Oct 10 '22 13:10

Robert Koritnik