Inherit & implement Question in C#

H

hitendra15

Hi
i have 2 Interface IA,IB and base class BC and derived class CD please

that is

class CD : BC //Inherit

but i also want to implement IA and IB

when i write
class CD : BC,IA,IB it give error

please can any one suggest........

Thanks in advance
Hitendra
 
J

Jon Skeet [C# MVP]

i have 2 Interface IA,IB and base class BC and derived class CD please

that is

class CD : BC //Inherit

but i also want to implement IA and IB

when i write
class CD : BC,IA,IB it give error

please can any one suggest........

Well, the above should work fine.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
H

Hiten

Its working sorry man it was my mistake to check signature of function
to override

but please can you solve my one query

abstract class AC
{
public virtual Void VM()
{
console.writeline("I am Virtual");
}
public abstract void AM();
}

class testit : AC{
public override void VM()
{
console.writeline("I override it");
}
public override void AM()
{
console.writeline("I override Absyract");
}}

as we know we cannot initialise abstract class than how do i call
abstract mathod of abstract class AC?

please can you explain the way...
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hiten,
See my comments inline
abstract class AC
{
public virtual Void VM()
{
console.writeline("I am Virtual");
}
public abstract void AM();
}

class testit : AC{
public override void VM()
{
console.writeline("I override it");
}
public override void AM()
{
console.writeline("I override Absyract");
}}

as we know we cannot initialise abstract class than how do i call
abstract mathod of abstract class AC?

Probably by initialize you mean instantiate. Yes you cannot instantiate an
abstract class because it is partially implemneted.

The abstract methods of an abstract class is used to porvide an interface.
Similar to the real interfaces with the difference that the abstract classes
can also provide implenetation for some of their methods and properties as
well as can have data fields. Because of this difference one class can
inherit/implement only one abstract class, but to implement from as many
interfaces as it needs.

So, you use the same way abstract classes as you would use an interface. As
an example

void Foo(AC ac)
{
ac.AM();
}

....

testit = t = new testit(); // AC is abstract, thus cannot be instantiated
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hiten,
See my comments inline
abstract class AC
{
public virtual Void VM()
{
console.writeline("I am Virtual");
}
public abstract void AM();
}

class testit : AC{
public override void VM()
{
console.writeline("I override it");
}
public override void AM()
{
console.writeline("I override Absyract");
}}

as we know we cannot initialise abstract class than how do i call
abstract mathod of abstract class AC?

Probably by initialize you mean instantiate. Yes you cannot instantiate an
abstract class because it is partially implemneted.

The abstract methods of an abstract class is used to porvide an interface.
Similar to the real interfaces with the difference that the abstract classes
can also provide implenetation for some of their methods and properties as
well as can have data fields. Because of this difference one class can
inherit/implement only one abstract class, but to implement from as many
interfaces as it needs.

So, you use the same way abstract classes as you would use an interface. As
an example

void Foo(AC ac)
{
ac.AM();
}

....

testit = t = new testit(); // AC is abstract, thus cannot be instantiated
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hiten,

[Sorry for the previous posts. Had troubles with the ng reader.]

So the following is the example that I wanted to give.

void Foo(AC ac)
{
ac.AM();
}

....

testit = t = new testit(); // AC is abstract, thus cannot be instantiated

Foo(t);
 
H

Hiten

thansk man

please will you do one more favore to me?
I have interview today on C# please will give some hint and links to
prepare C# interview as well as Windows Form

Thanks
Hitendra
 
H

Hiten

Stoitcho Goustev

But i want to call virtual mathod of abstract class from outside of
derived class...how do i do that, is it possible?

base.AM(); //in derived class will call AC's AM

Regards
Hitendra
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hiten,

You cannot call base.AM() from the testit's AM implenetation. C# compiler
will report an error as soon as it sees this call.

I'd suggest to read some books or articles regarding the polymorphism in
OOP. But I believe that it would be easier for you if you think of abstract
classes as form of intefaces. If you don't have problem understanding the
concept of intrfaces you should have problems understanding abstract
classes. FYI C++ doesn't have intefaces per se. All interfaces are realized
via pure abstract classes.


HTH
Stoitcho Goutsev (100) [C# MVP]
 

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

Top