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.

Wednesday, 28 June 2017

The ADF Architecture


THE ORACLE ADF ARCHITECTURE
Oracle ADF is based on the Model-View-Controller (MVC) design pattern. An MVC application is separated into:
1) A model layer that handles interaction with data-sources and runs the business logic,
2) A view layer that handles the application user interface, and
3) A controller that manages the application flow and acts as the interface between the Model and the View layers.
Separating applications into these three layers simplifies maintenance and reuse of components across applications. The independence of each layer from the others results in a loosely coupled,
 Oracle ADF Architecture
Oracle ADF Architecture
Service Oriented Architecture (SOA).
Oracle ADF implements MVC and further separates the model layer from the business services to enable service-oriented development of applications.
The Oracle ADF architecture is based on four layers:
• The Business Services layer - provides access to data from various sources and handles business logic. (EJB, Web Services, ADF BC, .xml Bean definition file)
• The Model layer - provides an abstraction layer on top of the Business Services layer, enabling the View and Controller layers to work with different implementations of Business Services in a consistent way. (.cpx, .dcx, .xml)
• The Controller layer - provides a mechanism to control the flow of the Web application.
• The View layer - provides the user interface of the application. (VO files (__vo.xml), .java, .jsp, .jspx, .uix)
Oracle ADF lets developers choose the technology they prefer to use when implementing each of the layers. The diagram above shows the various options available for developers when building Oracle ADF applications. The glue that integrates the various components of Java EE applications and makes development so flexible is the Oracle ADF model layer. EJB, Web Services, JavaBeans, JPA/EclipseLink/TopLink objects and many others can all be used as Business Services for the Oracle ADF Model. View layers can include Web based interfaces implemented with JSF, Desktop Swing applications and MS Office front ends, as well as interfaces for mobile devices.
The Business Services Layer
The Business Services layer manages interaction with a data persistence layer. It provides such services as data persistence, object/relational mapping, transaction management, and business logic execution.
The Business Services layer in Oracle ADF can be implemented in any of the following options: As simple Java classes, EJB, Web services, JPA objects, and Oracle ADF Business Components (VO, EO & AM). In addition, data can be consumed directly from files (XML or CSV) as well as REST.
The Controller Layer
The controller layer manages the applications flow and handles user input. For example, when you click a Search button on a page, the controller determines what action to perform (do a search) and where to navigate to (the results page).
There are two controller options for web-based applications in JDeveloper: the standard JSF controller or the ADF Controller which extends the JSF controller functionality. Whichever controller you use, you will typically design your application flow by laying out pages and navigation rules on a diagram.
With the ADF controller you can break your application's flow into smaller, reusable task flows; include non-visual components such as method calls and decision points in your flow; and create "page fragment" flows that run inside a region of a single containing page. This approach encourages maximum reusability for user interface fragments and simplified integration into portals and smashup applications.
The View Layer
The View layer represents the user interface of the application.
Oracle ADF support multi-channel access to your business services allowing you to reuse your business services and access them from a Web client, a client-server swing desktop based application, Microsoft Excel spreadsheets, or a mobile devices such as a smart-phone.
For Web based interface Oracle ADF offers a rich set of over a 150 Ajax enabled JSF components that simplified the creation of dynamic and appealing user interfaces.
The Model Layer
The model layer connects the business services to the objects that use them in the other layers. Oracle ADF provides a model layer implementation that sits on top of business services, providing a single interface that can be used to access any type of business service. The model layer consists of two components, data controls and data bindings, which utilize metadata files to define the interface. Data controls abstract the business service implementation details from clients. Data bindings expose data control methods and attributes to UI components, providing a clean separation of the view and model. Due to the metadata architecture of the model layer, developers get the same development experience when binding any type of Business Service layer implementation to the View and Controller layers.
The client binding description file (.cpx) references the binding definitions in the client binding container definition files (.xml). The .cpx file also contains a reference to the data control description file (.dcx), which specifies the data control factory to use for a specific business service. 


Oracle ADF Request Life Cycle: