Register now or log in to join your professional community.
What is an Abstract Class?
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.
What is an Interface
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.
When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.
When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.
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. Interface:- 1) Interfaces cannot be instantiated directly. 2) Interfaces can contain events, method, indexer and properties. 3) An Interface can contain property definitions. 4) Interfaces contain no implementation of methods. 5) Classes and Structs can implement more than one interface. 6) An Interface can be inherited from by structures. 7) An interface itself can inherit from multiple interfaces (Interface can support multiple inheritance). 8) An abstract class can implement a property. 9) 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.
(IN JAVA)
Interface contains only public, final and static variables like (public final static int a; ) and public abstract methods like (public abstract m1(); ) by default.
Abstract method means, no definition is there. For this Interface we can't create object, and this should be Implemented by next class and abstract method definition should be given in Implementing class. To access it we have to createobject for implementing class and made a refernce to interface to use it. see it in example.
But we can access Interface without creating object of implementing class. How means with the help of annonymous class. (SEE BELOW EXAMPLES)
EX:- with creating a object for implemting class
Interface X
{
public abstract void m1();
}
class A implements X
{
void m1()
{
System.out.println("this is m1() which is in interface");
}
}
class Main
{
public static void main(String []args)
{
A a= new A();
interface i;
i=a;
i.m1();
}
}
Ex:- with annonymous class
Interface X
{
void m1();
}
class Main
{
X x = new X()
{
@override
public void m1()
{
System.out.println("this is m1() through annonymous class");
};
}
}
Abstarct Class is like user defined class but in this variable declaration & method declaration (abstract method) and method definition are present.
For this is also we can't create object. The Abstract class sholud be extended in next class. To access abstract class you have to create object for extending class through which you have to acess.
We access with annonymous class with creating object for extending class (similar to interface annonymous class usage (see example of interface annonymous class)).
Ex:-
abstract class A
{
int age;
abstract void m1();
void m2()
{
System.out.println("This is m2() in abstract class");
}
}
class B extends A
{
void m1()
{
System.out.println("This is m1() extending in class B");
}
void m3()
{
System.out.println("This is m3() present in B class");
}
}
class Main
{
public void static main(String args[])
{
B b= new B();
b.m1();
b.m2();
b.m3();
}
}
Interface -
Abstract Class -
Interface take less memory space or neglegible to store different section of your code
But in case of abstract class they need more memory spaces for methods of base class and also for Object class which is by default extended by all classes in java
Interface can be said as purely Abstract class.
in addition to the basic diffinations as mentioned here in many answers, the main and the technical difference is that an abstract class is used when two or more objects have IS_A relationship among them whereas interface is used when two or more non-related objects have to implement some common functionalities.....
INTERFACE:
interface is the collection of only abstract methods.
ABSRTACT CLASS:
abstract class means it should not have defination. only declaration present in this absrtact class.