difference between interface and class in java code example

Example 1: difference between class and object in java

Class is a blueprint or template which
you can create as many objects 
as you like. Object is a member or
instance of a class.
Class is declared using class keyword,
Object is created through new keyword
mainly. A class is a template for objects.
  A class defines object properties
 including a valid range of values, and
 a default value.
 A class also describes object behavior.
   An object is a member or 
 an "instance" of a class and has states
   and behaviors in which all of its
properties have values that you either
   explicitly define or that are defined 
   by default settings.

Example 2: difference between class and interface in java

A class can be instantiated by 
creating its objects.
An interface is
never instantiated as the methods 
declared inside an interface are abstract
and does not perform any action, 
  so there is no use of instantiating any
interface.
A class is declared using a keyword class. 
In the same way, an interface is 
created using a keyword interface.
The members of a class can have 
access modifier like public, private, protected.
But the members of an interface
are always public as they have to be accessed 
by the classes implementing them.
The methods inside a class are 
  defined to perform an action on the fields
declared in the class. 
The methods inside an interface are purely abstract.
A class can implement any number 
of interfaces but can extend only one 
super class.
An interface can extend any number
of interfaces but cannot implement any
interface.
A class has constructors defined inside
it to get the variable initialized.
But, an interface does not have any
constructors as there are no fields 
to be initialized. The fields of an
interface are initialized 
at the time of their declaration only.

Example 3: diff between class and interface

A class describes the attributes and behaviors of an object .
An interface contains behaviors that a class implements.

Tags:

Java Example