✅ Differences Between Classes and Interfaces in Java
Aspect | Classes | Interfaces |
---|---|---|
Definition | A blueprint for creating objects, containing methods and variables. | A blueprint that defines abstract methods and constants. |
Inheritance | Supports single inheritance only. | Supports multiple inheritance (A class can implement multiple interfaces). |
Implementation | Provides implementation for methods. | Contains only method declarations (until Java 7) and also supports default , static , and private methods (from Java 8 onward). |
Access Modifiers | Methods can have any access modifier (public , private , protected ). |
Methods are public and abstract by default. Variables are public , static , and final . |
Constructors | Can have constructors. | Cannot have constructors. |
Instantiation | Can be instantiated if not abstract . |
Cannot be instantiated directly. |
Usage | Used to define objects with properties and behaviors. | Used to specify what a class must do but not how it does it. |
Variables | Can be static , final , or non-final. |
All variables are public , static , and final by default. |
final Keyword |
Classes can be declared final . |
Interfaces cannot be declared final (but methods in interfaces can be final when static or default ). |
Abstract Keyword | Not necessary unless the class is abstract . |
Always abstract before Java 8; supports default and static methods from Java 8 onward. |
Multiple Inheritance | Not supported. | Supported via implementing multiple interfaces. |
✅ Example Demonstrating Differences
Class Example
class Animal { // Regular class
private String name;
Animal(String name) {
this.name = name;
}
public void display() {
System.out.println("Animal: " + name);
}
}
Interface Example
interface Drawable { // Interface
void draw(); // Abstract method
default void description() { // Default method (Java 8+)
System.out.println("This is a drawable object.");
}
}
Implementing Interface in Class
class Circle implements Drawable {
public void draw() { // Implementing the interface method
System.out.println("Drawing a circle.");
}
}
Main Class
public class Main {
public static void main(String[] args) {
Animal animal = new Animal("Lion");
animal.display();
Drawable drawable = new Circle();
drawable.draw();
drawable.description(); // Calling default method from interface
}
}
Output
Animal: Lion
Drawing a circle.
This is a drawable object.
✅ Extending Interfaces
An interface can extend one or more interfaces using the extends
keyword, achieving interface inheritance. This is useful for grouping related interfaces under a common name.
📌 Example of Extending Interfaces
interface Printable {
void print();
}
interface Showable extends Printable { // Extending interface
void show();
}
class Demo implements Showable { // Implementing extended interface
public void print() {
System.out.println("Printing...");
}
public void show() {
System.out.println("Showing...");
}
}
public class Main {
public static void main(String[] args) {
Demo obj = new Demo();
obj.print();
obj.show();
}
}
Output
Printing...
Showing...
🔍 Explanation of Interface Extension
Showable
extendsPrintable
, meaning any class implementingShowable
must also implement the methods ofPrintable
.- Java allows interfaces to extend multiple interfaces, providing a way to group functionalities.
✅ Key Differences Between Extending a Class and Extending an Interface
Aspect | Extending a Class | Extending an Interface |
---|---|---|
Inheritance | Only one class can be extended (single inheritance). | Multiple interfaces can be extended. |
Access Modifiers | Can inherit all public and protected members. | Can inherit only abstract methods unless they are default or static . |
Purpose | To extend functionality of a single class. | To provide additional functionality by combining multiple interfaces. |
Implementation Requirement | Can provide implementation. | Cannot provide implementation unless using default or static methods. |
No comments:
Post a Comment