What is difference between abstract and interface
whatis difference between abstract and interface
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Datta KharadPosted Dec 19, 2011, 9:15 AM
Abstract Class:-
1) An abstract method is created by specifying the abstract type modifier.
2) An abstract method contains no body.
3) An abstract method is not implemented by the base class.
4) An abstract method is automatically virtual.
5) A derived class must override it.
6) Abstract class can have modifiers for methods,properties etc.,
7) An abstract class can implement a property.
8) The abstract modifier cannot be applied to static methods.
9) Properties can also be abstract.
10) A class containing abstract methods must be declared as abstract with the abstract specifier.
11) There can be no objects of an abstract class.
12) If a derived class doesn't implement all of the abstract methods in the base class, then the derived class must also be specified as abstract.
13) An abstract class can inherit from a class and one or more interfaces.
14) An abstract class can implement code with non-Abstract methods.
15) Abstract class can have constant and fields.
16) An abstract class can have constructors or destructor's.
17) An abstract class cannot be inherited from by structures.
18) An abstract class cannot support multiple inheritance.
19) 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.
Example Abstract Class:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractClassDemo2
{
abstract class Employee
{
public Employee(string name, float billingRate)
{
this.name = name;
this.billingRate = billingRate;
}
virtual public float CalculateCharge(float hours)
{
return (hours * billingRate);
}
abstract public string TypeName();
private string name;
protected float billingRate;
}
class Manager : Employee
{
public Manager(string name, float billingRate): base(name, billingRate)
{
}
override public float CalculateCharge(float hours)
{
if (hours < 1.0F)
hours = 1.0F; // minimum charge.
return (hours * billingRate);
}
// This override is required, or an error is generated.
override public string TypeName()
{
return ("Manager");
}
}
class Clerk : Employee
{
public Clerk(string name, float billingRate) :base(name, billingRate)
{
}
override public string TypeName()
{
return ("Clerk");
}
}
class Test
{
public static void Main()
{
Employee[] earray = new Employee[2];
earray[0] = new Manager("A", 40.0F);
earray[1] = new Clerk("C", 45.0F);
Console.WriteLine("{0} charge = {1}",earray[0].TypeName(),earray[0].CalculateCharge(2F));
Console.WriteLine("{0} charge = {1}",earray[1].TypeName(),earray[1].CalculateCharge(0.75F));
}
}
}
Interface:-
Interfaces in C # provide a way to achieve runtime polymorphism. Using interfaces we can achieve multiple inheritance i,e we can invoke functions from different classes through the same Interface reference, whereas using virtual functions we can invoke functions from different classes in the same inheritance hierarchy through the same reference.
Example:-
interface a
{
void display(); //it cant be public
//By default it is abstract and public
}
interface b
{
void display1();
}
class c : a,b
{
public void display()
{
System.Console.WriteLine("Interface");
}
public void display1()
{
System.Console.WriteLine("Interface one");
}
}
class d
{
public static void Main()
{
c t=new c();
t.display();
t.display1();
}
}
Satyapriya NayakPosted Dec 19, 2011, 8:11 AM
Abstract class
A class that cannot be instantiated. An abstract class is a class that must be inherited and have the methods overridden. An abstract class is essentially a blueprint for a class without any implementation.
An abstract class is a special kind of class that cannot be instantiated. It normally contains one or more abstract methods or abstract properties. It provides body to a class.
Interface
An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body.
It's an abstract class with public abstract methods all of which must be implemented in the inherited classes.
In which Scenario you will go for Interface or Abstract Class?
Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes. Even though class inheritance allows your classes to inherit implementation from a base class, it also forces you to make most of your design decisions when the class is first published.
Abstract classes are useful when creating components because they allow you specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class is needed. They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code.
Difference Abstract class vs Interface
In the interface all methods must be abstract, in the abstract class some methods can be concrete. In the interface no accessibility modifiers are allowed, which is ok in abstract classes.
Thanks
Pravin MorePosted Dec 19, 2011, 7:47 AM
1) A class may inherit only one abstract class, but may implement multiple number of Interfaces.
Say a class named Car needs to inherit some basic features of a vehicle, it may inherit from an Aabstract class named Vehicle. A car may be of any kind, it may be a vintage car, a sedan, a coupe, or a racing car. For these kind of requirements, say a car needs to have only two seats (means it is a coupe), then the class Car needs to implement a member field from an interface, that we make, say ICoupe.
2) Members of an abstract class may have any access modifier, but members of an interface are public by default, and cant have any other access modifier.
3) Abstract class methods may OR may not have an implementation, while methods in an Interface only have a definition, no implementation.
Thanks,
Pravin.
Vikrant JadhavPosted Dec 19, 2011, 7:45 AM
In an interface class, all methods are abstract - there is no implementation.
In an abstract class some methods can be concrete - there can be implementation.
In an interface class, no accessibility modifiers are allowed - are public by default.
In an abstract class accessibility modifiers are allowed.
Thanks & Regards
Vikrant Jadhav
Mark this as a Correct Answer if this post helpful to you.