OOPS concepts in Java
Java tutorials for beginners
Java basics concepts
Java Objects and Classes
What is Inheritance in Java?
What is Abstraction in Java?
What is Interface in Java?
What is Abstract Class in Java?
What is Polymorphism in Java?
What is Encapsulation in Java?
Java programming example
How Java Program Work?
Hello World in Java
Java basics for beginners
Interface:
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));
}
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));
}
{
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));
}