Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Interfaces and abstract classes in Java

Interfaces and abstract classes in Java

Class given at ReDI School Berlin in April 2020.

Faiçal Tchirou

April 06, 2020
Tweet

More Decks by Faiçal Tchirou

Other Decks in Programming

Transcript

  1. Interfaces & Abstract Classes Program: Digital Career Program Semester: Spring

    2020 Course: Java Intermediate Date: April 6, 2020
  2. Learning Objectives ✅ Define what an interface is ✅ Create

    and use interfaces in Java ✅ Explain what interfaces are useful for ✅ Define what an abstract class is ✅ Create and use abstract classes in Java ✅ Explain what abstract classes are useful for ✅ Explain the differences between abstract classes and interfaces
  3. An abstract class in Java is a class that cannot

    be instantiated. An abstract class can only be inherited or subclassed. What’s an abstract class?
  4. How to declare an abstract class? public abstract class GeometricShape

    { abstract double area(); void printArea() { System.out.println("The area of this shape is " + this.area()); } }
  5. How to declare an abstract class? public abstract class GeometricShape

    { abstract double area(); void printArea() { System.out.println("The area of this shape is " + this.area()); } }
  6. How to declare an abstract class? public abstract class GeometricShape

    { abstract double area(); void printArea() { System.out.println("The area of this shape is " + this.area()); } }
  7. How to use an abstract class? public class Rectangle extends

    GeometricShape { private double width; private double height; @Override double area() { return width * height; } }
  8. How to use an abstract class? public class Circle extends

    GeometricShape { private double radius; @Override double area() { return Math.PI * radius * radius; } }
  9. How to use an abstract class? public class Main {

    public static void main(String[] args) { Rectangle rectangle = new Rectangle(300, 200); Circle circle = new Circle(42); System.out.print("Rectangle: "); rectangle.printArea(); System.out.print("Circle: "); circle.printArea(); } } > Task :Main.main() Rectangle: The area is 60000.0 Circle: The area is 5541.769440932396
  10. Let’s build Super Mario Luigi Mario Toad ★Can run ★Can

    jump ★Can run ★Can jump ★Can run ★Can jump
  11. Let’s build Super Mario public class Character { private String

    name; public Character(String name) { this.name = name; } public void run() { System.out.println("I'm " + name + "! I'm running !!..."); } public void jump() { System.out.println("I'm " + name + "! I'm jumping !!..."); } }
  12. Let’s build Super Mario public class Game { Character character;

    public Game(Character character) { this.character = character; } public void makeCharacterRun() { character.run(); } public void makeCharacterJump() { character.jump(); } }
  13. Let’s build Super Mario > Task :Main.main() I'm Mario! I'm

    running ""... I'm Mario! I'm jumping ""... public class Main { public static void main(String[] args) { Character mario = new Character("Mario"); Game game = new Game(mario); game.makeCharacterRun(); game.makeCharacterJump(); } }
  14. Let’s build Super Mario Luigi Mario Toad ★Can run ★Can

    jump very high ★Can run ★Can jump ★Can run very fast ★Can jump
  15. public class Mario extends Character { @Override void jump() {

    System.out.println("I'm a Mario! And I'm running normally !!..."); } @Override void run() { System.out.println("I'm a Mario! And I’m jumping normally !!..."); } } Let’s build Super Mario
  16. public class Main { public static void main(String[] args) {

    Character mario = new Mario(); new Character("Mario") Game game = new Game(mario); game.makeCharacterRun(); game.makeCharacterJump(); } } Let’s build Super Mario > Task :Main.main() I'm a Mario! And I'm running normally ""... I'm a Mario! And I'm jumping normally ""...
  17. public class Luigi extends Character { @Override void jump() {

    System.out.println("I'm a Luiiiigi! And I'm jumping very high !!..."); } @Override void run() { System.out.println("I'm Luiiiigi! And I'm running normally!!..."); } } Let’s build Super Mario
  18. Let’s build Super Mario public class Main { public static

    void main(String[] args) { Character luigi = new Luigi(); new Character(“Luigi") Game game = new Game(luigi); game.makeCharacterRun(); game.makeCharacterJump(); } } > Task :Main.main() I'm Luiiiigi! And I'm running normally ""... I'm a Luiiiigi! And I'm jumping very high…
  19. public class Toad extends Character { @Override void jump() {

    System.out.println("I'm Toad Toad! And I'm jumping normally!!..."); } @Override void run() { System.out.println("I'm Toad Toad! And I'm running very fast !!..."); } } Let’s build Super Mario
  20. Let’s build Super Mario public class Main { public static

    void main(String[] args) { Character toad = new Toad(); new Character(“Toad") Game game = new Game(toad); game.makeCharacterRun(); game.makeCharacterJump(); } } > Task :Main.main() I'm Toad Toad! And I’m running very fast ""... I'm Toad Toad! And I'm jumping normally ""...
  21. Why abstract classes? Similarly to super classes (normal inheritance), abstract

    classes are used to share common capabilities among subclasses. The difference here is that the specifics of the capabilities are known to subclasses only.
  22. Abstract Classes: Recap ✅ Declared using the abstract keyword ✅

    Can have methods and attributes ✅ Can have abstract methods (with no implementation) ✅ Can not be instantiated ✅ Can only be inherited ✅ Used to define common capabilities among subclasses but leaves the specifics of those capabilities to the subclasses
  23. What’s an interface? An interface is a mechanism by which

    Java objects can adopt certain traits or behavior.
  24. How to declare an interface? public interface Drawable { void

    draw(); void print() { !// Prints this drawable !!... } } ❌
  25. How to use an interface? public class Rectangle extends Shape

    implements Drawable { private double width; private double height; @Override double area() { return width * height; } @Override public void draw() { !// Drawing this rectangle on screen! } }
  26. How to use an interface? public class Paint { public

    void draw(Drawable drawable) { drawable.draw(); } }
  27. How to use an interface? public class Main { public

    static void main(String[] args) { Paint paint = new Paint(); Rectangle rectangle = new Rectangle(); paint.draw(rectangle); } }
  28. Fire Balls Throwing public class Mario extends Character { @Override

    void jump() { System.out.println("I'm a Mario! And I'm running normally !!..."); } @Override void run() { System.out.println("I'm a Mario! And I’m jumping normally !!..."); } }
  29. Fire Balls Throwing public class Mario extends Character { @Override

    void jump() { System.out.println("I'm a Mario! And I'm running normally !!..."); } @Override void run() { System.out.println("I'm a Mario! And I'm jumping normally !!..."); } void throwFireBall() { System.out.println("I'm a Mario! And I'm throwing fire balls "); } }
  30. Fire Balls Throwing public class Mario extends Character implements FireBallThrowable

    { @Override void jump() { System.out.println("I'm a Mario! And I'm running normally !!..."); } @Override void run() { System.out.println("I'm a Mario! And I'm jumping normally !!..."); } @Override public void throwFireBall() { System.out.println("I'm a Mario! And I'm throwing fire balls "); } }
  31. Fire Balls Throwing public class Luigi extends Character implements FireBallThrowable

    { @Override void jump() { System.out.println("I'm a Luiiiigi! And I'm running normally !!..."); } @Override void run() { System.out.println("I'm Luiiiigi! And I'm jumping very high !!..."); } @Override public void throwFireBall() { System.out.println("I'm Luiiiigi! And I'm throwing fire balls "); } }
  32. Giant Mario public class Mario extends Character implements FireBallThrowable, GiantTransformable

    { @Override public void throwFireBall() { System.out.println("I'm a Mario! And I'm throwing fire balls "); } @Override public void stomp() { System.out.print("I'm a Mario! And I'm destroying anything in my path!"); } }
  33. Giant Mario public class Mario extends Character implements FireBallThrowable, GiantTransformable

    { @Override public void throwFireBall() { System.out.println("I'm a Mario! And I'm throwing fire balls "); } @Override public void stomp() { System.out.print("I'm a Mario! And I'm destroying anything in my path!"); } }
  34. Bonus Level public class Main { public static void main(String[]

    args) { Mario mario = new Mario(); Game game = new Game(); game.startBonusLevel(mario); } }
  35. Why interfaces? Interfaces are used to abstract a specific behavior,

    by defining a contract that objects who wish to adopt the behavior, should conform to.
  36. Interfaces: Recap ✅ Declared using the interface keyword ✅ Can

    not have methods with implementations ✅ Classes conform to an interface by using the implements keyword ✅ A class can implement multiple interfaces ✅ Different and unrelated classes can implement the same interface ✅ Interfaces are used to capture behaviors shared by different objects
  37. ✅ Declared using the abstract keyword ✅ Can have methods

    with implementations ✅ Can have public, private, protected attributes ✅ Adopted by using the extends keywordd ✅ A class can extend only one abstract class ✅ All the classes inheriting an abstract class are in the same “family” ✅ Used to define common capabilities among subclasses but leaves the specifics of those capabilities to the subclasses ✅ Declared using the interface keyword ✅ Can not have methods with implementations ✅ Can not have public, private, protected attributes ✅ Adopted by using the implements keywordd ✅ A class can implement as many interfaces as necessary ✅ Different and unrelated classes can implement the same interface ✅ Used to capture and abstract behaviors shared by different objects Abstract classes Interfaces