What are Classes and Interfaces in Java?

In this section we will understand what are classes and interfaces in Java programming language? When to use classes and when to use interfaces.

Ads

In this tutorial we are going to discuss about classes and interfaces in Java. These topics concepts concepts comes under the basics of Java programming language. Anyone learning Java programming language should learn classes and interfaces concepts in detail.

What are classes?

Java is Object oriented programming and classes are the main build blocks in Java. In object oriented programming language objects are center point and it used to design the program. Objects are the instance of Classes in Java programming language. Classes are the blueprint of the real world object in the programming language.

Classes contains the fields, methods, variables, constructors and objects to make it fully functional entity to perform some work.

For example you want to design a calculator then you have to create a class in Java and inside the class you will have the data and behavior of the calculator.

Following is an example of Calculator class:

public class Calculator 
{
	public int sum(int i, int j) 
	{
		return i+j;
	}
}

In the above code we have defined a class and created a method sum() for calculating the sum of the input parameters. An instance of the class are called objects and it is created in runtime by the JVM. The object of the class contains the data and various operations (in this case sum) are performed at runtime.

 

What are interfaces?

Interfaces in Java is also used to define the design of the objects in Java but it can't have the definition of the methods. For example we can define the Calculator class in following way:

public interface Calculator 
{
	public int sum(int i, int j);
}

To define an interface we have to use the keyword interface and method will only have the signature. We can't add the implementation in the interfaces in Java.

 

Related tutorials:

Advertisements

Ads
Share on Google+Share on Google+

What are Classes and Interfaces in Java?

Posted on: May 29, 2016 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: What are Classes and Interfaces in Java?  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads