CSE Helper
Powered By Dream Dragon Creation
Interfaces
Interface is a collection of abstract methods.A class Implementsan interface, thereby inheriting the abstract of the interface.
A class describes the attributes and behaviors of an object. Aninterface contains behaviors that a class implements.
An interface isfrom a class in several ways, including:
*You cannot instantiate an interface
* an interface does not contain any constructors.
*An interface cannot contain instance The only fields that can
appear in an interface must be declared both static and final.
*An interface is not extended by a class; it is implemented by a class.
*An interface can extend multiple interfaces.
Interfaces have the following properties:
An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an Each method in an interface is also implicitly abstract, so the
abstract keyword is needed.
Methods in an interface are implicitly public.
Example:
/* File name : Animal.java*/
interface Animal {
public void eat( );
}
Implementing Interfaces:
When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.
A class uses the implements keyword to implement an interface.
/* File name: Mammallnt.java */
public class Mammallnt implements Animal{
public void eat( ) {
System.out.print|n("Mamma| eats");
}
public static void main(String args[]){
Mammallnt m = new Mammallnt();
m.eat( );
}
}
This would produce following result:
Mammal eats
Extending Interfaces:
The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.
The following Sports interface is extended by Hockey and Football interfaces.
//filename: Sports.java
public interface Sports{
public void setHomeTeam(String name);
//fiIename: Football.java
public interface Football extends Sports{
public void homeTeamScored(int points);
}
The football interface has two methods, but it inherits one from Sports; thus, a class that implements Football needs to implement both methods.
Extending Multiple Interfaces:
A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.
The extends keyword is used once, and the parent interfaces are declared in a comma-Copied to clipboard
For example, if the Hockey interface extended both Sports and Event, it would be declared as:
public interface Hockey extends Sports, Event
Tagging Interfaces
The most common use of extending interfaces occurs when the parent interface does not contain any methods. For example, the MouseListener interface in the java.awt.event package extended
java.uti|.EventListener, which is defined as:
package java.util;
public interface EventListener{ }
An interface with no methods in it is referred to as
a tagging interface. There are two basic design purposes of tagging
interfaces:
creates a common parent:
As with the EventListener interface, which is extended by dozens of other interfaces in the Java API, you can use a tagging interface to create a common parent among a group of interfaces. For example,
when an interface extends EventListener, the JVM knows that this particular interface is going to be used in an event delegation scenario.
Adds a data type to a class:
This situation is where the term tagging comes from. A class that implements a tagging interface does not need to define any methods (since the interface does not have any), but the class becomes an
interface type through polymorphism.
Packages
Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching ocating and usage of classes, interfaces, enumerations and annotations easier etc.
A package can be defined as a grouping of related types (classes, interfaces, enumerations and annotations) providing access protection and name space management.
Some ofthe existing packages in Java are:
*java.|ang - bundles the fundamental classes
*java.io - classes for input , output functions are bundled in this package
Programmers can define their own packages to bundle group of classes / interfaces etc. It is a good practice to group related classes implemented by you so that a programmers can easily determine that the classes, interfaces, enumerations, annotations are related.
Since the package creates a new namespace there won‘t be any name conflicts with names in other packages. Using packages, it is easier to provide access control and it is also easier to locate the
related classed.
Creating a package
When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and
annotation types that you want to include in the package.
The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.
If a package statement is not used then the class, interfaces, enumerations, and annotation types will be put into an unnamed package.
Example:
/* File name : Animal.java */
package animals;
interface Animal{
public void eat( );
}
Now put an implementation in the same package animals:
package animals;
/* File name: Mammallnt.java */
public class Mammallnt implements Animal {
public void eat( ){
System.out.println("Mamma| eats");
}
}
Now you compile those two files and put them in sub- directory calledanimals and try to run as fallows:
$mkdir animals
$cp Animal.class Mmmallnt.class animals
$jana animals / Mammallnt
Mammal eats
import keyword
If a class wants to use another class in the same package, the package name does not need to be used. Classes in the same package find each other without any special syntax.
Example:
Here a class named Boss is added to the payroll package that already contains Employee. The Boss can then refer to the Employee class without using the payroll prefix, as demonstrated by the following Boss class.
package payroll;
public class Boss{
public void payEmployee(Employee e){
e.mailCheck();
}
}
What happens if Boss is not in the payroll package? The Boss class must then use one of the following techniques for referring to a class in a different package.
The fully qualified name ofthe class can be used. For example:
payroll.Employee
The package can be imported using the import keyword and the wild card (*). For example:
import payroll.*;