Java classes and interfaces are essential building blocks in Java programming. Here’s a simple explanation of both:
Java Classes
A class in Java is a blueprint or template used to create objects (instances). It contains:
- Fields (Instance Variables): To store the state of objects.
- Methods: To define behaviors or functionalities of the objects.
- Constructors: To initialize objects.
Key Characteristics of a Class:
- It can extend (inherit from) another class (single inheritance).
- It can implement multiple interfaces.
- Commonly used for defining real-world entities with their properties and behaviors.
Example of a Class:
public class Animal {
// Fields
private String name;
private int age;
// Constructor
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
// Method
public void speak() {
System.out.println(name + " says hello!");
}
// Getter
public String getName() {
return name;
}
}
How to use a class:
public class Main {
public static void main(String[] args) {
Animal dog = new Animal("Buddy", 3);
dog.speak(); // Output: Buddy says hello!
}
}
Java Interfaces
An interface in Java is a contract that defines a set of methods that a class must implement. Interfaces do not provide implementation but only the method declarations (method signatures).
Key Characteristics of Interfaces:
- A class that implements an interface must provide concrete implementations for all of its methods.
- A class can implement multiple interfaces (unlike inheritance where a class can extend only one class).
- Interfaces in Java 8+ can have:
- Default Methods: Methods with a default implementation.
- Static Methods: Methods that belong to the interface and can be invoked without an instance.
Example of an Interface:
public interface AnimalBehavior {
void eat(); // Abstract method
void sleep();
}
Implementing an Interface:
public class Dog implements AnimalBehavior {
@Override
public void eat() {
System.out.println("The dog is eating.");
}
@Override
public void sleep() {
System.out.println("The dog is sleeping.");
}
}
How to use the class with interface:
public class Main {
public static void main(String[] args) {
AnimalBehavior myDog = new Dog();
myDog.eat(); // Output: The dog is eating.
myDog.sleep(); // Output: The dog is sleeping.
}
}
Differences Between Classes and Interfaces
Feature | Class | Interface |
---|---|---|
Purpose | Blueprint for creating objects. | Contract defining behavior (method signatures). |
Inheritance | Supports single inheritance. | Can be implemented by multiple classes. |
Access Modifiers | Methods can have different access modifiers. | Methods are public by default (abstract). |
Implementation | Contains method implementations. | No method implementations (except default/static in Java 8+). |
Usage | Used for defining states and behaviors. | Provides abstraction and enforces contract. |
Summary:
- Use classes to define what something is and its behavior.
- Use interfaces to define what something can do (define a contract for behavior).