Difference betwixt a degree as well as an interface inwards Java

It is 1 of the frequently asked Java questions from beginners which struggles to acquire the concept behind an interface. The principal divergence betwixt a class as well as an interface lies inwards their usage as well as capabilities. An interface is the purest cast of abstraction available inwards Java where yous only define the API or contract e.g. yous define run() method on the Runnable interface without worrying almost how something volition run, that is left to the implementor which volition job a class to define how just to run. So an interface gives yous method cite but how the deportment of that method is come upwards from the class which implements it. That's your full general divergence betwixt an interface as well as class inwards Java as well as applicable to all object oriented programming language, non only Java. Even though this inquiry is non just the difference betwixt abstract class as well as interface, it's somewhat related to it because an abstract class is cipher but a class amongst only about abstract method. The points I convey discussed there, likewise applicable hither inwards damage of rules of Java programming related to class as well as interface.



Difference betwixt class as well as interface inwards Java

Now, let's bear witness both syntactical as well as functional divergence betwixt a class as well as interface inwards Java betoken past times point, this volition give yous a ameliorate thought of both of them.

1) Purpose
The interface is best suited for defining type because it promotes multiple inheritances inwards Java. It is fifty-fifty recommended past times Joshua Bloch inwards Effective Java. Since an interface tin extend multiple interfaces as well as a class tin implement multiple interfaces, it all of a precipitous becomes to a greater extent than polymorphic. On the other hand, a class is best suited for writing concrete code which does the actual job.



2) Instantiation
Since an interface is completely abstract yous cannot practise an object of them but a class tin endure both abstract as well as concrete so yous tin practise an object of a concrete class inwards Java.

For example
Runnable r = new Runnable(); // compiler mistake  but Runnable r = new Task();; // Ok

where Task is a class which implements Runnable interface.


3) State
An interface tin define constants but that is non component division of the dry reason of an object, which is alone defined past times the event variable. You cannot define dry reason variable within interface inwards Java until JDK seven but that is changed straightaway inwards Java SE 8, where yous tin define both static as well as default methods within interface. (see Java SE 8 for Really Impatient past times Cay S. Horstmann to larn to a greater extent than almost static as well as default methods)


4) Inheritance
H5N1 class tin alone inherit 1 class but an interface tin extend multiple interfaces. Similarly, a class tin implement multiple interfaces but an interface cannot extend a class. This powerfulness appear confusing to yous but Java allow multiple inheritance inwards cast of 1 interface extending multiple interface but doesn't allow C++ agency multiple inheritance where a class tin extend multiple classes.


5) Flexibility
Interface adds flexibility inwards coding. Any code which is written using interface are to a greater extent than adaptable to a modify than code written using concrete classes. This is likewise a pop object oriented pattern principle, unremarkably known every bit "programming for interfaces than implementation". You tin come across my postal service 10 object oriented pattern principles which every Java developer should know to larn to a greater extent than almost that.


6) UML Diagram
In UML diagram an interface is represented using keyword <<interface>> piece class doesn't demand anyword. Any class which impelments interface is likewise denoted via dotted delineate amongst arrowhead every bit shown inwards next diagram. In this diagram yous convey a class called Circle which implements an interface called GeometricObject. There is only about other class called ResizableCircle which extends Circle class as well as implement only about other interface called Resizable. If yous are non real familiar amongst UML than yous should read UML for Java Programmers past times Uncle Bob to larn to a greater extent than almost UML inwards Java)

 from beginners which struggles to acquire the concept behind an interface Difference betwixt a class as well as an interface inwards Java



When to job a class as well as interface inwards Java?

This is the most of import inquiry yous should ask, this is fifty-fifty to a greater extent than of import than the difference betwixt class as well as interface but both are related to each other. If a candidate understands the class as well as interface concept correctly he tin respond both of these questions. If yous are writing a trading application, yous tin define an Instrument interface as well as all type of musical instrument e.g. Stock, Future, Option will endure the classes which implement Instruments. Any code, which needs an Instrument e.g. Order demand an Instrument as well as then yous tin code amongst Instrument as well as it volition piece of work for whatever Instrument including Stock, Future, as well as Options every bit shown below:

public class ClassVsInterface {    public static void main(String[] args) {      Instrument[] instruments = new Instrument[3];     instruments[0] = new Stock();     instruments[1] = new Future();     instruments[2] = new Option();      for (Instrument i : instruments) {       print(i);     }    }    public static void print(Instrument instrument) {     System.out.println(instrument.getAssetClass());   } }  interface Instrument {   public String getAssetClass(); }  class Stock implements Instrument {    @Override   public String getAssetClass() {     return "STOCK";   }  }  class Future implements Instrument {    @Override   public String getAssetClass() {     return "FUTURES";   }  }  class Option implements Instrument {   @Override   public String getAssetClass() {     return "OPTION";   } }

You tin come across that since nosotros convey defined Instrument every bit interface the print() method tin piece of work amongst Stock, Future, as well as Option, that's the ability of Inheritance as well as Polymorphism which comes past times using interface.

That's all almost the difference betwixt a class as well as interface inwards Java. The most of import divergence is to sympathise when to job a class as well as interface. As I convey previously explained on actual job of interface inwards Java, it allow yous write polymorphic code which adds flexibility to your software. This is likewise known every bit "programming for interfaces" inwards the object-oriented world.

Related Java as well as OOP tutorials yous may like:
  • Difference betwixt abstraction as well as encapsulation inwards Java? (answer)
  • 30 OOP questions from Java Interviews amongst answrs (list)
  • Why abstract class is of import inwards Java? (answer)
  • 21 Java Inheritance Interview Questions amongst answers (list)
  • Differnece betwixt abstraction as well as polymorphism inwards Java? (answer)
  • Can nosotros declare constructor within abstract class inwards Java? (answer)

Further Reading
SOLID Principles of Object Oriented Design
Design Patterns Library
Java Fundamentals: The Java Language


Subscribe to receive free email updates:

0 Response to "Difference betwixt a degree as well as an interface inwards Java"

Posting Komentar