تفاوت بین Abstract class و اینترفیس در جاوا

تفاوت interface و Abstract class در جاوا به شرح زیر می باشد:

Abstract ClassInterface
contains zero or more abstract methodsinterface contains all abstract methods
Abstract class can contain variableinterface contains only constant
Abstract class contains constructorinterface does not have any constructor
extend keyword is used for abstract class for provide body of abstract methods Implement keyword is used to provide methods of interface

چند نکته در مورد Abstract class در جاوا

  • Abstract class contains zero or more abstract methods.
  • the class that want to provide the definition of the abstract method present in abstract class, has to extend the abstract class.
  • the abstract keyword can be used only before a method and before a class.
  • we can create reference variable of the abstract class but we can not create
    an object of the abstract class using "new" keyword. it is possible using the
    concept of sub class and super class.
  • abstract methods by default are public and no need for public word.


چند نکته در مورد اینترفیس در جاوا

  • directly JVM knows that the method of the interface is abstract and it is  no need to write abstract before the method in the interface.
  • we can create reference variable of an interface, but we can not  create the object of an interface using a new keyword.
  • interface can not contain constant. They are "static final" by default.
  • one interface also can Extend other interfaces.
  • to the reference of an interface we can store the object of the class that implements the interface.
  • also we can store the reference variable of the interface to the reference
    variable of the class by using of type casting concepts.
  • interface dose not have constructor==> we can not create object of it.


اینترفیس و Abstract در جاوا

  • new keyword is not applicable before interface
  • Abstract methods are methods without body
  • Abstract method contains only signature(return type, name of the method and arguments) example: abstract public void meth(int x , int y);
  • Interface is a fully unimplemented structure  supported by java.
  • Interface contains all abstract methods.
  • we are using keyword Interface to declare a interface.
  • the class who wants to provide the body, for these abstract methods those class has to implement the interface using implements keyword and that class has to provide the body for all those abstract methods.
  • JVM creates the .class file according to the structures presented in the program.

class، Interface و abstract class در جاوا

java supports 3 types of structures as follows:

  • fully implemented that is class
  • fully unimplemented that is interface
  • partly implemented and unimplemented that is Abstract class