CIS-260 Home http://www.c-jump.com/bcc/

Interfaces and Abstract Classes


  1. Interfaces and Abstract Classes
  2. Understanding contracts
  3. Interface, Inheritance, and Composition
  4. What is an interface?
  5. Why discuss inheritance, interfaces, abstract classes, and composition in one module?
  6. Interface, Inheritance, and Composition Example
  7. Contract Enforcement using Interfaces
  8. Java and Multiple Inheritance
  9. Java and Multiple Inheritance, Cont.

1. Interfaces and Abstract Classes



2. Understanding contracts


  • Contract is an interface: what is to be done, not how.

  • Interface is like a class with a set of public operations but no attributes and no implementation.

  • A UML diagram of a Java interface:

     

      java interface

  • 
    public interface Nameable {
        String getName();
        void setName (String aName);
    }
    
    
  • Compare to:

    
    public abstract class Nameable
    {
        public abstract  String getName();
        public abstract void setName (String aName);
    
    }
    
    

3. Interface, Inheritance, and Composition



4. What is an interface?



5. Why discuss inheritance, interfaces, abstract classes, and composition in one module?



6. Interface, Inheritance, and Composition Example


  • Inheritance: Dog is-a Mammal.

  • Interface: Dog implements Nameable.

  • Composition: Dog has-a Head.

     

  • Note that Nameable interface could also be implemented by a Planet class, a Car class, etc.

  • UML example of the interface, inheritance, and composition relationships, nameable dog:

      nameable dog


7. Contract Enforcement using Interfaces



8. Java and Multiple Inheritance



9. Java and Multiple Inheritance, Cont.