Saturday, 2 December 2017

Java-9-private-method-in-interfaces


Interface

An interface is a reference type in java that means you can not create an object of an interface like abstract class. You can only use interface reference variable to hold the object of child class.
You can also say an interface is a blueprint of a class that means it tells a class what should it do? But not how should it do?
Suppose you want to open a bank in India. Then what is the first thing that you have to do to open a bank? You have to take permission from Reserve Bank Of India (RBI). RBI will tell you about all the basic functionality that your bank should have like check balance, withdraw money,transfer money etc.

But does not tell you about the implementation of those functionality.

Points to Remember:-

1. An interface can have only public and abstract methods. By default all the methods of an interface are public and abstract.  (up to java 7)

2. You can not provide body of an abstract method. 

3. An interface can have only static constants. That means all the data member of an interface is public static and final by default. 

Need of An Interface:-

1. It is used to achieve the abstraction in java.
2. It is used to achieve the multiple inheritance in java.

Example:-


interface My
{
   int a;               // by default public static and final
   void show();        // by default public and abstract

}

class Temp implements My
{
    public void show()
{
    System.out.println("show method");

}

}




If any class implements an interface then it must have to give implementation of all the abstract methods of an interface otherwise it will be a compilation error.


Multiple Inheritance in Java:-

java does not support multiple inheritance. But with the help of interfaces you can achieve multiple inheritance in java.
An interface can extends from any number of interfaces and a class can implements any number of interfaces.

Example:-


interface My
{
 void show();  // by default public and abstract
}

interafce My1
{
 void show();   // by default public and abstract
}

interface Demo extends My,My1
{
 void display();   // by default public and abstract
}

Class Implementing Multiple Interfaces:-


interface My
{
 void show();  // by default public and abstract
}

interafce My1
{
 void show();   // by default public and abstract
}

class Temp implements My,My1
{
 void show()
     {
           System.out.println("show method");
      }

      public static void main(String ar[])
    {
         Temp t=new Temp();
          t.show();
    }
}

Multiple Inheritance Is Not Supported With Classes. Why?

You can not achieve multiple inheritance with the help of classes in java. A class can not be extended from multiple classes.
This is because of ambiguity or diamond problem.

Lets Understand this With The Help Of An Example:-

I am assuming here that multiple inheritance is possible with the help of classes in java. Then we will discuss about the problems that will arise.


class Temp
{
 void show()
 {
  System.out.println("show method from Temp class");
 }
}

class Temp1
{
 void show()
 {
  System.out.println("show method from Temp1 class");
 }
}

Class Demo extends Temp,Temp1
{
 // both the methods will be inherited

 /* 
 
 void show()
 {
  System.out.println("show method from Temp class");
 }
 
 */
 
 /* 
 
 void show()
 {
  System.out.println("show method from Temp1 class");
 }
 
 */
 
 public static void main(String ar[])
 {
  Demo d=new Demo();
  d.show();             // Error will be generated
 }

}


As you can see in above example that the show() method of both Temp and Temp1 will be inherited in Demo class. When we will call show() method from Demo object then compiler will generate an error. Because both methods are identical the compiler is confused and does not know which method it has to call that is why it will generate an error which is ambiguity error.

Interface Inheritance:-


interface My
{
 void show();
}

interface My1 extends My
{
 void display();
}

class Temp implements My1
{
 public void show()
 {
  System.out.println("show method");
 }
 
 public void display()
 {
  System.out.println("display method");
 }
 
 public static void main(String ar[])
 {
  Temp t=new Temp();
  t.show();
  t.display();
 }
}

Java 8 Features

There are two new features introduced in interfaces in java 8.

1. Default Methods

2. Static methods

Until java 7 you can not declare default and static method in your interface.

Default Methods:-

default  is the keyword given by java to declare default methods in an interface. Until java 7 you can not declare method with body (non-abstract method) in an interface. But in java 8 you can have method with body in your interface with the help of default keyword.

Example:-


interface My
{
 default void show()
 {
  System.out.println("show method");
 }
}

class Temp implements My
{
 public static void main(String ar[])
 {
  Temp t=new Temp();
  t.show();
 }
}

Need Of Default Methods:-

Suppose you have created your software using java 6 version. Let you have used an interface My in your software which is an built interface of java in which there is 5 methods.
In java 7 version java wants to update that interface and wants to add 2 more methods in that interface. You have updated your java software to java 7 version. In java 7 version there are 7 methods in My interface but you have given body of only 5 methods in your software and your software will give you an error and ask you to give body of remaining two methods.
To solve this problem java has introduced default method concept in java 8. So that java can add new features in the technology and when you update your software you does not get any error also.

Note:- You cannot make default method as static.

Static Methods In Interface:-

Static methods are the utility methods in java that means these method is shared by the all the objects of an class / interface. Static methods are common for all the objects of a class.
You can also give body of a method with the help of static methods in java. Untill java 7 you can not declare static methods in an interface.

Example:-


interface My
{
 static int cube(int a)
 {
  return(a*a*a);
 }
}

class Temp implements My
{
 public static void main(String ar[])
 {
  int b=My.cube(5);
  System.out.println(b);
 }
}


Note:- You can not declare a default method as static.

Java 9 Features

Private Method In Interface:-

You can declare private method in your interface from java 9 on wards.

Example:-


interface Demo
{
 default void call()
 {
  fetchRecord();
 }
 
 
 private void fetchRecord()
 {
  // fetching record from database
 }
}

class Test implements Demo
{
 public static void main(String ag[])
 {
  Test t=new Test();
  t.call();
 }
}

Need Of Private Method:-

In the above program there is two methods in Demo interface. default method call() will be inherited in Test class automatically. But inside call method we have used fetchRecord() method which will fetch the record from database whose code we do not want to expose in the child class.
There was no way  to hide your internal code in interfaces before. Private methods are introduced to hide your internal code in java 9.

No comments:

Post a Comment