Reading:  

Quick introduction to Object oriented concepts in Java - Part 2 of series


Working with Interfaces

Interface is similar to class or it a blueprint of a class.by using interface we can achieve full abstraction.

Why Interface?

  • To achieve fully abstraction
  • It supports the functionality of multiple inheritance

The "interface" is a keyword used to create interface.

Syntax:

interface interfaceName {
	//Statements
}

Points about interface:

  • Interface allows only static and final variables by default all the variables are static and final.
  • Interface allows only abstraction methods by default all the methods are public and abstract in interface.
  • Since in interface all methods are abstract, interface is 100% abstract.
  • Interface cannot be instantiated.
  • An interface can extend number of interfaces.
  • To inherit members from interface to class use a ketwory “implements”

 

Class implements interface:

When a class implements interface, in sub-class all methods should be implemented otherwise sub-class become abstract.

Example:

Creating an interface: 

public interface First_Interface {
	void test1();
}

 

Creating a class which implements the interface:

public class A implements First_Interface{
	public void test1()
	{
		System.out.println("test1() in class A");
	}
} 

 

Access implemented interface method:

public class Run {
	public static void main(String[] args) {
		System.out.println("main starts");
		A a1 = new A();
		a1.test1();
	}
}
 

Output

Extending Interfaces:

To extend an interface by an interface we use "extends" keyword.

Example:

Create an interface:

interface Second_Interface {
            void test2();
}

 

Creating an interface and also extending an interface:

interface Third_Interface extends Second_Interface {
	void test3();
}

Class implementing a interface:

class B implements Third_Interface {
	public void test2() {
		System.out.println("test2() inside class B");
	}
	public void test3() {
		System.out.println("test3() inside class B");
	}
}

Class to create object referencefor class B:

public class Run2 {
	public static void main(String[] args) {
		B b1 = new B();
		b1.test2();
		b1.test3();
	}
}
 

Output

Extending Multiple Interfaces:

C++ supports multiple inheritance

Java doen’t support multiple inheritance using classes, java class can extend only one class, but by using interface in Java multiple inheritance can be done.

As usual extends keyword is used once and interfaces declared are separated by comma.

Example:

interface Third_Interface extends Second_Interface, First_Interface  {
	void test3();
}

Description

This tutorial will introduce you to Object oriented concepts in Java. Basic definitions and learning with code examples.

This tutorial has subdivided into 7 parts as listed below

  1. Understanding Inheritance    
  2. Understanding Overrides
  3. Understanding Polymorphism
  4. Understanding Abstraction
  5. Understanding Encapsulation
  6. Understanding Interfaces
  7. Understanding Packages

Let us know if you found any error in this tutorial. Your feedback is welcome as always.



Audience

Students seeking a quick overview on Java Programming language and its Object oriented features.

Learning Objectives

Learn Object Oriented concepts in Java language.

Author: Subject Coach
Added on: 10th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!