What does extending a class mean?

  • Thread starter Thread starter needin4mation
  • Start date Start date
N

needin4mation

Hi, I read this in a book about the Xml classes in c#:

"These classes are abstract and therefore must be extended."

I just wanted to know what this statement means. I know it is not in
context, but the author gave it in such a matter that it appears
experience folks will know what it means to say a class is abstract and
extended.

Thanks.
 
Hi, I read this in a book about the Xml classes in c#:

"These classes are abstract and therefore must be extended."

I just wanted to know what this statement means. I know it is not in
context, but the author gave it in such a matter that it appears
experience folks will know what it means to say a class is abstract
and extended.

Thanks.

An abstract baseclass might contain some code (methods), but at least
has one "abstract" method. This is not a "real" method at all (it contains no
code), but rather some sort of "placeholder". By using an abstract method
you declare that all derived classes (classes that use this abstract class
as a baseclass) also have this method. If those derived classes are not
abstract themselves, they *must* provide code ("implement") these
abstract methods.

Why have methods that you can't use?
You can use that abstract type as a parameter type for some method
(this method might be in the same baseclass, or somewhere else).
When you call that method you have to specify some "real" object, that
derives from the (abstract) baseclass. Because the method (-signature)
is defined in that baseclass, the method (or rather compiler) knows it can
use it. How it is implemented is not important.

Hans Kesting
 
Basically, it means that you have to derive from the abstract base class
and implement functionality exposed by it (through abstract methods).

So if you had this:

public abstract class AbstractBase
{
// This needs to be implemented in any derived classes.
public abstract void DoSomething();
}

You need to do this:

public class Derived : AbstractBase
{
public override void DoSomething()
{
// Provide some implementation here.
}
}

Hope this helps.
 
An abstract class is one that you can not create instances of. You need to
create a class that inherits from it (extends it) and then you create
instances of this derived class. Abstract classes are used when you want to
provide base functionality that you need to refine necessarily in derived
class. For example, you can have an abstract class Animal (you can not
create instances of it, since it is so generic or "abstract" ...) and you
derive classes extending it like Dog, Cat, etc, which are creatable classes.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
abstract classes are similar to Interfaces in that you can designate
signatures of methods, but different from interfaces in that you can
specifically implement functionality within an abstract class whereas
in an interface you are constrained to only having signatures of its
members.

interface ISomething
{
void DoSomething();
void DoSomethingElse();
}

abstract class Something
{
void DoSomething();
void DoSomethingElse()
{
// code here
}
}
 
I also forgot to actually answer your original question. But it appears
it has mostly been taken care of by others. Basically an abstract class
cannot be used by itself and must be inherited from by another class.

If a member of an abstract class is abstract itself:

abstract class Something
{
public abstract void DoSomething();
public void DoSomethingElse()
{
// code here
}
}

The inheriting class must override the abstract member of the abstract
class, but is not required to implement the non-abstract members.

public class MySomething : Something
{
public override void DoSomething()
{
// code here
}
}
 

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