Difference between Inheritance and Interfaces inheritance In java
Program To An Interface, Not An Implementation:
Implementation Inheritance vs Interface Inheritance:- An interface is the set of methods one object knows it can invoke on another object.
- An object can have many interfaces. (Essentially, an interface is a subset of all the methods that an object implements).
- A type is a specific interface of an object
- Different objects can have the same type and the same object can have many different types
- An object is known by other objects only through its interface
- In a sense, interfaces express "is a kind of" in a very limited way as "is a kind of that supports this interface"
- Interfaces are the key to pluggability!.
- Implementation Inheritance (Class Inheritance) - an object's implementation is defined in terms of another's objects implementation.
- Interface Inheritance (Subtyping) - describes when one object can be used in place of another object.
- The C++ inheritance mechanism means both class and interface inheritance.
- C++ can perform interface inheritance by inheriting from a pure abstract class
- Java has a separate language construct for interface inheritance - the Java interface
- Java's interface construct makes it easier to express and implement designs that focus on object interfaces.
package
aurangzeb.utk.com;
public interface IManeuverable {
public void left();
public void right();
public void forward();
public void reverse();
public void climb();
public void dive();
public void setSpeed(double speed);
public double getSpeed();
}
package
aurangzeb.utk.com;
public class Car implements IManeuverable {
@Override
public void left() {
// TODO
Auto-generated method stub
}
@Override
public void right() {
// TODO
Auto-generated method stub
}
@Override
public void forward() {
// TODO
Auto-generated method stub
}
@Override
public void reverse() {
// TODO
Auto-generated method stub
}
@Override
public void climb() {
// TODO
Auto-generated method stub
}
@Override
public void dive() {
// TODO
Auto-generated method stub
}
@Override
public void setSpeed(double speed) {
// TODO
Auto-generated method stub
}
@Override
public double getSpeed() {
// TODO
Auto-generated method stub
return 0;
} //
Code here. }
package
aurangzeb.utk.com;
public class Boat implements IManeuverable {
@Override
public void left() {
// TODO
Auto-generated method stub
}
@Override
public void right() {
// TODO
Auto-generated method stub
}
@Override
public void forward() {
// TODO
Auto-generated method stub
}
@Override
public void reverse() {
// TODO
Auto-generated method stub
}
@Override
public void climb() {
// TODO
Auto-generated method stub
}
@Override
public void dive() {
// TODO
Auto-generated method stub
}
@Override
public void setSpeed(double speed) {
// TODO
Auto-generated method stub
}
@Override
public double getSpeed() {
// TODO
Auto-generated method stub
return 0;
}
}
This method in some other class can maneuver the vehicle without being concerned about what the actual class is (car, boat,submarine) or what inheritance hierarchy it is in.
public void
travel(IManeuverable vehicle) {
vehicle.setSpeed(35.0);
vehicle.forward();
vehicle.left();
vehicle.climb();
}
Join me on Google+
