Wednesday, October 16, 2019

Abstract Class vs Interface

These days you can count on this question in an interview:

https://techdifferences.com/difference-between-interface-and-abstract-class-in-java-and-csharp.html


 Interface-and-Abstract-ClassInterface and Abstract class both contribute to “incomplete type” in OOP. Sometimes we need a superclass to define “what to do” but, not “how to do”, it’s how to do part will be implemented by the derived class according to its need, “interface” provide a solution to this. Sometimes we need a superclass class that defines some generalised structure that can be implemented by derived classes and some specified structure that can be utilised by the derived classes, “abstract class” provides a solution to this. The fundamental difference between interface and abstract class is that interface is fully incomplete, and abstract class is partially incomplete.


Comparison chart

BASIS FOR COMPARISONINTERFACEABSTRACT CLASS
BasicWhen you only have the knowledge of the requirements not about its implementation, you use "Interface".When you partially know about the implementations you use "Abstract classes".
MethodsInterface only contains abstract methods.Abstract class contains abstract methods as well as concrete methods.
Access Modifier of MethodsInterface methods are always "Public" and "Abstract", even if we do not declare. Hence, it can be said as 100%, pure abstract class.It is not compulsory that method in abstract class will be public and abstract. It can have concrete methods also.
Restricted Modifier for MethodsAn interface method can not be declared with the following modifiers:
Public: Private and Protected
Abstract: final, static, synchronized, native, strictfp.
There are no restrictions on the modifiers of the abstract class variable.
Access Modifier of VariablesAcess Modifier allowed for Interface variables are public, static & final whether we are declaring or not.The variables in abstract class need not be public, static, final.
Restricted modifiers for VariablesInterface variables can not be declared as private, protected, transient, volatile.There is no restriction on the modifiers of abstract class variables.
Initialization of variablesThe interface variables must be initialized at the time of its declaration.It is not compulsory that abstract class variables must be initialized at the time of its declaration.
Instance and static blocksInside interface, you can't declare an instance or static block.Abstract class allows an instance or static block inside it.
ConstructorsYou can not declare constructor inside interface.You can declare constructor inside an abstract class.


Definition of Interface

Java doesn’t allow multiple inheritance. That is, a single class cannot inherit more than one class at a time. The reason behind this can be explained with an example. Let’s suppose we have two parent class, A and B and a derived class C. The derived class C inherits both the classes A and B. Now, both have the class A and B have method set( ), then it will be a question for class C that which class’s set( ) method should it inherit. The solution to this problem is “interface”.InterfaceInterface is a pure abstract class. The keyword used to create an interface is “interface”. As all the method inside interface are fully abstract. The interface only specifies what a class must do but, it does not define how it does it. Just because all the method declared inside the interface are abstract, no instance is created for an interface. The general form of “interface” in java is:
  1. access_specifier <strong>interface</strong> interface_name {
  2. return-type method-name1(parameter-list);
  3. return-type method-name2(parameter-list);
  4. type final-varname1 = value;
  5. type final-varname2 = value;
  6. // ...
  7. return-type method-nameN(parameter-list);
  8. type final-varnameN = value;
  9. }
The access specifier is declared public because the classes need to implement the interface.
We do not have the concept of “Interface” in C++.  But, Java and C# define interface very well.

Interface in Java:
  • Variables of an interface are by default always public, static and final.
  • Variables must be initialized at the time of its declaration.
  • Variables can never be declared as private, protected, transient and volatile.
  • Methods of an interface are always public and abstract whereas, they can never be declared as private, protected, final, static, synchronised, native and strictfp.
  • You can not declare any constructor inside interface as the main purpose of the constructor is the initialization of class variables but, in interface variables are initialized at the time of its declaration.
  • Interface can inherit other interfaces but, the class implementing such interface must implement the methods of all the inherited interfaces.
  • A class can inherit more than one interface at a time, and it must implement all the methods of all the inherited interfaces.


The general form of implementing an interface in Java:
  1. class class_name implements Interface_name{
  2. // class-body
  3. }
For inheriting an interface, a class uses a keyword “implements”, and the class implements all the method declared by an inherited interface.
Interface in C#:
Interface in C#  are almost similar to interface in Java except:
  • Interface in C# does not declare variables.
  • The name of the interface is prefixed with a capital I and is inherited with a colon (:) sign.
The general form of implementing an interface in C#:
  1. class class_name : interface_name {
  2. // class-body
  3. }

 Definition of Abstract Class

A class that contains one or more abstract methods is called abstract class, and a class is declared as abstract using the keyword “abstract”, preceded by the “class” keyword at the beginning of the class declaration. As the abstract class contains the abstract method it constitutes to an incomplete type. Hence, you can not create objects of an abstract class. Whenever a class inherits an abstract class, it must implement all the abstract methods of the abstract class if it doesn’t then it must also be declared as abstract.The abstract attribute is inherited until the complete implementation of abstract methods is achieved.
The abstract class can also contain concrete methods which can be utilised by the derived class as it is. But, you can not declare an abstract constructor or an abstract static method inside an abstract class. The general form of the abstract class in Java is as follow:

  1. abstract class class_name{
  2. abstract method_name1( );
  3. abstract method_name2( );
  4. :
  5. return_type method_name3( parameter_list){
  6. //concrete method
  7. }
  8. return_type method_name4( parameter_list){
  9. //concrete method
  10. }
  11. };
The concept of an abstract class is similar in both Java and C#. An abstract class is slightly different in C++.
 In C++ if a class at least have one virtual function the class becomes an abstract class. Instead of the keyword “abstract”, the keyword “virtual” is used to declare an abstract method.

Key Differences Between Interface and Abstract Class in Java & C#

  1. When you have the knowledge of “what is required” but not of “how it would be implemented” then interface must be used. On the other hand, if you know what is require and partially know how it would be implemented then use an abstract class.
  2. An interface has all its methods abstract but, an abstract class has some abstract methods and some concrete methods.
  3. The methods inside an interface are public and abstract hence, it is also called as a pure abstract class. On the other hand, the methods inside an abstract are not restricted to be public and abstract only.
  4. An interface method can never be private, protected, final, static, synchronized, native or strictfp. On the other hand, there are no restrictions to methods of an abstract class.
  5. The variables in an interface are public and final whether we declare them or not whereas, there is no such restriction to the variables of an abstract class to be public and final only.
  6. Variables in an interface can never be private protected transient or volatile whereas, there is no restriction to variables in an abstract class.
  7. The variable of an interface must be initialized during declaration. On the other hand, the variables in an abstract class can be initialized at any time.
  8. Inside an interface, an instance or static block can’t be declared but, you can declare instance or static block inside an abstract class.
  9. You can not define constructor inside an interface whereas, you can define constructor inside an abstract class.

Conclusion:

When you need to create a base class which contains a generalised form of methods that can be implemented by the derived classes according to their need, the concept of interface and abstract class helps in doing so.

No comments: