Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

Interfaces

In Java, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields or constructors. Other classes can implement interfaces, which means they agree to provide implementations for all the methods declared by the interface.

Here’s a basic example of an interface in Java:

				
					// Defining an interface
interface Animal {
    void sound(); // Method signature
}

// Implementing the interface
class Dog implements Animal {
    public void sound() {
        System.out.println("Woof");
    }
}

// Using the interface
public class Main {
    public static void main(String[] args) {
        Animal dog = new Dog();
        dog.sound(); // Outputs: Woof
    }
}

				
			

In this example, the Animal interface declares a method sound(). The Dog class implements this interface and provides an implementation for the sound() method. Then, in the Main class, we create an instance of Dog and call the sound() method through the Animal reference. This demonstrates the concept of polymorphism, where a reference of a superclass/interface type can point to a subclass object.

Interfaces are commonly used to achieve abstraction and provide a contract for classes to adhere to, ensuring a consistent API across different implementations. They are widely used in Java frameworks and APIs for defining contracts that classes must follow.

Some more examples of packages in Java:

  1. Comparable Interface: The Comparable interface is used to define a natural ordering of objects. Classes that implement this interface must provide an implementation for the compareTo() method.
				
					class Student implements Comparable<Student> {
    private int id;
    private String name;

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public int compareTo(Student other) {
        return this.id - other.id;
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student(1, "Alice");
        Student s2 = new Student(2, "Bob");

        System.out.println(s1.compareTo(s2)); // Outputs: -1
    }
}

				
			

2. Runnable Interface: The Runnable interface is used for defining a task that can be executed concurrently by a thread. It has a single run() method that must be implemented.

				
					class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Thread is running...");
    }
}

public class Main {
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        thread.start();
    }
}

				
			

3. Iterable Interface: The Iterable interface allows objects to be iterated over using the enhanced for loop (for-each loop) or with iterators. It requires implementing the iterator() method, which returns an iterator over the elements.

				
					import java.util.Iterator;

class MyIterable implements Iterable<String> {
    private String[] elements = {"Apple", "Banana", "Orange"};

    @Override
    public Iterator<String> iterator() {
        return new MyIterator();
    }

    private class MyIterator implements Iterator<String> {
        private int index = 0;

        @Override
        public boolean hasNext() {
            return index < elements.length;
        }

        @Override
        public String next() {
            return elements[index++];
        }
    }
}

public class Main {
    public static void main(String[] args) {
        MyIterable iterable = new MyIterable();
        for (String fruit : iterable) {
            System.out.println(fruit);
        }
    }
}

				
			
Scroll to Top