Interface:
• An interface is an abstract "class"
that is used to group related methods
with "empty" bodies: To access the
interface methods, the interface must
be "implemented" (kind a like
inherited) by another class with the
implements keyword (instead of
extends ).
https://www.ue.edu.pk/
An interface is different from a class:
• An interface is different from a class in several ways:
• An interface cannot be instantiated.
• All of the methods in an interface are abstract.
• An interface is not extended by a class; it is implemented by a
class.
• An interface can extend multiple interface.
https://www.ue.edu.pk/
Defining an interface
• An interface is defined much like a class. This is a
Simplified general Form of an interface:
• Syntax:
Access interface name {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
//….
return-type method-nameN(parameter-list);
}
https://www.ue.edu.pk/
Difference between class and interface.
ABSRACT CLASS INTERFACE
• Abstract class can have abstract and • Interface can have only abstract
non abstract methods. methods..
• Abstract class does not support • Interface supports multiple
multiple inheritance. inheritance.
• The Abstract keyword is used to • The interface keyword is used to
declare abstract class. declare interface.
• Example: • Example:
Public abstract class Shape { Public interface Drawable{
Public abstract void draw (); Void draw();
} }
https://www.ue.edu.pk/
Defining an Interface
• Here interface is a keyword and interface name is any valid
java variable.
• Syntax:
Interface interfaceName
{
Variable Declaration;
Methods Declaration;
}
https://www.ue.edu.pk/
Extending Interfaces
• An interface can extend another interface in the same way that a class can extend another
class. The extends keyword is used to extend an interface, and the child interface inherits the
methods of the parent interface.
• Syntax:
Interface name1 extends name2
{
Body of name 2
}
https://www.ue.edu.pk/
Implementing the interface
• Interfaces are used as “superclasses”
whose properties are inherited by classes
by using the keyword implements.
• Syntax:
Class classname impements interfacename
{
body of class
}
https://www.ue.edu.pk/
Implementing and Extending Interface in Java
• An interface can extend another interface in the same way that a class can extend another
class. The extends keyword is used to extend an interface, and the child interface inherits
the methods of the parent interface. The following Sports interface is extended by Hockey
and Football interfaces.
https://www.ue.edu.pk/