Interfaces
Interfaces
1) Abstract class can have abstract and non- Interface can have only abstract methods.
abstractmethods.
2) Abstract class doesn't support multiple Interface supports multiple inheritance.
inheritance.
3) Abstract class can have final, non-final, Interface has only static and final variables.
static and non-static variables.
4) Abstract class can have static methods, main Interface can't have static methods, main
method and constructor. method or constructor.
5) Abstract class can provide the Interface can't provide the implementation of
implementation of interface. abstract class.
6) The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
7) Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Simply, abstract class achieves partial abstraction (0 to 100%)
whereas interface achieves fully abstraction (100%).
PROG:SeriesDemo2
IMPLEMENTING MULTIPLE INTERFACES
A class can implement more than one interface.
Multiple inheritance is not supported in case of class.
But it is supported in case of interface because there is
no ambiguity as implementation is provided by the
implementation class.
interface IfA {
void doSomething();
}
interface IfB {
void doSomethingElse();
}
// Implement both IfA and IfB.
class MyClass implements IfA, IfB {
public void doSomething() {
System.out.println("Doing something.");
}
public void doSomethingElse() {
System.out.println("Doing something else.");
}
}
If a class implements two interfaces that declare the
same method, then the same method implementation will
be used for both interfaces. This means that only one
version of the method is defined by the class.
PROG: MultiImpDemo
CONSTANTS IN INTERFACES:
package pkg;
Here, pkg is the name of the package. For example, the
following statement creates a package called
MyPackage:
package MyPackage;
You can create a hierarchy of packages.
package pkg1[.pkg2[.pkg3]];
Eg : package java.awt.image;
A SIMPLE PACKAGE EXAMPLE.
E:/java>javac mypack/B.java
Running:
E:/java>java mypack/B
Hello
PACKAGES AND MEMBER ACCESS:
Visible within
different package by
NO NO YES YES
subclass
Visible within
different by non-
NO NO NO YES
subclass
A package access example:
IMPORTING PACKAGES:
Java includes the import statement to bring certain
classes, or entire packages, into visibility.
Once imported, a class can be referred to directly, using
only its name.
This is the general form of the import statement: