Difference between Class & Interface Extend Implement Interface

Video Tutorials
Artificial Intelligence Tutorial
Machine Learning Projects TutorialPython Video tutorial
Computer Graphics Video TutorialC++ Video Tutorial
Big Data Analytics Video TutorialSoft Computing Video Tutorial
Placement Video TutorialJava Video Tutorial

In this article, I will cover,

What are Interfaces in Java?

Difference between Class & Interface.

How to Extend and Implement Java Interface?

Video Tutorial – Difference between Class & Interface Extend Implement Interface:

Definition of Java Interface:

An interface is similar to a class but there lies some difference between the two.

Difference between Class & Interface

The class is denoted by a keyword classThe interface is denoted by a keyword interface
The class contains data members and methods. But the methods are defined in class implementation. Thus class contains an executable code.The interfaces may contain data members and methods but the methods are not defined. The interface serves as an outline for the class.
By creating an instance of a class the class members can be accessed.You cannot create an instance of an interface.
The class can use various access specifiers like public, private or protected.The members of a class can be constant or final.
The interface makes use of only public access specifier.The members of interfaces are always declared as constant(i.e. final).

How to create Interfaces – Syntax

The interface can be defined using the following syntax

access_modifier interface name_of_interface 
    return_type method_name1(parameter1, parameter2, ... parametern); 
    return_type method_name2(parameter1, parameter2, ... parametern); 
    type static final variable_name=value; 

Extending Interfaces

Interfaces can be extended similarly to the classes.

That means we can derive subclasses from the main class using the keyword extends, similarly, we can derive the sub-interfaces from main interfaces by using the keyword extends.

The syntax extends interface is

interface Interface_name2 extends interface_name1 
	//Body of interface 

Example to demonstrate extends keyword:

Public interface A 
	final int val=10;
interface B extends A 
	void print_val() ;
class MyClass implements B
	 void print_val()
		Syste.out.println("The value is "+val)
public class Interfacedemo 
	public static void main(String args[]) 
		 MyClass obj = new MyClass();

That means in interface B the print_valmethod can access the value of variable val. Similarly, more than one interface can be extended.

How to implement Interface: implements keyword

It is necessary to create a class for every interface. The class must be defined in the following form while using the interface

class Class_name implements interface_name1,interface_name2,... 
     //body of class 

Let us learn how to use interface for a class

Step 1: Write the following code and save it as my_interface.java

public interface my_interface 
      void my_method(int val);

Do not compile this program. Simply save it.

Step 2: Write the following code in another file and save it using InterfaceDemo.java

class A implements my_interface 
	public void my_method(int i) 
		System.out.println("The value in class A: "+i);
	public void another_method() 
	//Defining another method not declared in interface 
		System.out.println("This is another method in class Al");
class InterfaceDemo 
	public static void main(String args[]) 
		my_interface obj=new A(); 
		//or A obj=new A() is also allowed 
		A objl=new AO; 

Step 3: Compile the program created in step 2 and get the following output

The value in class A: 100

This is another method in class A

Program Explanation:

In the above program, the interface my_interface declares only one method i.e. my_method. This method can be defined by class A. There is another method that is defined in class A and that is another_method. Note that this method is not declared in the interface my_interface. That means a class can define any additional method which is not declared in the interface.

This tutorial discusses, Difference between Class & Interface, How to use Extend Interface, and Implement the Interface. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *