Introduction

Other Object Oriented Programming languages, such as C++, provide multiple inheritance (deriving a single new class from two or more base classes). But such type of inheritance leads in ambiguity when two base classes have the methods with the same name. Fortunately Java does not support multiple inheritance. But it can not overlook the benefit of multiple inheritance. Therefore in order to enjoy the benefits of multiple inheritance, Java provides interfaces. An interface forces the classes to implement certain methods with certain signatures. Actually an interface takes the abstract concept one step ahead.

Interfaces

Interfaces are used to define a behavior of a class. That is, using interface you can specify what a class must do, but not how it does. Once an interface is defined you can implement any number of classes from this interface. Also one class can implement any number of interfaces.

Syntax

An interface is defined like a class, but using a keyword interface rather than class as:

public interface InterfaceName

{

// ….

}

Here the access specifier is used either public or not used. An interface contains constant values (final variables) and methods declarations. The difference between interface and classes is that the methods in an interface are only declared and not implemented, that is, the methods do not have a body. In other words there can be no default implementation of any method specified within an interface.

Example

public interface BankEmployee{
static final int count = 10;

void setdata();

void display();

}

Since the methods of an interface have to be overridden by the classes that implement it, therefore they can not be declared final. The data members can be public, static or final. Here public ensures that it can be accessed in unrelated classes; static ensures that you can not create an object of the interface and final ensures that the value is not changed in the classes that implement the interface.

Implementing Interfaces

Once an interface has been defined, one or more classes can implement that interface. The keyword implements is used in order to implement an interface by a class.

Syntax

The keyword implements is used in order to implement an interface by a class.

class ClassName implements InterfaceName

{

// ….

}

Each class that includes an interface must implement all of the methods.

Example

class Manager implements BankEmployee{
// ….

void setdata(){

// ….

}

void display(){

// ….

}

}

Extending Interfaces

Like classes, interfaces can also be extended. It means that an interface can be subinterfaced from other interfaces. The new subinterface will inherit all the members of the super interface.

Syntax

A new interface is inherited from the old interface using the keywords – extends, as:

interface SubInterfaceName implements SuperInterfaceName{

// ….

}

Here one should remember that when a class implements that inherits another interface, it must provide implementation for all the methods defined within the interface chain.

Program ExtendInterfaceDemo.java illustrates this concept.

// Program – ExtendInterfaceDemo.java

interface first{

void show1();

}

interface second extends first{

void show2();

}

class third implements second{

public void show1(){                // Implementing first interface method declaration

System.out.println("First interface method");

}

public void show2(){                // Implementing second interface method declaration

System.out.println("Second interface method");

}

}

class ExtendInterfaceDemo{

public static void main(String args[]){

third t = new third();

t.show1();

t.show2();

}

}

The output of this program is….

First interface method

Second interface method

Grouping Interfaces (Multiple Inheritance)

As stated earlier, Java does not directly supports multiple inheritance. But being a pure Object Oriented Programming Language, it can not forget the lovely taste of inheritance. Java enjoys the benefits of multiple inheritance by grouping several interfaces, which are separated by commas, into one interface.

Syntax

interface InterfaceName1{

// ….

}

interface InterfaceName2{

// ….

}

interface InterfaceName3 extends  InterfaceName1, InterfaceName2{

// ….

}

Remember that the subinterfaces are too interfaces therefore you can not define the methods declared in super interfaces. When we implement a class from a subinterface then it is the duty of this class to define all the methods of superinterfaces as well as subinterfaces.

Program GroupInterfaceDemo.java illustrates this.

// Program – GroupInterfaceDemo.java

interface first{

void show1();

}

interface second{

void show2();

}

interface third{

void show3();

}

class forth implements first, second, third{

public void show1(){                // Implementing first interface method declaration

System.out.println("First interface method");

}

public void show2(){                // Implementing second interface method declaration
System.out.println("Second interface method");

}

public void show3(){                // Implementing third interface method declaration
System.out.println("Third interface method");

}

}

class GroupInterfaceDemo{

public static void main(String args[]){

forth f = new forth();

f.show1();

f.show2();

f.show3();

}

}


The output of this program is….

First interface method

Second interface method

Third interface method

Interfaces Verses Abstract Classes

  • An interface looks like an abstract class, but there are some differences between these two as shown below:
  • An abstract class is an incomplete class that requires further specialization. An interface is just a specialization or prescription for the behavior.
  • A class can implements several interfaces at once, whereas a class can extend only one parent class.
  • An abstract class is generally used where you want to initiate a hierarchy of ore specialized classes. On the other hand, an interface is used where you say – “I need to be able to call methods with these signatures in your classes.”

Packages

Like an ordered library provides books, a package provides organizes related classes into groups. In other words a package is a collection of classes and interfaces of similar nature. Two packages may have classes of same name. But within a package, each class has unique name.

Java provides two types of packages:

  1. Built-in Packages
  2. User-defined Packages

Before the discussion of these packages let us study the import statement.

The import Statement

Java Package, irrespective of built-in or user-defined, can be used in a program by using the import statement.

Syntax

The import statement is used as:

import PackageName.*;

// importing an entire package PackageName

import PackageName.ClassName;

// importing a class ClassName from package PackageName

Example

import java.awt.*;

import java.awt.Button;

Here a dot separates an element. This convention shows that a hierarchy has been followed while creating these built-in packages. The first statement imports all the classes or interfaces of the awt package. The second statement imports Button class from the awt package.

After importing the above package, you can create an object of the Button class as:

Button b = new Button(“Yes”);

However if you don’t want to import a package then you create an object of the Button class as:

java.awt.Button b = new java.awt.Button(“Yes”);

Naturally the first statement looks better than second.

  1. Built-in Packages
  2. User-defined Packages

User-defined Packages

You can create your own packages in order to organize your related classes. Such packages are known as user-defined packages.

Syntax

A user-defined package is created by writing the first statement in a Java program as:

package PackageName;

Here package is the reserved word and PackageName is the name of the user-defined package. You can make only one package declaration in a source Java program. After the package declaration all the classes declared within that file will belong to the specified package.

Example

package mypackage;
public class FirstClass{ // ….
}

Save this source file as FirstClass.java. When you compile this file – FirstClass.java, its class file – FirstClass.class is stored in the same directory with the name of the package. Now if you want to use the classes of this package then go to one level higher and then import a class or all classes of this package - mypackage.

Let your mypackage has the following path:
C:\>jdk\mypackage

And your FirstClass.java and FirstClass.class files are stored in C:\>jdk\mypackage\ then before using the classes of mypackage you must go to one level higher, say:

C:\>jdk

And then import the classes of mypackage, as shown below:

import mypackage.*;
public class FirstDemo{
public static void main(String args[]){
FirstClass f = new FirstClass()
}
}

S

ave this file as FirstDemo.java in C:\jdk\ directory and then compile and execute this file. If you do not go up one level higher and try to import the classes of mypackage, say you store the file FirstDemo.java in directory C:\>jdk\mypackage\ then you would certainly encounter the following error message:

“can’t find class FirstClass”


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet

Will Rajnikanth's support bring in Tamilians to the fast?

Tamilian were not very enthusiastic the last time. So will it be more popular this time?

No answer selected. Please try again.
Please select either existing option or enter your own, however not both.
Please select minimum 0 answer(s) and maximum 2 answer(s).
/polls/politics/6209-will-rajnikanths-support-bring-in-tamilians-to-the-fast.json?task=poll.vote
6209
radio
[{"id":"21410","title":"You can expect a lot of his fans at the fasting venues","votes":"70","type":"x","order":"0","pct":86.42,"resources":[]},{"id":"21411","title":"No, his fans will not care about his support","votes":"11","type":"x","order":"0","pct":13.58,"resources":[]}] ["#ff5b00","#4ac0f2","#b80028","#eef66c","#60bb22","#b96a9a","#62c2cc"] ["rgba(255,91,0,0.7)","rgba(74,192,242,0.7)","rgba(184,0,40,0.7)","rgba(238,246,108,0.7)","rgba(96,187,34,0.7)","rgba(185,106,154,0.7)","rgba(98,194,204,0.7)"] 350
bottom 200
No married couple wants to end up getting divorced. It is not like they have planned for it. They try to put up with their partners for as long as they
Due to our modern lifestyle, we feel that digestion related disorders are a common problem. Thus, we neither give importance to them nor seek any help
The bond of marriage brings the two people together. Initially, everything may seem okay and both of them slowly start discovering each other in the journey.
Why to choose Java? In the computer market, being able to use Java is a crucial knowledge. Java is a development terminology used to build compelling
DATABASE CONNECTIVITY This articles explains the need of database connectivity, the way it is achieved through Java using the JDBC(java database connectivity)
  Write your first simple java program.   First of all we need is a placewhere we can write your Java code. All Java programs are written using
Array of objects in Java Arrays are defined as block of continous memory locations which are arranged in one after another. Elements of the Array can
Synchronized block is alternative mechanism for "synchronized" methods.If we inherihate the non synchronized blocks either from base class or interface