Interface
Abstract
Interface
Difference
When to use what?
Abstract and Interface

  1. public interface Drivable
  2. {
  3. // Constant
  4. int wheel = 4;
  5.  
  6. // Abstract methods
  7. String getMaker();
  8. void setMaker(String m);
  9.  
  10. // Static methods
  11. static String getType()
  12. {
  13. return "Wheel: "+4;
  14. }
  15.  
  16. // Default methods
  17. default void display()
  18. {
  19. System.out.println("Wheel: "+wheel);
  20. }
  21. }
  1. public abstract class Car implements Drivable
  2. {
  3. private String maker;
  4.  
  5. public Car(String m)
  6. {
  7. maker = m;
  8. }
  9.  
  10. public String getMaker()
  11. {
  12. return maker;
  13. }
  14.  
  15. public void setMaker(String m)
  16. {
  17. maker = m;
  18. }
  19.  
  20. @Override
  21. public String toString()
  22. {
  23. return "Maker: "+maker;
  24. }
  25.  
  26. public abstract void display();
  27. }
  1. public class Buick extends Car
  2. {
  3. private String color;
  4.  
  5. public Buick(String m, String c)
  6. {
  7. super(m);
  8. color = c;
  9. }
  10.  
  11. public String getColor()
  12. {
  13. return color;
  14. }
  15.  
  16. public void setColor(String c)
  17. {
  18. color = c;
  19. }
  20.  
  21. public String toString()
  22. {
  23. return super.toString()+" "+"Color: "+color;
  24. }
  25.  
  26. @Override
  27. public void display()
  28. {
  29. System.out.println("Buick display ...");
  30. }
  31. }
  1. public class Test
  2. {
  3. public static void main(String args[])
  4. {
  5. Drivable d = new Buick("Buick", "White");
  6.  
  7. System.out.println(d.wheel);//static field
  8. System.out.println(Drivable.getType());//static method
  9. d.display();//default method
  10. System.out.println(d);//toString
  11.  
  12. Buick b = new Buick("Honda", "Buick");
  13.  
  14. System.out.println(b.getMaker()+" "+b.getColor());//abstract methods
  15. }
  16. }

Multiple Inheritance
  1. public interface Drivable
  2. {
  3. // Constant
  4. int wheel = 4;
  5.  
  6. // Abstract methods
  7. String getMaker();
  8. void setMaker(String m);
  9.  
  10. // Default methods
  11. default void display()
  12. {
  13. System.out.println("Wheel: "+wheel);
  14. }
  15. }
  1. public interface Paintable
  2. {
  3. String getColor();
  4. void setColor(String c);
  5. }
  1. public class Car implements Drivable, Paintable
  2. {
  3. private String maker;
  4. private String color;
  5.  
  6. public Car(String m, String c)
  7. {
  8. maker = m;
  9. color = c;
  10. }
  11.  
  12. public String getColor()
  13. {
  14. return color;
  15. }
  16.  
  17. public String getMaker()
  18. {
  19. return maker;
  20. }
  21.  
  22. public void setColor(String c)
  23. {
  24. color = c;
  25. }
  26.  
  27. public void setMaker(String m)
  28. {
  29. maker = m;
  30. }
  31.  
  32. @Override
  33. public String toString()
  34. {
  35. return "Maker: "+maker+" Color: "+color;
  36. }
  37. }
  1. public class Test
  2. {
  3. public static void main(String args[])
  4. {
  5. Drivable c = new Car("Buick", "White");
  6.  
  7. System.out.println(c.getMaker());
  8.  
  9. Paintable c2 = new Car("Honda", "Blue");
  10.  
  11. System.out.println(c2.getColor());
  12. }
  13. }

Implements Interfaces Containing Same Functions
  1. public interface Drivable
  2. {
  3. // Constant
  4. int wheel = 4;
  5.  
  6. // Abstract methods
  7. String getMaker();
  8. void setMaker(String m);
  9.  
  10. // Default methods
  11. default void display()
  12. {
  13. System.out.println("Driable ...");
  14. }
  15. }
  1. public interface Paintable
  2. {
  3. String getColor();
  4. void setColor(String c);
  5.  
  6. default void display()
  7. {
  8. System.out.println("Paintable ...");
  9. }
  10. }
  1. public class Car implements Drivable, Paintable
  2. {
  3. private String maker;
  4. private String color;
  5.  
  6. public Car(String m, String c)
  7. {
  8. maker = m;
  9. color = c;
  10. }
  11.  
  12. public String getColor()
  13. {
  14. return color;
  15. }
  16.  
  17. public String getMaker()
  18. {
  19. return maker;
  20. }
  21.  
  22. public void setColor(String c)
  23. {
  24. color = c;
  25. }
  26.  
  27. public void setMaker(String m)
  28. {
  29. maker = m;
  30. }
  31.  
  32. @Override
  33. public void display()
  34. {
  35. Drivable.super.display();
  36. Paintable.super.display();
  37. }
  38.  
  39. @Override
  40. public String toString()
  41. {
  42. return "Maker: "+maker+" Color: "+color;
  43. }
  44. }
Reference