Skip to main content

Q) What is the real time usage of interface?




A)
->An interface specifies the contract between service provider and service utilizer objects.
->Implementation class of the interface carries out the contract.
->An interface acts as service specification of an object. I.e. what service methods should provided by an object are specified in interface.
->for a class, if required functionality is not provided by its class hierarchy , it goes for interface hierarchy for that functional specification.

Ex:
Class restaurant           interface menu                 class customer
{                                    {                                       {
                                           S1();
                                           S2();
}                                     }                                       }
(service provider)         (service specifier )            (service utilizer)

->via interface only the provider provides to utilize with the agreement. That agreement is specified in interface but implementation in service provider. Whenever provider doesn’t provide good services then the utilize change the provider but the utilizer doesn’t change the specification/contract. I.e. interface.

Ex:
interfaceDriver
{
   Cnnection connect();
}//sun.
->Any class is implement this Driver interface that is act as Driver in the world. This Driver interface created by the “sun”.
->services means methods, methods are called on the object to provide the task/service.
->any object which is called fan should provide revolve() stop() services then only that.

interface fan
{
   revolve();
   stop();
}
class OracleDriver implements Driver
{
       connection connect()
       {
             
       }
}
->Here oracle vendor must implementing Driver to get the eligible to act as driver, Otherwise the java program doesn’t recognize as DrNW. So the Oracle Documentation must implement connection() method.

class SQLservletDriver implements Driver
{
       Connect connect()
       {
             
       }
}
->specification in done by sun but implementation is decided by vendors its own.
->if any vendor provide bad functionality but its mane specification is same as interface. But sun can’t ask that vendor but the customer doesn’t purchase that Driver that’s why the implementation is must good by vendor.

Ex:
Class A extends B implements Runnable , xyz
{

}

->whenever a class is not given required functionality. If it wants more functionality it wants another class services. But in java multiple inheritance is not allowed. So that’s why interface can be implementing by the required class. Extends can’t allow 2 classes extending but one are more interfaces can implement by one class.
->to provide loose coupling between different parts (partitions) of an application interfaces are used.

Loose coupling:
Establish the 2 connections via using interfaces, if one class is used another class services via tight coupling (i.e. direct connection) , if processing part is bad when we change backend process then we change the user program. But it is not useful. That’s why by using interface we need not change user program but we can change backend process when we not like the backend process.
 

->ambiguity in reusability is there in the multiple inheritance that’s why it’s not allowed in java by james goslin.
->With interfaces there is no coming ambiguity because

1) The behavioral ambiguity is not there because the implementation is there in its child class.
2) The structural ambiguity is not there, because the inheritance variables are constants.

->so that’s why with multiple inheritance implementation we need not face ambiguity.
->if we want that interface variables with the interface name and “.” Operation that interface variable can access by children class.

interface I1
{
       int marks=100;
}
interface I2
{
       int cla=12;
}
class test2 implements I1,I2
{
       public static void main(String args[])
       {
              int c,b;

       c=I1.marks+10;
       b=I2.cla;
       System.out.println("marks :"+c);
       System.out.println("class :"+b);
       }
}

Output:
marks :110
class :12

Comments

Popular posts from this blog

Q) Explain repeated deletion of records.

A) //RepeatedDeletion.java import java.util.Scanner; import java.sql.*; class RepeatedDeletion {        public static void main(String[] args) {               Connection con= DriverManager. getConnection ( "jdbc:odbc:prince" , "scott" , "tiger" );               PreparedStatement ps=con.PreparedStatement( "DELETE FROM ACCOUNT WHERE ACCNO=?" );               Scanner s= new Scanner(System. in );               while ( true )               {                      System. out .println( "Enter accno : " );      ...

Introduction

1) What is not JDBC? 2) What is the purpose of JDBC? 3) What is ODBC? 4) What is JDBC? 5) What is JDBC architecture? 6) What is JDBC client? What are its responsibilities? 7) What is JDBC API?  8) What is driver manager? 9) What is JDBC driver? What is its role in java database connection? 10) What is database server?

Q) Explain about nested classes?

A) ->defining a class with in another class is known as nesting of the class. Ex: class A {        class B        {                      } //nested class, and inner class.because it is not static class. } //enclosing class or outer class. ->static class is declared in inside an another class then it does not become an inner class. Note: non-static nested class is known as “inner class”. In the above example class B is nested but not an inner class. Ex: class D {        static c        {                      } //nested class, but not an inner class , because of static class. } //outer class. ->we have three different kinds of inner classes. 1) Member inner class 2) Meth...