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”. Interface 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:
- access_specifier <strong>interface</strong> interface_name {
- return-type method-name1(parameter-list);
- return-type method-name2(parameter-list);
- type final-varname1 = value;
- type final-varname2 = value;
- // ...
- return-type method-nameN(parameter-list);
- type final-varnameN = value;
- }
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:
- class class_name implements Interface_name{
- // class-body
- }
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#:
- class class_name : interface_name {
- // class-body
- }
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:
- abstract class class_name{
- abstract method_name1( );
- abstract method_name2( );
- :
- return_type method_name3( parameter_list){
- //concrete method
- }
- return_type method_name4( parameter_list){
- //concrete method
- }
- };
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#
- 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.
- An interface has all its methods abstract but, an abstract class has some abstract methods and some concrete methods.
- 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.
- 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.
- 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.
- Variables in an interface can never be private protected transient or volatile whereas, there is no restriction to variables in an abstract class.
- 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.
- Inside an interface, an instance or static block can’t be declared but, you can declare instance or static block inside an abstract class.
- 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.
| |