The concept of "realization" is what is implied by the Java implements keyword: class Dell implements Laptop; which is not the same as than Dell extends Laptop. A realization is an implementation (read: actual code) for a given interface.
public interface Laptop {
public String getOpSystem();
}
public class Dell implements Laptop {
// implementation of the Laptop inteface
public String getOpSystem() { return "Windows XP";
}
}
The "inheritance" relationship is between a class and a subclass; for example, class Inspiron extends Dell is an example where the class Inspiron is a subclass of Dell. The Java extends keyword is saying: "the Inspiron class inherits the implementation (attributes and methods) of the Dell class."
public class Dell implements Laptop {
// implementation of the Laptop inteface
public String getOpSystem() { return "Windows XP";
}
}
public class Inspiron extends Dell {
// override behavior to make this class unique to the Inspiron line of Dell laptops
public String getOpSystem() { return "Windows 7";
}
}
You could also model laptops from other vendors; let's say Apple: class MacBook implements Laptop. And we could even have a class hierarchy of Apple laptops like we did with Dell: class MacBookPro extends MacBook.
public class MacBook implements Laptop {
// implementation of the Laptop inteface
public String getOpSystem() { return "Mac OS X";
}
}
public class MacBookPro extends MacBook {
// override behavior to make this class unique to the Pro line of Apple laptops
}
Now we have two distinct class hierarchies that share the same "contract" of what it means to be a Laptop. These two hierarchies of laptop classes could have fairly different behaviors (one runs Windows and the other runs Mac OS X, for example) but they both satisfy the laptop contract. So if we look at my original Client class with your example; we could write code like this:
public class Client {
public void doSomething(Laptop laptop) {
System.out.println(laptop. getOpSystem());
}
}
public class Main {
public static void main(String[] args) {
Client client = new Client();
// We can create a variable of type: Laptop
Laptop laptop = new Inspiron();
client.doSomething(laptop);
// We can create a variable of a more specific type: MacBook
MacBook macintosh = new MacBook();
client.doSomething(macintosh) ;
}
}
The type of the object is not important to the Client class; all it needs to know is that the object implements the Laptop contract.