0Pricing
Java Academy · Lesson

Interfaces in Practice

Use interfaces as types, pass them to methods, and swap implementations easily. Practice small patterns for flexible code.

Interfaces in Practice is a free Java Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Overview

Interfaces in practice: treat capabilities as types and keep callers independent from concrete classes. You can pass an interface to a method, store it in a list, and replace it with a different implementation when needed.

Interface as parameter

Methodlar interface türünü parametre olarak alırsa esneklik kazanır:

  • Çağıran kod sabit kalır.
  • Farklı sınıflar aynı sözleşmeyi uygulayıp kullanılabilir.
  • Testte Fake veya Stub takıp hızla deneyebilirsin.

Code: Printable capability

Yöntem, somut sınıfı değil Printable sözleşmesini kullanıyor; uygulamalar değişse de çağıran kod aynı kalır.

public class Main {
  // 1) Define a tiny capability
  static interface Printable { void print(); }

  // 2) Two implementations
  static class TextDoc implements Printable {
    public void print() { System.out.println("[TextDoc] Printing text pages"); }
  }
  static class Photo implements Printable {
    public void print() { System.out.println("[Photo] Printing a photo"); }
  }

  // 3) A method that depends on capability, not class
  static void runJob(Printable p) {
    p.print(); // caller does not care which class it is
  }

  public static void main(String[] args) {
    runJob(new TextDoc());
    runJob(new Photo());
  }
}

Interface as return type

Bazen bir factory metodu bir interface döndürür. Çağıran yalnızca yetenekleri görür; gerçek sınıfı bilmesine gerek yoktur. Bu da bağımlılığı azaltır ve modülerliği artırır.

Code: Strategy with Formatter

Aynı metot, farklı Formatter uygulamalarıyla çalışır. Sınıfı değiştirmeden davranışı değiştirebildik.

public class Main {
  // Strategy: different ways to format text
  static interface Formatter { String format(String s); }

  static class UpperFormatter implements Formatter {
    public String format(String s) { return s.toUpperCase(); }
  }
  static class LowerFormatter implements Formatter {
    public String format(String s) { return s.toLowerCase(); }
  }

  // Uses any Formatter
  static String show(String title, Formatter f) {
    return title + " -> " + f.format(title);
  }

  public static void main(String[] args) {
    System.out.println(show("Hello", new UpperFormatter()));
    System.out.println(show("Hello", new LowerFormatter()));
  }
}

Code: Interface list

Bir dizi/liste interface tipinden olabilir. Döngü tek tip çağrı yapar; her eleman kendi uygulamasını çalıştırır.

public class Main {
  static interface Task { void run(); }

  static class EmailTask implements Task { public void run(){ System.out.println("Send email"); } }
  static class ReportTask implements Task { public void run(){ System.out.println("Generate report"); } }
  static class CleanupTask implements Task { public void run(){ System.out.println("Clean temp files"); } }

  public static void main(String[] args) {
    Task[] queue = { new EmailTask(), new ReportTask(), new CleanupTask() };
    for (int i = 0; i < queue.length; i = i + 1) {
      queue[i].run(); // same call, different behavior
    }
  }
}

Interface practice check

Quick check: Why is it useful to "program to an interface"?

Recap

Recap: Interface türü ile parametre/geri dönüş/ koleksiyon kullanmak kodu esnek kılar. Uygulamaları değiştirebilir, testte sahte nesnelerle hızlıca deneyebilirsin.

Frequently asked questions

Is the “Interfaces in Practice” lesson free?

Yes — the full text of “Interfaces in Practice” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Interfaces in Practice”?

Use interfaces as types, pass them to methods, and swap implementations easily. Practice small patterns for flexible code. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Interfaces in Practice” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Interfaces in Practice
  2. Generics Basics (T, inference)
  3. Generic Methods/Classes
← Back to Java Academy