Abstract Class
An Abstract Class is just like any other class with a few key differences. As we discussed in my previous post, non-static classes can be instantiated except with one type of class - an Abstract Class. In other words, an abstract class is one that cannot be instantiated and can only be used as a parent class in other derived (concrete) classes. Since this class cannot be instantiated and you cannot use a non-static class without instantiating it, you cannot use the abstract class by itself. You must inherit from it in other class(es) to use it.
An abstract class may have one or more methods that are completely implemented but it must have at least one abstract method that must be implemented in the derived class(es). This is what makes the abstract class - abstract.
So, in a nutshell an abstract class is just like any other class except that it cannot be instantiated and must have one or more abstract (unimplemented) methods.
Interface
An Interface is not a class. It is a contract and all classes that implement the interface must implement all the properties/methods declared in an Interface.
Unlike an abstract class, an interface cannot have actual implementation of any property/method and also all the properties / methods must be declared public.
Since an interface cannot have any implementation, all the classes inheriting this interface must implement all the methods defined in the interface. Abstract class on the other hand can have fully implemented methods and derived classes can simply use that implementation.
If an abstract class has all the methods defined as abstracts, then both Interface and Abstract class is the same.
Why would you use one over the other?
Both interfaces and abstract classes are good if you want to keep the same structure in your classes inheriting from it. But abstract class allows you to implement one or more common method that can be used as is by all the classes. One advantage that interface have over abstract class is that at least in C# and VB.NET (also true in Java but not in C++) a class can only inherit from one class, while it can implement multiple interfaces.
Interfaces are generally used to define the abilities of a class i.e. what a class can do. For example - IComparable interface. This interface defines "Compare To" signature which can be implemented in the class to compare the instance of an object with another object of the same type. Any class that implements IComparable interface must be capable of doing so.
Abstract classes on the other hand generally implement or define the core capability of the class. For example a MemoryStream class can inherit from a Stream abstract class that implements a Serialize method to serialize the content.
You generally want to use interface if various classes only share certain features, although they may be of different type. For example, a class called Car and a class called Plane may implement the same IMovable interface. You want to use an abstract class when both classes are of same or similar kind. For example a class "BMW" and a class "Lexus" can inherit from the same abstract class called "Car".
If you modify an interface and add a new method, you must modify all the classes implementing that interface to implement that method. You can however modify an abstract class and as long as you fully implement a new method, you don't have to touch other classes inheriting from it.
Let's see both Interface and Abstract class in action.
Abstract Class
Using System;
namespace AbstractInterface
{
public abstract class Student
// notice keyword abstract. In VB.NET the equivalent keyword is MustInherit
{
protected string studentnum; //protected variables
protected string studentname;
public abstract String StudentNumber
// notice the keyword abstract. The property is not fully implemented.
{
get;
set;
}
public abstract String StudentName
{
get;
set;
}
public String GetStudentInfo() // notice this method is fully implemented
{
return "Student Number: " + studentnum + " Name: " + studentname;
}
public abstract String EnrollmentStatus(); // notice this method is not implemented.
}
}
namespace AbstractInterface
{
public abstract class Student
// notice keyword abstract. In VB.NET the equivalent keyword is MustInherit
{
protected string studentnum; //protected variables
protected string studentname;
public abstract String StudentNumber
// notice the keyword abstract. The property is not fully implemented.
{
get;
set;
}
public abstract String StudentName
{
get;
set;
}
public String GetStudentInfo() // notice this method is fully implemented
{
return "Student Number: " + studentnum + " Name: " + studentname;
}
public abstract String EnrollmentStatus(); // notice this method is not implemented.
}
}
Concrete Class
using system;
namespace AbstractInterface
{
public class GraduateStudent : Student // this class inherits from Student class.
{
public GraduateStudent() //constructor
{
}
public override String StudentNumber
// remember abstract class didn't implement this property so we have to implement it here.
// Notice keyword override
{
get
{
return studentnum;
}
set
{
studentnum = value;
}
}
public override String StudentName
{
get
{
return studentname;
}
set
{
studentname = value;
}
}
public new String GetStudentInfo() // this method is implemented in base class.
{
return base.GetStudentInfo();
}
}
public override String EnrollmentStatus () // notice this method is not implemented in base class
{
return studentname + " is enrolled."
}
}
}
namespace AbstractInterface
{
public class GraduateStudent : Student // this class inherits from Student class.
{
public GraduateStudent() //constructor
{
}
public override String StudentNumber
// remember abstract class didn't implement this property so we have to implement it here.
// Notice keyword override
{
get
{
return studentnum;
}
set
{
studentnum = value;
}
}
public override String StudentName
{
get
{
return studentname;
}
set
{
studentname = value;
}
}
public new String GetStudentInfo() // this method is implemented in base class.
{
return base.GetStudentInfo();
}
}
public override String EnrollmentStatus () // notice this method is not implemented in base class
{
return studentname + " is enrolled."
}
}
}
Now lets see an Interface Example
Interface
Public interface IStudent // As a convention, interfaces are prefixed with "I"
{
String StudentNumber
// No need to specific "public" since interface can only have public properties/methods.
{
get;
set;
}
String StudentName
{
get;
set;
}
String GetStudentInfo();
String EnrollmentStatus();
// notice no implementation of any method or property
}
{
String StudentNumber
// No need to specific "public" since interface can only have public properties/methods.
{
get;
set;
}
String StudentName
{
get;
set;
}
String GetStudentInfo();
String EnrollmentStatus();
// notice no implementation of any method or property
}
Concrete Class using Interface
using system;
namespace AbstractInterface
{
public class Student2 : IStudent // implements interface
protected string studentnum; //protected variables
protected string studentname;
public Student2 () //constructor
{
}
public String StudentNumber
// must be implemented here, otherwise compiler will throw error.
{
get
{
return studentnum;
}
set
{
studentnum = value;
}
}
public String StudentName
{
get
{
return studentname;
}
set
{
studentname = value;
}
}
public String GetStudentInfo() // implemented here
{
return "Student Number: " + studentnum + " Name: " + studentname;
}
}
public String EnrollmentStatus ()
{
return studentname + " is enrolled."
}
// notice - all properties and methods declared in interface must be implemented. Every class inheriting //from this interface must implement all of them.
}
}
namespace AbstractInterface
{
public class Student2 : IStudent // implements interface
protected string studentnum; //protected variables
protected string studentname;
public Student2 () //constructor
{
}
public String StudentNumber
// must be implemented here, otherwise compiler will throw error.
{
get
{
return studentnum;
}
set
{
studentnum = value;
}
}
public String StudentName
{
get
{
return studentname;
}
set
{
studentname = value;
}
}
public String GetStudentInfo() // implemented here
{
return "Student Number: " + studentnum + " Name: " + studentname;
}
}
public String EnrollmentStatus ()
{
return studentname + " is enrolled."
}
// notice - all properties and methods declared in interface must be implemented. Every class inheriting //from this interface must implement all of them.
}
}
Hopefully this will help clarify some of the differences between an Interface and an Abstract class. As always, your comments are welcome.
No comments:
Post a Comment