Interface in Java in Details Step by Step

The interface in java provides abstraction. We can create only abstract method in interface and can’t create implementation or method body. It is used to achieve abstraction and multiple inheritance in Java.  It has static constants and abstract methods.

Rule of Interface

1.        All interface Methods are implicitly public and abstract
2.        All variables in Interface by default constant(public, static, final).
3.        Interface Methods can not be static.
4.        Interface Methods can not be final.
5.        One Interface can extend one or more Interface.

Example:


interface Area
{
  final static float pi = 3.14f;
  float findArea(float x);
}


class Circle implements Area

{
  public float findArea(float x)
   {
     return (x*x*xpi);
   }
}


class Square implements Area

{
  public float findArea(float x)
  {
   return(x*x);
  }
}


public class InterFaceDemo

{
 public static void main(String args[])
 {
  Circle c = new Circle();
  Square s= new Square();
  Area area;
  area = c;
  System.out.println("Area of Circle is" + area.findArea(10));
  area = s;
  System.out.println("Area of Square is" + area.findArea(30));

 }

2 comments:

  1. Really very good article. the post has excellent tips which are very useful.


    laptop price

    ReplyDelete
  2. Excellent Post. Are you looking for Laptops for your training? Click here Laptop Price to select your idle Laptop in budget.

    ReplyDelete