473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

using Interface and abstract classes

Hello!!

Assume you have an Interface called ITest with these three method
declarations.
interface ITest
{
void foo1();
void foo2();
void foo3();
}

Assume that you "inherit" this interface to an abstract class called ABCBase
like this.
abstract public class ABCBase : ITest
{
//Here you add some more.
abstract public void goo1();
abstract public void goo2();
abstract public void goo3();

abstract public void foo1();
abstract public void foo2();
abstract public void foo3();
}

Below we have class RealClass that implements all six methods that must be
implemented.
Now to my question I have noticed that I must redeclare all three from the
interface like this
abstract public void foo1(); abstract public void foo2(); abstract public
void foo3();
in the abstract ABCBase class despite having a definition in the RealClass.
Does it really have to be in that way. In a way it feels unnessessary to do
this. Is this a misstake in
the construction of C# having to declare methods in the abstract class when
the same method exist in the interface.

My next question is it common to have this construction with an interface
then an abstract class
that "inherit" and finally the the real class that make the method
definitions.
public class RealClass : ABCBase
{
void foo1()
{//some code here }

void foo2()
{//Some code here}

void foo3()
{//some code here}
void goo1()
{//some code here }

void goo2()
{//Some code here}

void goo3()
{//some code here}
}

//Tony



Apr 22 '06 #1
5 1817
First of all, your code would not compile, since you are not overriding the
abstract methods in your RealClass (missing override keyword).

Yes, you have to declare the methods from interface as abstract methods in
your abstract class and this is a common practice.

Maybe this article would give you some insight:
http://msdn.microsoft.com/msdnmag/is...12/NETMatters/

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:cy*******************@newsb.telia.net...
Hello!!

Assume you have an Interface called ITest with these three method
declarations.
interface ITest
{
void foo1();
void foo2();
void foo3();
}

Assume that you "inherit" this interface to an abstract class called
ABCBase like this.
abstract public class ABCBase : ITest
{
//Here you add some more.
abstract public void goo1();
abstract public void goo2();
abstract public void goo3();

abstract public void foo1();
abstract public void foo2();
abstract public void foo3();
}

Below we have class RealClass that implements all six methods that must be
implemented.
Now to my question I have noticed that I must redeclare all three from the
interface like this
abstract public void foo1(); abstract public void foo2(); abstract public
void foo3();
in the abstract ABCBase class despite having a definition in the
RealClass.
Does it really have to be in that way. In a way it feels unnessessary to
do this. Is this a misstake in
the construction of C# having to declare methods in the abstract class
when the same method exist in the interface.

My next question is it common to have this construction with an interface
then an abstract class
that "inherit" and finally the the real class that make the method
definitions.
public class RealClass : ABCBase
{
void foo1()
{//some code here }

void foo2()
{//Some code here}

void foo3()
{//some code here}
void goo1()
{//some code here }

void goo2()
{//Some code here}

void goo3()
{//some code here}
}

//Tony


Apr 22 '06 #2
Your problem is that you are thinking like a C++ programmer: interfaces are
not just classes with abstract methods - You don't inherit interfaces in C#
you implement them.

What you are saying is that ABCBase implements ITest......but it doesn't so
you have to make a derived class do it for you hence the abstract.

I do agree that it is a bit odd though.

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:cy*******************@newsb.telia.net...
Hello!!

Assume you have an Interface called ITest with these three method
declarations.
interface ITest
{
void foo1();
void foo2();
void foo3();
}

Assume that you "inherit" this interface to an abstract class called
ABCBase like this.
abstract public class ABCBase : ITest
{
//Here you add some more.
abstract public void goo1();
abstract public void goo2();
abstract public void goo3();

abstract public void foo1();
abstract public void foo2();
abstract public void foo3();
}

Below we have class RealClass that implements all six methods that must be
implemented.
Now to my question I have noticed that I must redeclare all three from the
interface like this
abstract public void foo1(); abstract public void foo2(); abstract public
void foo3();
in the abstract ABCBase class despite having a definition in the
RealClass.
Does it really have to be in that way. In a way it feels unnessessary to
do this. Is this a misstake in
the construction of C# having to declare methods in the abstract class
when the same method exist in the interface.

My next question is it common to have this construction with an interface
then an abstract class
that "inherit" and finally the the real class that make the method
definitions.
public class RealClass : ABCBase
{
void foo1()
{//some code here }

void foo2()
{//Some code here}

void foo3()
{//some code here}
void goo1()
{//some code here }

void goo2()
{//Some code here}

void goo3()
{//some code here}
}

//Tony


Apr 22 '06 #3
You just need to look at the problem a bit differently. In C++ ITest
would be a
pure virtual class and RealABC would inherit from both ABCBase and ITest
using
C++ support for multiple inheritance. C# supports single inheritance of
implementation and multiple inheritance of interfaces or pure virtual
classes. So
you could just have RealABC "inherit" from ABCBase and "implement"
ITest. If
ABCBase has no implementation details then in C# you would create two
interfaces IABC and ITest and RealABC would implement IABC and ITest.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Apr 22 '06 #4
On Sat, 22 Apr 2006 16:08:40 GMT, Tony Johansson wrote:
Does it really have to be in that way. In a way it feels unnessessary to do
this. Is this a misstake in
the construction of C# having to declare methods in the abstract class when
the same method exist in the interface.


An Interface is only a contract, one that says that any class that
implements the interface must implement all methods/properties defined in
the interface. If you class, abstract or not, is going to implement an
interface, then it must implement the entire interface. It's not a
mistake.
--
Tom Porterfield
Apr 22 '06 #5
> Is this a misstake in the construction of C# having to declare methods
in the abstract class when the same method exist in the interface.


No, in fact it's by intelligent design that you must explicitly say
that you are leaving the methods abstract instead of leaving it as an
implicit assumption. Think about what would happen if you could leave
those abstract declarations out.

In the class RealClass, you would be overriding methods but it wouldn't
be clear just where you were overriding them from, because they would
appear nowhere in ABCBase, and RealClass would have no direct reference
to ITest.

In the class ABCBase, you would apparently be implementing the
interface ITest, but you would never explicitly say what you were doing
about its methods. If goo1, goo2, and goo3 weren't abstract in ABCBase,
it wouldn't be clear why ABCBase was abstract.

Yes, if you noticed the ": ITest" after ABCBase, everything would be
clear, but the syntax required by C# makes things completely explicit.
This way, ABCBase must explicitly state that yes, it is leaving the
methods in ITest to be implemented by derived classes.

Furthermore, if you add a new method to ITest at some point in the
future, it is ABCBase that will fail to compile, which is what you
would want: the consideration of what to do about an addition to the
interface must start back at ABCBase, which can choose to either
implement the new method or make it abstract, which then passes the
problem to derived classes. By eliminating implicit assumptions, the
language deals more gracefully with change.

Apr 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
26
by: Marius Horak | last post by:
As in subject. Thanks MH
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
9
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
12
by: craigkenisston | last post by:
Hi, I will use an example to do a simplified description of my problem, please don't laugh. I just "believe" I would have to use either interface or abstract classes, but I've not ever write an...
10
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract...
10
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what...
1
by: /M | last post by:
Hi! As you know, one way to seperate platform dependent code from platform _independent_ code is to create an abstract base class which acts as an interface towards all platform independent code...
9
by: msbs1984 | last post by:
Difference Between Interface and Abstract Class?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.