Inherits VS Implements....what's the difference?

  • Thread starter Thread starter Jim
  • Start date Start date
J

Jim

What is the difference between using Inherits and Implements? I think I get
it, but I want to be sure.....
 
December 21, 2005

Basically, Inherits allows you to have your class get a COPY of the parent
class's methods and code (and therefore you don't have to write it
again).... Basically, Implements is the same as Inherits, although there is
no CODE behind the methods. Therefore, you get the 'shell' of the methods,
but you have to write the code inside the methods, and therefore a custom
implementation.... Also, Implements is only used with Interfaces...

--

Joseph Bittman
Microsoft Certified Solution Developer
Microsoft Most Valuable Professional -- DPM

Blog/Web Site: http://71.39.42.23/
 
Jim said:
What is the difference between using Inherits and Implements? I think I
get it, but I want to be sure.....

Classes can inherit from other classes, and they can implement interfaces.
Implementing an interface means that the developer adds the behavior of the
methods defined in an interface the class implements to the class. By
inheriting implementations of the base class are made implicitly available
to the derived class. Interfaces can typically inherit from other
interfaces.
 
Inherits will include the implementation of each method/property of the
inherited class.

Implements will only give you the same interface. You need to provide your
own implementation.

The harder question is when to use each one. Here's my opinion:

You want to inherit a class when your new class is strongly related to the
class you want to inherit. Your new class should just be a specialized
version of the base class you are inheriting. These relationships are
typical described by "Is a". For example: Secretary is an Employee. In
this case all Employees have common atrtributes such as SS number, Office
Number, ... A Secretary will only have different responsibilities and
therefore can inherit most all of the Employee class.

The reason you would want to implement a class is because the relationship
can be linked through a common interface but the implementation of this
interface will be different for every class. For example: Bears are
animals. In this example, a Bear is really not a specialized version of
Animal because each animal is very different and don't really share any
common functions. But they all need to eat, sleep, ... It's just that the
do it very differently. Since every animal is so different you can't really.
Therefore inheritance is not correct. Instead all animals will implement
the Animal interface. So, they can be looked at as both a Bear and an
Animal. If you want to know the fancy OO word for this it is called
polymorphism.

Of course you could take this further and say that a Bear is a Mammal and
mix the whole thing up. In this case you would have Bear inheriting Mammal
which implements the Animal Interface. (OOD is fun!)
 
Beautiful description!

Thanks so much!

Joseph Bittman MVP MCSD said:
December 21, 2005

Basically, Inherits allows you to have your class get a COPY of the
parent class's methods and code (and therefore you don't have to write it
again).... Basically, Implements is the same as Inherits, although there
is no CODE behind the methods. Therefore, you get the 'shell' of the
methods, but you have to write the code inside the methods, and therefore
a custom implementation.... Also, Implements is only used with
Interfaces...

--

Joseph Bittman
Microsoft Certified Solution Developer
Microsoft Most Valuable Professional -- DPM

Blog/Web Site: http://71.39.42.23/
 
That makes things clearer for me.

Thanks!!

Herfried K. Wagner said:
Classes can inherit from other classes, and they can implement interfaces.
Implementing an interface means that the developer adds the behavior of
the methods defined in an interface the class implements to the class. By
inheriting implementations of the base class are made implicitly available
to the derived class. Interfaces can typically inherit from other
interfaces.
 
Another great explanation!

Thanks!


TrtnJohn said:
Inherits will include the implementation of each method/property of the
inherited class.

Implements will only give you the same interface. You need to provide
your
own implementation.

The harder question is when to use each one. Here's my opinion:

You want to inherit a class when your new class is strongly related to the
class you want to inherit. Your new class should just be a specialized
version of the base class you are inheriting. These relationships are
typical described by "Is a". For example: Secretary is an Employee. In
this case all Employees have common atrtributes such as SS number, Office
Number, ... A Secretary will only have different responsibilities and
therefore can inherit most all of the Employee class.

The reason you would want to implement a class is because the relationship
can be linked through a common interface but the implementation of this
interface will be different for every class. For example: Bears are
animals. In this example, a Bear is really not a specialized version of
Animal because each animal is very different and don't really share any
common functions. But they all need to eat, sleep, ... It's just that
the
do it very differently. Since every animal is so different you can't
really.
Therefore inheritance is not correct. Instead all animals will implement
the Animal interface. So, they can be looked at as both a Bear and an
Animal. If you want to know the fancy OO word for this it is called
polymorphism.

Of course you could take this further and say that a Bear is a Mammal and
mix the whole thing up. In this case you would have Bear inheriting
Mammal
which implements the Animal Interface. (OOD is fun!)
 
I INHERIT DNA from my parents and ancestors: Each bit of DNA in my cells
is a copy (of a copy of a copy ...) of a piece of DNA that was an actual
part of my father or my mother's body.

I IMPLEMENT my job function: there is a manual I am handed by my employer
which tells me what to do, but it's up to me to actually do it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top