Packages And Interfaces
  • In these packages can be container for classes.It is used to store class named called list.

  • Using interface, you can specify a set of methods that can be implemented by one or more classes.

Defining a package: General form

package MyPackage;

Java uses file system directories to store packages.

For example:

  • class files for any
    classes you declare to be part of MyPackage must be stored in a directory called MyPackage.

  • Remember that case is significant, and the directory name must match the package name exactly.

  • You can create a hierarchy of packages. To do so, simply separate each package name
    from the one above it by use of a period.

The general form of a multileveled package statement is shown here:

                                        package pkg1[.pkg2[.pkg3]];

For example: In a Windows environment, if the path to MyPack is

                                        C:\MyPrograms\Java\MyPack

then the class path to MyPack is

                                        C:\MyPrograms\Java

Example:

package mypack;
class Balance {
String name;
double bal;
Balance(String n, double b) {
name = n;
bal = b;
}
void show() {
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
public class AccountBalance {
public static void main(String args[]) {
Balance current[] = new Balance[3];
current[0] = new Balance("K. J. Fielding", 123.23);
current[1] = new Balance("Will Tell", 157.02);
current[2] = new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++) current[i].show();
}
}

Access Protection:

  • It access four levels of class.

1.subclasses in same packages.

2.non-subclasses in same packages.

3.subclasses in different packages.

4.classes that are neither in same package nor different packages.

public- accessed outside in its class.

private-accessed inside in its class.

Examples:

public class Protection {
int n = 1;
private int n_pri = 2;
protected int n_pro = 3;
public int n_pub = 4;
public Protection() {
System.out.println("base constructor");
System.out.println("n = " + n);
System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
  • In these the default, private,protected,public modifiers are used to declare the source code for the program.

Importing packages:

  • Java.lang is the mainly used to implicitly import the functionality of the compiler.

Example:

                                     package MyPack;
/* Now, the Balance class, its constructor, and its show() method are public. This means that they can
be used by non-subclass code outside their package.*/
public class Balance {
String name;
double bal;
public Balance(String n, double b) {
name = n;
bal = b;
}
public void show() {
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
In these balance class and constructor and void are showing in public so they can be accessed by
 any type of the code outside the package.
Balance test = new Balance("J. J. Jaspers", 99.88);
test.show(); // you may also call show()
}
}
  • As a result remove the public specifier and then compile it.

Interface:

  • Interface is inherit the abstract method of interface.

  • Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types.

  • Before continuing an important point needs to be made. JDK 8 added a feature to interface that makes a significant change to its capabilities.

Example:


class A {
// this is a nested interface
public interface NestedIF {
boolean isNotNegative(int x);
}
}
// B implements the nested interface.
class B implements A.NestedIF {
public boolean isNotNegative(int x) {
return x < -11 ? false: true;
}
}
public class NestedIFDemo {
public static void main(String args[]) {
// use a nested interface reference
A.NestedIF nif = new B();
if(nif.isNotNegative(10))
System.out.println("10 is not negative");
if(nif.isNotNegative(-12))
System.out.println("this won't be displayed");
}
}

Interfaces Extended:

  • One interface can inherit another by use of the keyword extends.

  • The syntax is the same as for inheriting classes.

Examples:

interface A {
void meth1();
void meth2();
}
// B now includes meth1() and meth2() -- it adds meth3().
interface B extends A {
void meth3();
}
// This class must implement all of A and B
class MyClass implements B {
public void meth1() {
System.out.println("Implement meth1().");
}
public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");
}
}
public class IFExtend {
public static void main(String arg[]) {
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}
Default method for inheritance:  

Example:
class MyIFImp implements MyIF {
// Only getNumber() defined by MyIF needs to be implemented.
// getString() can be allowed to default.
public int getNumber() {
return 100;
}
}
public class DefaultMethodDemo {
public static void main(String args[]) {
MyIFImp obj = new MyIFImp();
// Can call getNumber(), because it is explicitly
// implemented by MyIFImp:
System.out.println(obj.getNumber());
// Can also call getString(), because of default
// implementation:
System.out.println(obj.getString());
}
}

Multiple Inheritance Issues:

  • Multiple inheritance of implementation is the ability to inherit method definitions from multiple classes.

Example:

  • In cases in which one interface inherits another, with both defining a common default
    method, the inheriting interface’s version of the method takes precedence.

  • Therefore, continuing the example, if Beta extends Alpha, then Beta’s version of reset( ) will be used.

  • If Beta wants to refer to Alpha’s default for reset( ), it can use this statement:

                                                  Alpha.super.reset();
    

Use static Methods in an Interface:

  • No implementation of the interface is necessary and no instance of the interface is required, in order to call a static method.

  • Here is the general form:

                                         InterfaceName.staticMethodName
    

results matching ""

    No results matching ""