🔒 Locked
Abstract classes vs. Interfaces
Advertisement
Original Post
I am in the middle of trying to find a ASP.NET programming job in Russia. The question everyone asks is 'Tell me the differences between interfaces and abstract classes and when you would use them' Can anyone briefly explain this and give a brief explanation that I can keep in my head. Since I write ASP.NET web pages I have never needed to really know this answer I understand Interfaces have no methods defined and .. I guess asbstract classes dont also.. But I am tired of sounding stupid when they ask this questions so... Anyone have a good example?? Or a link to a good example ? (I am using C#.NET)
Both interfaces and abstract classes enforce a 'contract' on what a class has to actually provide in terms of functionality. One of the biggest differences is that in C# you can inherit multiple interfaces but only a single class. Also the fact that an interface is not a class but define the peripheral abilities of a class while an abstract class defines the core identity of a class.
An interface does not have any implementation and is only a contract whereas an abstract class can provide various implementation for others to inherit.
There are probably around 10 main differences or so between the two and the concepts are completely different.
An interface does not have any implementation and is only a contract whereas an abstract class can provide various implementation for others to inherit.
There are probably around 10 main differences or so between the two and the concepts are completely different.
A good question. I've always used Base Classes to inherit from but can see the advantage of being able to utilise multiple interfaces instead of building a class hierarchy.
Hopefully someone can give a small example of when an interface is best over a base class.
edit:
Not sure if I am understanding this correctly but lets see:
Abstract Class
Use when some common code but need to enforce overriding of functions
Base Class
Use when there is common code but no need to override functions
Interface
Use when there is no common code but there is common functionality
Hopefully someone can give a small example of when an interface is best over a base class.
edit:
Not sure if I am understanding this correctly but lets see:
Abstract Class
Use when some common code but need to enforce overriding of functions
Base Class
Use when there is common code but no need to override functions
Interface
Use when there is no common code but there is common functionality
It really depends on the situtation. I mean there is absolutely no reason to use a 'base class' instead of an abstract class if you are never going to instantiate that object, because the abstract class enforces this. Obviously if your base class in the hierarchy is meant to be instantiated than you wouldn't want it to be abstract.
I think I come confusing abstract class and a virtual class. Or are they the same thing
What about the difference between a PURELY ABSTRACT CLASS vs. INTERFACE ?
Both have no methods defined right?
Anyone have any small sample game code I could study that shows the benefits.
I have tried to read some design pattern books but you really need to understand interfaces first.
Are there any differences between how C# and Java use interfaces and abstract classes ?
I am looking for a page or something that I can read over and over until I totally understand this.
What about the difference between a PURELY ABSTRACT CLASS vs. INTERFACE ?
Both have no methods defined right?
Anyone have any small sample game code I could study that shows the benefits.
I have tried to read some design pattern books but you really need to understand interfaces first.
Are there any differences between how C# and Java use interfaces and abstract classes ?
I am looking for a page or something that I can read over and over until I totally understand this.
I think one of your biggest misunderstandings is that an interface is not a class whereas an abstract class is.
I googled for the topic and this is the first link:
Abstract vs Interface
If you can't understand from that I'm not sure what else to say.
I googled for the topic and this is the first link:
Abstract vs Interface
If you can't understand from that I'm not sure what else to say.
Quote:
Original post by Anonymous Poster
Base class and abstract class are not at all mutually exclusive.
I think your whole problem has just been reading comprehention.
The OP stated he is using the C# language for ASP.Net development where there is an actual keyword for abstract classes and interface. Now if you would have read that than you would have noticed that any class can be a base class but only one that is defined as abstract will be. I could create a class that I want to be a base for others but still instantiate it, but if I want to make sure that never happens I can declare it abstract.
Next time read the post, it isn't about 'modern language vs. older' or whatever nonsense, it is about C#. And what are you talking about stone throwing? All I did was clear up a point for the original poster. Just accept the fact that you are wrong and walk away.. it's easy since you post as anonymous.
Quote:
Original post by Anonymous Poster
Yes, an abstract class can implement some of its methods, whereas interfaces cannot. My main point though was that abstract classes are considered abstract because they don't implement methods (for at least one method, though this is more of a modern language difference).
No. Abstract classes is used instead of concrete classes, when you need a class which should never itself be constructed, but contains implementation details. Interfaces, on the other hand, just makes sure classes (abstract or concrete) follow an interface.
Quote:
Original post by Anonymous Poster
Except that I did say I was wrong and already cleared it up.
Nowhere did you say that so I didn't take it as that. My bad.
Quote:
Regardless of which language you use, abstract classes are not mutually exclusive from base classes, as your post says. Maybe you should take your own advice here.
Nowhere did I say it was mutually exclusive, I said if you want to instantiate the base than don't make it abstract, otherwise you can gaurentee that it can't be.
Quote:
since the OP did not ask about implementation, only about the differences.
The reason I am focusing on C# is because it is the language the OP is using along with ASP.Net and I did not want to confuse him.
Quote:
Original post by angrytofu
So can anyone give me a example where doing something is easier because of using a custom interface ?
I never have the need to use a interface I guess since everything I make is simple. I am still waiting for the day I will realize to use one.
interface MessageTarget{ void SendMessage(string Message);}public class MessageToFile: MessageTarget{ void SendMessage(string Message){ /* ToFile(Message); */ }}public class MessageToScreen: MessageTarget{ void SendMessage(string Message){ /* ToScreen(Message); */ }}public class SomeModule{ private MessageTarget OutputTarget; public void SomeMethod(){ OutputTarget.SendMessage("Hey! In Some Method!"); }}So, how would you output to either the screen or a file without interfaces?
Sure, you could use a base class, but what if MessageToScreen already inherited from another base? You could add the MessageTarget there, but that might not be appropriate.
Sure, you could make the MessageTarget a delegate/event. That's probably the best way to do it actually for something this simple. What though if you needed more than just SendMessage? It'd be a pain to group a bunch of delegate/events and make sure they stayed in synch.
Is that phrase "A class may *inherit* several interfaces" in that article entirely correct? I mean, he mentions the difference "Core vs Peripheral", and then he uses the phrase "class inherits interfaces", which gives again the wrong impression. The way I see it, classes don't inherit interfaces, they merely implement them. Interfaces inherit from interfaces, classes inherit from classes, and classes implement interfaces. That's the real difference between interfaces and abstract classes: When a class B inherits from an abstract class A, not only B gets the attributes and methods of A, but more importantly objects of type B are naturally also of type A. When a class B implements an interface I, it just provides implementation for several functions, but that has no impact on its place amongst the type hierarchy. It doesn't become of 'type I'.
I understand everything from that link except one thing
How can emp contain anything ? I though emp (which is the interface) cannot contain data?
About the example above. I don't really know C++ too well so do not completely understand
private void InterfaceExample_Click(object sender,
System.EventArgs e)
{
try
{
IEmployee emp;
Emp_fulltime2 emp1 = new Emp_fulltime2();
//has to be casted because of the interface!
emp = (IEmployee) emp1;
emp.ID = "2234";
emp.FirstName= "Rahman" ;
emp.LastName = "Mahmoodi" ;
//call add method od the object
MessageBox.Show(emp.Add().ToString());
//call the CalculateWage method
MessageBox.Show(emp.CalculateWage().ToString());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
How can emp contain anything ? I though emp (which is the interface) cannot contain data?
About the example above. I don't really know C++ too well so do not completely understand
private void InterfaceExample_Click(object sender,
System.EventArgs e)
{
try
{
IEmployee emp;
Emp_fulltime2 emp1 = new Emp_fulltime2();
//has to be casted because of the interface!
emp = (IEmployee) emp1;
emp.ID = "2234";
emp.FirstName= "Rahman" ;
emp.LastName = "Mahmoodi" ;
//call add method od the object
MessageBox.Show(emp.Add().ToString());
//call the CalculateWage method
MessageBox.Show(emp.CalculateWage().ToString());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Quote:
It doesn't become of 'type I'.
[imo]
For all practical purposes it does. Sure it isn't in the type hierarchy, but it is assignable to type I. All of type I's events and methods are callable from the interface reference. A reference to type I can be a type B (or any other interface implementation) while the programmer effectively ignores what it is.
From what I understand of that information link IEmployee contained the property markers ID, Forename and LastName. They were defined in the class using the interface but I guess because it was declared in the interface creating a variable directly from IEmployee gave us access to its limited declared functions.
EG:
Now if I understood what I have read so far the ii variable has access to the names of the functions declared in IInterface and defined in CClassA and CClassB so will in effect execute the code specified.
Hopefully the more knowledgable out there can correct what I've understood wrong.
EG:
IInterface{ bool Initialise(** initialisation parameters **); void Update(void); void Print(** Print Format **);};class CClassA : IInterface{ int m_nA; String* m_sB; bool Initialise(** init params **) { m_sB = psB; m_nA = pnA; return true; }};class CClassB : IInterface{ float m_fA; bool Initialise(** init params **) { m_fA = pfA; return true; }};main(){ IInterface ii; CClassA ca = new CClassA(); CClassB cb = new CClassB(); // Use CClassA defined functions ii = (IInterface)ca; ii.Initialise(** params **); ii.Update(); ii.Print(** print format **); // Use CClassB defined functions ii = (IInterface)cb; ii.Initialise(** params **); ii.Update(); ii.Print(** print format **); }Now if I understood what I have read so far the ii variable has access to the names of the functions declared in IInterface and defined in CClassA and CClassB so will in effect execute the code specified.
Hopefully the more knowledgable out there can correct what I've understood wrong.
Quote:
Original post by angrytofu
I understand everything from that link except one thing
How can emp contain anything ? I though emp (which is the interface) cannot contain data?
About the example above. I don't really know C++ too well so do not completely understand
private void InterfaceExample_Click(object sender,
System.EventArgs e)
{
try
{
IEmployee emp;
Emp_fulltime2 emp1 = new Emp_fulltime2();
//has to be casted because of the interface!
emp = (IEmployee) emp1;
emp.ID = "2234";
emp.FirstName= "Rahman" ;
emp.LastName = "Mahmoodi" ;
//call add method od the object
MessageBox.Show(emp.Add().ToString());
//call the CalculateWage method
MessageBox.Show(emp.CalculateWage().ToString());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Quote:
Original post by Anonymous Poster Quote:
Original post by CTar Quote:
Original post by Anonymous Poster
Yes, an abstract class can implement some of its methods, whereas interfaces cannot. My main point though was that abstract classes are considered abstract because they don't implement methods (for at least one method, though this is more of a modern language difference).
No. Abstract classes is used instead of concrete classes, when you need a class which should never itself be constructed, but contains implementation details. Interfaces, on the other hand, just makes sure classes (abstract or concrete) follow an interface.
No what? You are saying two things here, both of which are very wrong.
1) that abstract classes have to contain implementation details
2) that interfaces make sure classes follow an interface. Interfaces are completely independant of the class paradigm.
1) He never said that, he said they are often used with implementations. Which they are.
2) We're not talking about paradigms, we're talking about C#, in which the interface keyword denotes a "class" or "interface" that does force a contract onto the implementing class - a contract whereby the class must adhere to the defined methods specified in the interface.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement