|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 5 of 7
Multiple interface inheritance solves the problem of read/write variables with two different types (and/or initial values) by requiring interfaces to declare only constants (that is, read-only variables). Because each variable is read-only, the compiler does not generate bytecode instructions to allocate memory for that variable -- the read-only variable never changes so it does not require its own memory. The compiler only needs to generate bytecode instructions that retrieve the constant's value from its class. If two different interfaces declare a constant with the same name, but with a different type and/or a different initial value, and if a class implements both interfaces but does not access either constant, the compiler does nothing; there is no problem. However, if a constant name appears in the class, the compiler requires the interface's name (followed by a period character) to prefix that name. Problem solved!
Two issues arise in multiple implementation inheritance in the context of methods:
To solve the first problem, multiple interface inheritance prohibits interfaces from specifying code bodies. Instead, the class that implements the interface supplies the code body.
In regards to the second issue, interfaces do not shoulder the blame for incorrectly overloaded methods, developers do. The following code fragment solves the same-named constant problem, but fails to solve the incorrectly overloaded method problem:
interface A
{
char c = 'A';
char m ();
}
interface B
{
boolean c = true;
void m ();
}
class C implements A, B
{
char m ()
{
return A.c;
}
void m ()
{
System.out.println (B.c);
}
}
The above code fragment will not compile because it unsuccessfully overloads m(). The solution: avoid incorrectly overloaded methods through careful class and interface design.
Though interfaces are a potent language feature, many Java newcomers fail to understand why they are so powerful. Interfaces don't just provide a workaround to Java's lack of support for multiple implementation inheritance. Interfaces factor out commonality across diverse class hierarchies, resulting in compact and understandable source code. For a taste of interface power, examine Listing 6:
Listing 6. InventoryDemo.java
// InventoryDemo.java
interface Product
{
String getName ();
double getCost ();
}
class Vehicle implements Product
{
private String name;
private double cost;
Vehicle (String name, double cost)
{
this.name = name;
this.cost = cost;
}
public String getName ()
{
return name;
}
public double getCost ()
{
return cost;
}
}
class Car extends Vehicle
{
Car (String name, double unitCost)
{
super (name, unitCost);
}
}
class Truck extends Vehicle
{
Truck (String name, double unitCost)
{
super (name, unitCost);
}
}
class Tool implements Product
{
private String name;
private double cost;
Tool (String name, double cost)
{
this.name = name;
this.cost = cost;
}
public String getName ()
{
return name;
}
public double getCost ()
{
return cost;
}
}
class InventoryDemo
{
public static void main (String [] args)
{
Product [] products =
{
new Car ("Jaguar", 100000.00),
new Tool ("JigSaw", 150.23),
new Car ("Neon", 17000.00),
new Tool ("JigSaw", 149.18),
new Car ("Jaguar", 110000.00),
new Car ("Neon", 17500.00),
new Car ("Neon", 17875.32),
new Truck ("RAM", 35700.00)
};
takeInventory ("JigSaw", products);
takeInventory ("Neon", products);
takeInventory ("Jaguar", products);
takeInventory ("RAM", products);
}
static void takeInventory (String item, Product [] p)
{
int quantity = 0;
double totalCost = 0.00;
for (int i = 0; i < p.length; i++)
if (p [i].getName ().equals (item))
{
quantity++;
totalCost += p [i].getCost ();
}
System.out.println (item + ": Quantity = " + quantity +
", Total cost = " + totalCost);
}
}
InventoryDemo declares two class hierarchies: a vehicle hierarchy and a tool hierarchy. What is common to both class hierarchies? Objects
you create from appropriate classes (such as Car and Tool) in each class hierarchy are products. The Product interface factors that commonality into its operations. As you can see, those operations make it easy to write compact code
that takes inventory. And that is the power of the interface.