Thursday, February 2, 2012

Interface in Java


Introduction of Inheritence in Java
Inheritence is one of the concept of the Object- Oriented programming. It gives the idea of reusability. The advantage of using the inheritence is
  1. The reusability of the code.
  2. The helps to enhance the properties of the class.
The class from which a new class is derive is called base class or parent class and the class which is derive is called derive class or child class.
It allows you to define a general class, and later more specialized classes by simply adding some new details. The property of base class will automatically inherited to derive class if we inherited any derive class from base class.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_KUGxws64C46Ag8_iuX1W_gL-VF7aficZcetgfODTw1IiqpuSldb-MbAAibvY-2nPFspYv9XrZmAVcbl7HK_spBCKhQihD52fgMv5bpsHwvyhpRz_dBQGViDYt3DVH7i3MNAqpyDZO1U/s320/InheritanceTerms.gif
Java support three types of inheritance.
1) Simple  inheritence
2)Multilevel inheritance
3)Hierarchical inheritence


Forms of java inheritance
A) There are two forms of inheritence in the Java language. The standard form of inheritence is by extension; a class declares that it extends another class, or an interface extends another interface. In this case, the subclass or sub-interface inherits all the fields and methods of its parent.
B)The second special form of inheritence is where classes declare that they implement an interface, which has more limited consequences. When a class implements an interface, it inherits any fields from the parent as final constants, but must provide its own implementation of the interface methods.


Simple Inheritence
When a  subclass is derived simply from it's parent class then this mechanism is known as simple inheritence. In case of simple inheritence there is only a sub class and it's parent class. It is also called single inheritance or one level inheritence.     
Example Program
 class A {
  int x;
  int y;
  int get(int p, int q){
  x=p; y=q; return(0);
  }
  void Show(){
  System.out.println(x);
  }
}

class extends A{
  public static void main(String args[]){
  A a = new A();
  a.get(5,6);
  a.Show();
  }
  void display(){
  System.out.println("B");
  }
}

Multilevel Inheritence
It is the enhancement of the concept of inheritence. When a subclass is derived from a derived class then this mechanism is known as the multilevel inheritence. The derived class is called the subclass or child class for it's parent class and this parent class works as the child class for it's just above ( parent ) class.  Multilevel inheritence can go up to any number of level.
Example program

class A {
  int x;
  int y;
  int get(int p, int q){
  x=p; y=q; return(0);
  }
  void Show(){
  System.out.println(x);
  }
}
class extends A{
  void Showb(){
  System.out.println("B");
  }
}

class extends B{
  void display(){
  System.out.println("C");
  }
  public static void main(String args[]){
  A a = new A();
  a.get(5,6);
  a.Show();
  }
}

Hierarchical Inheritence
Here there is one base class and more than one derive class. The content of base class inherites into all derive class
Example of Hierarchical inheritance
  class one    //Super class
  {
        int x=10,y=20;
        void display()
        {
               System.out.println("This is the method in class one");
               System.out.println("Value of X= "+x);
               System.out.println("Value of Y= "+y);
        }
  }
   
  class two extends one        //Sub class -1 of class one
  {
        void add()
        {
               System.out.println("This is the method in class two");
               System.out.println("X+Y= "+(x+y));
        }
  }
   
  class three extends one//Sub class-2  of  class one
  {
        void mul()
        {
               System.out.println("This is the method in class three");
               System.out.println("X*Y= "+(x*y));
        }
  }
  /* Main class */
  class Hier
  {
        public static void main(String args[])
        {
               two t1=new two();      //Object of class two
               three t2=new three();  //Object of class three
               t1.display();  //Calling method of class one using class two object
               t1.add();              //Calling method of class two
               t2.mul();              //Calling method of class three
        }
  }

NB:-Java do not support Multiple inheritance. But java developer did not forget the importance of multiple inheritence .They have incorporated one similar concept in java, That  is called interface.
Interface

Interfaces are similar to abstract classes(Class whose object did not created)  but all methods are abstract and all properties are static final. Interfaces can be inherited (ie. you can have a sub-interface). As with classes the extends keyword is used for inheritence. Java does not allow multiple inheritance for classes (ie. a subclass being the extension of more than one superclass). An interface is used to tie elements of several classes together. Interfaces are also used to separate design from coding as class method headers are specified but not their bodies. This allows compilation and parameter consistency testing prior to the coding phase. Interfaces are also used to set up unit testing frameworks.


Example of inheritance

public interface Working
{
  public void work();
}

Now we have to defile a class which will implement interface.


public class WorkingDog extends Dog implements Working
{
  public WorkingDog(String nm)
  {
    super(nm);    // builds ala parent
  }
  public void work()  // this method specific to WorkingDog
  {
    speak();
    System.out.println("I can herd sheep and cows");
  }
}





1 comment:

  1. In Java, an interface is a blueprint for a class. It declares methods that a class implementing the interface must define. Does Improve Streaming This enforces a contract for behavior, enabling multiple classes to share points.

    ReplyDelete