0% found this document useful (0 votes)
45 views8 pages

Variable in Interfaces and Extent Interface-2.Pptx-2

An interface is an abstract class that defines methods but does not provide method implementations. Classes implement interfaces by providing method bodies for the interface's abstract methods. Interfaces can extend other interfaces and classes can implement multiple interfaces. Interfaces are defined using the interface keyword and methods within interfaces are implicitly abstract.

Uploaded by

bsf2105191
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views8 pages

Variable in Interfaces and Extent Interface-2.Pptx-2

An interface is an abstract class that defines methods but does not provide method implementations. Classes implement interfaces by providing method bodies for the interface's abstract methods. Interfaces can extend other interfaces and classes can implement multiple interfaces. Interfaces are defined using the interface keyword and methods within interfaces are implicitly abstract.

Uploaded by

bsf2105191
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

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/

You might also like