RE: What is diff. between abstract class and an interface? * What is shadowing? * Diff between Overridin...
An Abstract class is a class with some common/certain implementations and define abstraction for other services which are implemented in its concrete sub classes, whereas interface only have method declaration with zero implementations.
RE: What is diff. between abstract class and an interf...
An abstract class and Interface both have method only but not have body of method.The difference between Abstract class and An Interface is that if u call Ablstract class then u have to call all method of that particular Abstract class but if u call an Interface then it is not necessary that u call all method of that particular interface.Method OverLoading:-Return type, Parameter type, parameter and body of method number may be different.Method Overriding:- Return type, Parameter type, Parameter Number all must be same . Only body of method can change.
RE: What is diff. between abstract class and an interf...
An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.
Feature
Interface
Abstract class
Multiple inheritance
A class may inherit several interfaces.
A class may inherit only one abstract class.
Default implementation
An interface cannot provide any code, just the signature.
An abstract class can provide complete, default code and/or just the details that have to be overridden.
Constants
Only Static final constants.
Both instance and static constants are possible.
Core VS Peripheral
Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
An abstract class defines the core identity of a class and there it is used for objects of the same type.
Homogeneity
If the various implementations only share method signatures then it is better to use Interface.
If the various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
Speed
Requires more time to find the actual method in the corresponding classes.
Fast
Adding functionality
If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
RE: What is diff. between abstract class and an interf...
What is Shadowing?
Shadowing is a concept of polymorphism usage in Object Oriented Programming. This is a concept related to over-riding functions at run-time or making a shadow of the object's methods in the inherited classes. Let me explain you in terms of technology now. Though it is not much used in programming but it may be used sometimes and its usage is very restrictive and unique.
Implementation of override to override a method Suppose you have a base class ClassA in which you have written a method MethodA. Now, if you want to inherit this class in a ClassB and use MethodA then the MethodA declared in ClassA will be used but if you override this method in your inherited class classB then it will be over-ridden and the implementation of classB will be used whenever we use it from classB and the implementation of classA will be used whenever we use it from a instance of classA.
using System; using System.Diagnostics; using System.IO; public class ClassA { public virtual void MethodA() { Trace.WriteLine("BaseClass MethodA"); } }
public class ClassB :ClassA{ public override void MethodA() { Trace.WriteLine("SubClass MethodA overridden"); } }
//implementation class for using the above defined classes public class TopLevel { static void Main(string[] args) { TextWriter tw = Console.Out; Trace.Listeners.Add(new TextWriterTraceListener(tw));
ClassA obj = new ClassB(); obj.MethodA(); // Output will be “Subclass MethodA overridden” } }
Shadowing instead of overriding Upto now the code shown was for overriding a base class method with the child class method, But what will happen If you want to provide the base class behaviour instead, use the new directive, with or without virtual at the base class Is this possible in C#? Yes, it is and that is the concept of shadowing in C# using a keyword "new' which is a modifier used instead of "override".
public class ClassA { public virtual void MethodA() { Trace.WriteLine("ClassA Method"); } }
public class ClassB : ClassA { public new void MethodA() { Trace.WriteLine("SubClass ClassB Method"); } }
public class TopLevel { static void Main(string[] args) { TextWriter tw = Console.Out; Trace.Listeners.Add(new TextWriterTraceListener(tw));
ClassA obj = new ClassB(); obj.MethodA(); // Outputs “Class A Method"
RE: What is diff. between abstract class and an interf...
Over Loading and Overriding
In a class if two method is having the same name and different signature,its known as overloading in Object oriented concepts.
For eg Take the case of a Shape Class. having a method with a name DrawShape();
This method has two definitins with different parameters.
public void DrawShape(int x1, int y1,int x2,int y2) { // draw a rectangle. }
public void DrawShape(int x1,int y1) {
// draw aline. }
Overriding means, to give a specific definition by the derived class for a method implemented in the base class.
For eg.
Class Rectangle {
publc void DrawRectangle() { // this method will draw a rectangle. }
}
Class RoundRectangle : Rectanlge {
public void DrawRectangle() {
//Here the DrawRectangle() method is overridden in the // derived class to draw a specific implementation to the //derived class, i.e to draw a rectangle with rounded corner.
RE: What is diff. between abstract class and an interf...
Over Loading and Overriding
In a class if two method is having the same name and different signature,its known as overloading in Object oriented concepts.
For eg Take the case of a Shape Class. having a method with a name DrawShape();
This method has two definitins with different parameters.
public void DrawShape(int x1, int y1,int x2,int y2) { // draw a rectangle. }
public void DrawShape(int x1,int y1) {
// draw aline. }
Overriding means, to give a specific definition by the derived class for a method implemented in the base class.
For eg.
Class Rectangle {
publc void DrawRectangle() { // this method will draw a rectangle. }
}
Class RoundRectangle : Rectanlge {
public void DrawRectangle() {
//Here the DrawRectangle() method is overridden in the // derived class to draw a specific implementation to the //derived class, i.e to draw a rectangle with rounded corner.
RE: What is diff. between abstract class and an interf...
The OverLoading is nothing but the class which has a differ sign with the same method name. In overridiging is the method which is dervied from the base class which has been changed in sub class of it's functionality.
RE: What is diff. between abstract class and an interf...
Shadowing means Same as Over riding concept.. but the overriding will affect when the override keyword is given in base class.. but if the key word is not given then we can't over ride the method.. but when using the shadowing means with out the keyword we will bale to override the method.
RE: What is diff. between abstract class and an interf...
I beg to differ with you on the purest definition of an interface. You claim that an interface in not a class based on the fact that in .NET you use the interface key word to define it however this is a very common misconception.An Interface is a class but it is a very special type of class. It is an abstract base class with no implementation such that all of its members are public and pure. Pure meaning no implementation. This goes all the way back to COM.
RE: What is diff. between abstract class and an interf...
Dear Sujit kumar,
The following lines of your comments marked in red need to be corrected.
Method OverLoading:-Return type, Parameter type, parameter and body of method number may be different.Method Overriding:- Return type, Parameter type, Parameter Number all must be same . Only body of method can change
For Method overLoading (Also referred as method redefineing) there is no scope if you have different return type, with rest of the method signature same. let me take an example.
consider a function prints()
void prints();
String prints();
These two signatures do not confine to the method overLoading rules.
So the return type plays no role in method OverLoading process.