Object Oriented Programming: Unit II: Inheritance, Packages and Interfaces

Interfaces

Object Oriented Programming

The access_modifier specifies the whether the interface is public or not. If the access specifier is not specified for an interface then that interface will be accessible to all the classes present in that package only.

Interfaces

• The interface can be defined using following syntax

access_modifier interface name_of_interface

{

return_type method_name1 (parameter1,parameter2,...parametern);

……..

return_type method_name1 (parameter1,parameter2,...parametern); 

type static final variable_name= value;

…….

}

• The access_modifier specifies the whether the interface is public or not. If the access specifier is not specified for an interface then that interface will be accessible to all the classes present in that package only. But if the interface is declared as public then it will be accessible to any of the class.

• The methods declared within the interface have no body. It is expected that these methods must be defined within the class definition.

Difference between Class and Interface

An interface is similar to a class but there lies some difference between the two.

Difference between Abstract Class and Interface

Following table presents the difference between the abstract class and interface.

Review Question

1. Present a detailed comparison between classes and interfaces.

Object Oriented Programming: Unit II: Inheritance, Packages and Interfaces : Tag: : Object Oriented Programming - Interfaces