Interface : Interface Presented By
Mr. SUBHAJIT ROY
(LECTURER DEPT. Of CSE, BUIE) Interface : Interface Interfaces are used to encode similarities which classes of various types share, but do not necessarily constitute a class relationship.
For example, a human and a parrot can both whistle; however, it would not make sense to represent Humans and Parrots as subclasses of a Whistler class. Rather they would most likely be subclasses of an Animal class (likely with intermediate classes), but both would implement the Whistler interface. Interface : Interface For another example, imagine a futuristic society where computer-controlled robotic cars transport passengers through city streets without a human operator. Automobile manufacturers write software (Java, of course) that operates the automobile—stop, start, accelerate, turn left, and so forth. Another industrial group, electronic guidance instrument manufacturers, make computer systems that receive GPS (Global Positioning System) position data and wireless transmission of traffic conditions and use that information to drive the car.
The auto manufacturers must publish an industry-standard interface that spells out in detail what methods can be invoked to make the car move (any car, from any manufacturer). The guidance manufacturers can then write software that invokes the methods described in the interface to command the car. Neither industrial group needs to know how the other group's software is implemented. In fact, each group considers its software highly proprietary and reserves the right to modify it at any time, as long as it continues to adhere to the published interface. Interface : Interface What is INTERFACE ?
In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces. Defining Interface : Defining Interface access interface interfacename
type final var-name=value;
Access is either public or not used.
Methods are declared have no bodies and essentially abstract method. Each class that includes an interface must implement all of the methods.
Variables are implicitly final and static, meaning they can't be change by the implementing class. Implementing Interface : Implementing Interface Class classname implements Interfacename
body of classname
Example1 What Can I Use Interfaces For? : What Can I Use Interfaces For? Interfaces are useful for the following:
Capturing similarities between unrelated classes without artificially forcing a class relationship.
Declaring methods that one or more classes are expected to implement .
Revealing an object's programming interface without revealing its class. (Objects such as these are called anonymous objects and can be useful when shipping a package of classes to other developers.) Summary of Interfaces : Summary of Interfaces An interface defines a protocol of communication between two objects.
An interface declaration contains signatures, but no implementations, for a set of methods, and might also contain constant definitions.
A class that implements an interface must implement all the methods declared in the interface.
An interface name can be used anywhere a type can be used. Packages : Packages This lesson explains how to bundle classes and interfaces into packages, how to use classes that are in packages, and how to arrange your file system so that the compiler can find your source files.
Definition: A package is a grouping of related types (class, interface) providing access protection and name space management. Creating and Using Packages : Creating and Using Packages Defining a package :
Importing a package :
Naming convention :
double y = java.lang.Math.sqrt(x);
package class method Creating and Using Packages : Creating and Using Packages Example for creating package
Class Member Access table: Summary of Creating and Using Packages : Summary of Creating and Using Packages To create a package for a type, put a package statement as the first statement in the source file that contains the type (class, interface type).
To use a public type that's in a different package, you have three choices: (1) use the fully qualified name of the type, (2) import the type, or (3) import the entire package of which the type is a member.
The path names for a package's source and class files mirror the name of the package.
You might have to set your CLASSPATH so that the compiler and the JVM can find the .class files for your types REFERENCES : REFERENCES Java 2 Complete reference by Schildt.
Programming with Java by E. Balagurusamy.
www.iam.ubc.ca/guides/javatut99/trailmap.html Slide 14: THANK YOU