Download Interfaces-Advanced Computer Programminng-Lab Lecture and more Lecture notes Java Programming in PDF only on Docsity!
API(Application Programming Interface)
Java API is a set of classes and interfaces that comes with the
JDK
Java API is actually a huge collection of library routines that
performs basic programming tasks such as looping, displaying
GUI form etc.
In the Java API classes and interfaces are packaged in packages
JAVA APIs
The Java comprises three components:
Java Language
JVM or Java Virtual Machine and
The Java API (Java programming interface)
Abstract Classes & Interfaces
Abstract class
An abstract class is a class in which one or more methods are
declared, but not defined.
Since they have no definition and cannot be executed, they
are called abstract methods.
Abstract class example
public abstract class Animal {
public abstract void sound(); // Abstract method
public Animal(String aType) {
type = new String(aType);
public String toString() {
return “This is a “ + type;
private String type;
Abstract class
An abstract method cannot be private since a private
method cannot be inherited and therefore cannot be
redefined in a subclass.
You cannot instantiate an object of an abstract class, but
you can declare a variable of an abstract class type. With the
new abstract version of the class Animal, you can still write:
Animal thePet = null; // Declare a variable of type Animal
Interfaces
An interface is essentially a collection of related constants
and/or abstract methods, and in most cases it will contain
just methods.
An interface doesn’t define what a method does. It just
defines its form—its name, its parameters, and its return
type, so by definition the methods in an interface are
abstract.
Interfaces
To make use of an interface, you implement the
interface in a class—that is, you declare that the class
implements the interface
and you write the code for each of the methods that the
interface declares as part of the class definition.
When a class implements an interface, any constants that
were defined in the interface definition are available
directly in the class, just as though they were inherited
from a base class
Interface Example
public interface Conversions {
double inchesToMillimeters (double inches);
double ouncesToGrams(double ounces);
double poundsToGrams(double pounds);
double hpToWatts(double hp);
double wattsToHP(double watts);
public class MyClass implements Conversions { // Implementations for the methods in the Conversions interface // Definitions for the other class members... }
Implementing more than one
interface
public class MyClass implements Conversions,
Definitions, Detections {
// Definition of the class including implementation
of interface methods
Using constants defined in an
interface
public class MyOtherClass implements ConversionFactors { public static double poundsToGrams(double pounds) { return pounds*POUND_TO_GRAM; } // Plus the rest of the class definition... }
public class MyClass { public static double poundsToGrams(double pounds) {
return pounds * ConversionFactors.POUND_TO_GRAM ;
// Plus the rest of the class definition... }
Partial interface implementation
You can omit the implementation of one or more of the methods from
an interface in a class that implements the interface,
but in this case you would need to declare the class itself as abstract:
public interface Convert { public void myMethod1(); public void myMethod2(); }
abstract public class interfaceExample implements Convert{ public void myMethod1(){
} }
Multiple Inheritance
Unlike a class, which can extend only one other class, an
interface can extend any number of other interfaces.
public interface MyInterface extends HisInterface, HerInterface { // Interface members – constants and abstract methods... }
Polymorphism and Interfaces
Same as polymorphism with class