Class and Interface | Difference between Class and Interface in java?

Class and interface in java are given below:

Class

  • Class is a basic unit in object oriented programming concept. Class is a collection of objects or a blue print of an object. It can be stored data part as well as method part. The class is declared by the use of class keyword. The class name should begin with a Capital letter.
       Syntax:
class Classname
{
datatype variable 1;
datatype variable 2;
.
.
datatype variable n;
datatype methodname1(parameter list)
{
//body of method;
}
datatype methodname2(parameter list)
{
//body of method;
}
.
.
datatype methodnamen(parameter list)
{
//body of method;
}
}
  • Characteristic of Class:

    • 1.  We can declare class as either public or default type.
    • 2. We can declare class as either private or protected type.
    • 3. We can not declare class as static type.
    • 4. We can declare class as abstract or final but combination of both ( means abstract final or final abstract ) are not supported.

    Interface

    Interface is a collection of method prototypers or method headers. Interfaces can be communicate with several sub or implemented classes.

  • Syntax:
  • upto jdk 1.7 version
interface interfacename
 
{
 
constants;
 
abstract methods;
 
}

 

  • upto jdk 1.8 version
    interface interfacename
    
    {
    
    constants;
    
    abstract methods;
    
    static methods;
    
    }

    upto jdk 1.9 version

    interface interfacename
     
    {
     
    constants;
     
    abstract methods;
     
    private methods;
     
    }

     

    The difference between Class and Interface in java are as follows..

    class and interface in java

    Class Interface
     1.Class begin with Class keyword.  1.Interface begin with interface keyword.
     2.Class support Concrete methods only.  2.Interface does not support Concrete methods.
     3.Class does not support Abstract method.  3.Interface only support abstract methods.
     4.We can declare class as either Abstract or Final.  4.We can declare Interface as Abstract type but does not support final modifier.
     5.Class support Variables.  5.Interface does not support Variables.
     6.Class support Constants, Constant must by started by final keyword.  6.Interface supports only Constants. By default it is considered as public, static, final type.
     7.Class support Constructor.  7.Interface does not support Constructor.
     8. We can create object for the class through object we can access entire properties of class.  8.Interface does not support Object creation but we can pass sub-class reference.
     9.We can create sub-classes. In the sub-classes we uses extend keyword.  9. We can create sub-classes for interface. In sub-classes we use implement keyword.
     10. Class does not support default methods.  10. Interface support default methods.
     11.Private method supported since jdk 1.0 version.  11.Private method supported since jdk 1.9 version.
     12. If all features are common, class is recommanded.  12.If no features are common, interface are recommanded.
     13.Class having very less security.  13.Interface having high security.
     14.By default class methods are default type.  14.By default interface methods are public abstract type.
     15.Static methods are supported since jdk 1.0 version.  15.Static methods are supported since jdk 1.8 version.
    Multiple inheritance is not supported. Multiple inheritance is supported.
    16.From several super classes we cannot create one sub class.  16.From several interfaces we can create one sub class.
    16.From several super classes we cannot create one sub class.  16.From several interfaces we can create one sub class.
    17. Syntax:

    class ClassName

    {

    //variables;

    //methods;

    }

     17. Syntax:

    interface InterfaceName

    {

    //constants;

    //abstract methods;

    //private methods;

    }

    • Read More: 

difference between class and object in java?

Leave a Comment