Difference between Implementation Inheritance and Interface Inheritance

Jaideep

New member
Joined
Jul 11, 2006
Messages
1
Programming Experience
Beginner
Which one is recommended in .NET and Why?
A. Implementation Inheritance
OR
B. Interface Inheritance.

I will appreciate any help on this ...
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+
A. You create inherited classes to benefit from base functionality already implemented (although overridable).

B. You implement an interface to enable accessing different classes on an equal foundation. An instance of an interface (or typed parameter) can be assigned instances of any class that implements it.
 

Apexprim8

Member
Joined
Oct 6, 2005
Messages
18
Location
Cheshire, United Kingdom
Programming Experience
Beginner
Because Implementation Inheritance does not always guarantee polymorphic subsititutability (i.e. just because TennisBall uses Ball as a base class does not always ensure Ball can be substituted by TennisBall), the tendency is more towards the use of delegates.

Click here for a discussion of Implementation Inheritance.

Basically dude it depends on many things but they are both used in different situations afaik.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+
I was perhaps not very clear with another difference, but it kinda goes with the expression: "implement an interface" also does mean that the class must write the code for each member of the interface (to fullfill the 'contract' with the interface).

Another main difference is that a class can only inherit one single base class, there can be multiple levels of inheritance but only one-to-one parent-child relation. A class can implement several interfaces.
 

Apexprim8

Member
Joined
Oct 6, 2005
Messages
18
Location
Cheshire, United Kingdom
Programming Experience
Beginner
So, back to the original question, there isn't a "preferred" practice. VB.Net allows you to benefit from both of the methods at the same time. Different languages use both to a greater or lesser extent but to ask "what is the recommended approach in .Net" is purely rhetorical, academic, and certainly not a mutually exclusive relationship.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,602
Location
Sydney, Australia
Programming Experience
10+
If I was defining a Car class and a Horse class, Car would inherit Vehicle and Horse would inherit Animal, but both would implement IModeOfTransport. Interfaces are usually used to allow you to treat disparate objects in the same way. As has been said, each method is used in the situations where it is appropriate for that method.
 
Top Bottom