Interfaces
Interfaces
• In Java, we can not only achieve Abstraction,
with the help Abstract classes but also with
the help of Interfaces.
• Interfaces provides a skeleton of a class.
• We can only declare public, static and final
variable in an Interface.
• All the methods declared in an Interface are
public and abstract.(a method without
implementation)
interface A {
int v1=1;
}// interfaces can be inherited
interface B extends A
{
int v2=2;
int sum=v2+A.v1;
}
Interfaces
• Interface provide a protocol of behaviour
• How the behaviour is implemented is the
responsibility of that Individual class.
• Keyword “implements” is used to apply any
interfaces on any class.
• Interfaces provide a mechanism to provide
multiple inheritance.
Example
Why Interfaces required?
• Interface allows us to implement common
behaviour in two different classes.
• Interfaces allows us to apply behaviours beyond
classes and class hierarchy.
• Consistent specification among unrelated classes.
• Reduce coupling between software component
• Applying multiple inheritance.
• Two or more classes may not achieve same
functionality.(No duplicity of class)
• Create richer classes.
Interfaces
• More than one interfaces can be implemented to
single class
• Keyword “interface” is used to declare an
interface.
• Syntactically an interface is declared same as a
class.
• However, interface variable can be only public,
static and final.
• We can’t create object of an Interface like
abstract class.
Syntax

Java Interface

  • 1.
  • 2.
    Interfaces • In Java,we can not only achieve Abstraction, with the help Abstract classes but also with the help of Interfaces. • Interfaces provides a skeleton of a class. • We can only declare public, static and final variable in an Interface. • All the methods declared in an Interface are public and abstract.(a method without implementation)
  • 3.
    interface A { intv1=1; }// interfaces can be inherited interface B extends A { int v2=2; int sum=v2+A.v1; }
  • 4.
    Interfaces • Interface providea protocol of behaviour • How the behaviour is implemented is the responsibility of that Individual class. • Keyword “implements” is used to apply any interfaces on any class. • Interfaces provide a mechanism to provide multiple inheritance.
  • 5.
  • 6.
    Why Interfaces required? •Interface allows us to implement common behaviour in two different classes. • Interfaces allows us to apply behaviours beyond classes and class hierarchy. • Consistent specification among unrelated classes. • Reduce coupling between software component • Applying multiple inheritance. • Two or more classes may not achieve same functionality.(No duplicity of class) • Create richer classes.
  • 7.
    Interfaces • More thanone interfaces can be implemented to single class • Keyword “interface” is used to declare an interface. • Syntactically an interface is declared same as a class. • However, interface variable can be only public, static and final. • We can’t create object of an Interface like abstract class.
  • 8.