CSC/ECE 517 Fall 2011/ch1 2c rs

From Expertiza_Wiki
Revision as of 18:54, 20 September 2011 by Svontel (talk | contribs) (Created page with "Interfaces in Java Interfaces in Java. An interface in java is a collection of related methods with empty bodies,constant declarations,nested types. It is declared using a...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Interfaces in Java

Interfaces in Java. An interface in java is a collection of related methods with empty bodies,constant declarations,nested types. It is declared using a key word ‘interface’. Constants are both static and final. The methods are implicitly An interface cannot be instantiated as it is incomplete ie the methods are only declared but not defined. It can only be extended by other interfaces or implemented by classes

A variable whose declared type is an interface type can hold a reference to an object of a class (or its subclass) that has implemented this interface.

Contractual Obligation A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an abstract classif it fails to implement even one method. An error message is issued by the compiler if the if a class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.

eg:

Usage of Interface. A very common use of interface is for listeners.

Difference between class and interface in Java:

A class may be de