Course List: http://www.c-jump.com/bcc/

Interfaces


  1. interface example
  2. interface implementation example
  3. Calling method via interface
  4. Abstract class compared to interface
  5. Abstract class compared to interface, cont.
  6. Interface and abstract class compared
  7. Common java.lang interfaces
  8. interface syntax
  9. interface constants
  10. Tagging class by interface
  11. Implementing multiple interfaces
  12. extends and implements syntax
  13. Book and Product example
  14. Interface as method parameter
  15. Interface inheriting other interfaces
  16. Product interface examples
  17. interface constants example
  18. Factory method example
  19. Cloneable interface example
  20. clone method of the Product class
  21. Cloneable Object implementation
  22. Copy constructors

1. interface example



2. interface implementation example



3. Calling method via interface



4. Abstract class compared to interface



5. Abstract class compared to interface, cont.

  • A Printable interface

    
    public interface Printable
    {
        // interface methods are
        // abstract by default: 
        public void print();
    }
    
    
  • A Printable abstract class

    
    public abstract class Printable
    {
        public abstract void print();
    }
    
    

6. Interface and abstract class compared

  • Advantages of an abstract class

    • An abstract class can use instance variables and constants as well as static variables and constants. Interfaces can only use static constants

    • An abstract class can define regular methods that contain code as well as abstract methods that don't contain code. An interface can only define abstract methods.

    • An abstract class can define static methods. An interface can't.

  • Advantages of an interface

    • A class can only directly inherit one other class, but it can directly implement multiple interfaces.

    • Any object created from a class that implements an interface can be used wherever the interface is accepted.

7. Common java.lang interfaces



8. interface syntax



9. interface constants



10. Tagging class by interface



11. Implementing multiple interfaces



12. extends and implements syntax



13. Book and Product example



14. Interface as method parameter



15. Interface inheriting other interfaces



16. Product interface examples


  • A ProductReader interface

    
    public interface ProductReader
    {
        Product getProduct(String code);
        String getProductsString();
    }
    
    
  • A ProductWriter interface

    
    public interface ProductWriter
    {
        boolean addProduct(Product p);
        boolean updateProduct(Product p);
        boolean deleteProduct(Product p);
    }
    
    

17. interface constants example



18. Factory method example



19. Cloneable interface example



20. clone method of the Product class



21. Cloneable Object implementation



22. Copy constructors