Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

What is an Interface in Java? How it’s used? Java Tutorial Example Attached

$
0
0

Java Interface Example by Crunchify What is an Interface in Java? How its used? Java Tutorial Example Attached

I believe this is the 1st question you might expect in Java Interview. Very basic questions but widely used in interview :). There is no perfect answer for this and there are number of ways to answer this question. Might be your interviewer looking for practical approach of this questions? Possible. Then let’s start with basic definition and then we will go over the same with multiple examples.

  • What Is an Interface?
  • What is an interface in java with realtime example?
  • Why use an interface in java
  • Interface Design Java
  • Most common interview questions on Interface

Interface

  1. An interface is just a contract, a description of the behavior an implementing class will have. The implementing class ensures, that it will have these methods that can be used on it. It is basically a contract or a promise the class has to make.
  2. What if in your projects all of the various implementations share the same method signatures? Interface works best in that case.
  3. At the later stage after major project implementation let’s see if you have implemented interface definitions to ~50 places, what if you change interface? You have to make modification to all 50 places in your projects.
  4. It’s advisable to spend some more time defining Interface during design phase rather change it at later stage icon smile What is an Interface in Java? How its used? Java Tutorial Example Attached
  5. An interface is consist of singleton variables (public static final) and public abstract methods. We normally prefer interface in real time when we know what to do but don’t know how to do. An interface cannot contain instance fields.
  6. The classes which implement the Interface must provide the method definition for all the methods present.
  7. A Class may implement several interfaces.
  8. An interface implementation may be added to any existing third party class.
  9. An interface can contain any number of methods.
  10. In Java you cannot instantiate an interface.
  11. An Interface does not contain any constructors.
  12. An interface is not extended by a class; it is implemented by a class.
  13. An interface can extend multiple interfaces.

Interface Examples:

Tip 1.

Create Interface CrunchifyDatabaseInterface.java

package com.crunchify.tutorial;

import java.util.Map;
import java.util.UUID;

/**
 * @author Crunchify.com
 * 
 */

public interface CrunchifyDatabaseInterface {

	// Basic Database CRUD Operations
	// Insert
	boolean insertCrunchifyRecord(UUID id, String name, String address, String phone, String zip, String comments);

	// Delete
	public boolean deleteCrunchifyRecord(UUID id);

	// Get
	public Map<String, String> getListOfAllCrunchifyRecords();

	// Update
	boolean updateCrunchifyRecord(UUID id, Map<String, String> records);
}

Tip 2.

Implement Interface CrunchifyDatabaseOracleImpl.java

When you 1st implements an interface, Eclipse will show you add unimplemented methods.

Java Interface Add unimplemented methods What is an Interface in Java? How its used? Java Tutorial Example Attached

Just click on  “Add unimplemented methods” and your IMPL class should be ready with Auto-generated method stub.

Tip 3.

Actual Impl Method.

package com.crunchify.tutorial;

import java.util.Map;
import java.util.UUID;

/**
 * @author Crunchify.com
 * 
 */

public class CrunchifyDatabaseOracleImpl implements CrunchifyDatabaseInterface {

	@Override
	public boolean insertCrunchifyRecord(UUID id, String name, String address, String phone, String zip, String comments) {
		// TODO Provide your actual implementation here based on your need specific to Oracle
		return false;
	}

	@Override
	public boolean deleteCrunchifyRecord(UUID id) {
		// TODO Provide your actual implementation here based on your need specific to Oracle
		return false;
	}

	@Override
	public Map<String, String> getListOfAllCrunchifyRecords() {
		// TODO Provide your actual implementation here based on your need specific to Oracle
		return null;
	}

	@Override
	public boolean updateCrunchifyRecord(UUID id, Map<String, String> records) {
		// TODO Provide your actual implementation here based on your need specific to Oracle
		return false;
	}
}

Tip 4.

Similar way you could use the same Interface to implement different Database specific operations. Like, for DB2, MySQL, MongoDB, Cassandra DB, etc.

What’s next?

In process of writing “Tutorial on Abstract Class” and then another tutorial which clearly shows “Difference between Abstract Class and Interface“.

Have anything to add to this article? Please chime in and join the conversion.


The post What is an Interface in Java? How it’s used? Java Tutorial Example Attached appeared first on Crunchify.
Author: Arpit Shah


Viewing all articles
Browse latest Browse all 1037

Trending Articles