Novice Info: Difference between Classes, Interfaces in Java

Everyone of us, before writing a java program will create a Class and then add some methods to it. Before starting the conversation and going into the details of the differences between Classes and Interfaces, let us see the meaning, constructs / syntax of the Classes and Interfaces.

Class : A Java class is a virtual construct in Programming world, which can be instantiated to create Logical object to represent a Physical entity in the Virtual / Programming environment.
Every Object instantiated from a Class will have a state, and a behavior. These objects will communicate with other Objects in their virtual world using this behavior. We will take a deep dive about this communication between the objects in a short while.

Class Definition & Syntax:

   1: public class Vehicle { // syntax of class

   2: int steering; // these are the instance variables
   3: int wheels;
   4:
   5: Vehicle(int wheelCpunt){ // Is a constructor of the class
   6: wheels = wheelCount;
   7: }
   8: int getWheels(){
   9: return wheels;
  10: }
  11: }

In the above code snippet, public is a modifier and class keyword specifies that the Vehicle is a type of Class, and the second and third lines are having two variables, these are called instance variables and they hold the state of an object when instantiated.

The fifth line contains a Constructor for the class which will be called when an object is instantiated for the vehicle, and sets the state of the object by giving them the passed values or default values. In the above snippet when an Object of Vehicle is created, the state of wheels field is set by the parameter to the constructor, and the steering is given a default value which is 0 for an int.

There are 2 other types of classes, Inner Class and anonymous class, I will explain about them in the forth coming blog.

Interface : A Java Interface is also a virtual construct in programming world, but in contrary to the Java Class, Interface can not be instantiated or created as an object. An Interface denotes a group of logical entities. It can also act as a contract between two sub systems, while communicating with each other.

 
   1: public interface Vehicle{ // syntax of class

   2: int steeringCount = 2;
   3: int getWheels();
   4: }

 
The above code snippet, defines an interface called Vehicle. An interface will not have a constructor and cannot be instantiated or an object cannot be created for this type. The variables in an Interface are called Class variables / Static variables, since they cannot have instances they should be able to communicate using the Class Name itself. In addition to being a static variables these are also final by default which makes them not to be altered by any other objects.
 
Some of the differences between a class and an Interface
 
Property Class Interface

Instantiation

Can Be Instantiated

Can not be instantiated

State

Each Object created will have its own state

Each objected created after implementing will have the same state

Behavior

Every Object will have the same behavior unless overridden.

Every Object will have a define its own behavior by implementing the contract defined.

Inheritance

A Class can inherit only one Class and can implement many interfaces

An Interface cannot inherit any classes while it can implement many interfaces

Variables

All the variables are instance by default unless otherwise specified

All the variables are static final by default, and a value needs to be assigned at the time of definition

Methods

All the methods should be having a definition unless decorated with an abstract keyword

All the methods are abstract by default and they will not have a definition.

I hope this article was useful, and you did not find the time spent on this as useless. Please do share your comments and suggestions from the comments section.

Advertisements

About Sharath

I am a passionate technologist, I have varied interests in Development, Testing and Maintenance phases of PDLC / SDLC. In my leisure I love to play with my daughter. I love eating out, hanging out with friends and reading blogs/articles.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s