Interfaces and abstract classes are the essential components for achieving abstraction and polymorphism.
An interface in Java is a reference type, similar to a class, that can contain only abstract methods, static methods, default methods, and static final variables (constants). Interfaces are used to achieve abstraction and multiple inheritance in Java. Interfaces may not be directly instantiated.
?Before Java 8, interfaces could have only abstract methods.
The implementation of these methods has to be provided in a separate class. So, if a new method is to be added in an interface, then its implementation code has to be provided in the class implementing the same interface.
?To overcome this issue, Java 8 has introduced the concept of default methods which allow the interfaces to have methods with implementation without affecting the classes that implement the interface.
Default methods can be overridden by implementing classes if necessary.
An abstract class in Java is a class that cannot be instantiated on its own and may contain abstract methods (methods without a body) and concrete methods (methods with a body). Abstract classes are used to provide a common base for subclasses, allowing for code reuse and the definition of shared behavior.
Java supports only single inheritance, meaning each class can inherit fields and methods of only one class. If you need to inherit properties from more than one source, Java provides the concept of interfaces, which is a form of multiple inheritance.
?Interfaces are similar to classes. However, they define only the signature of the methods and not their implementations. The methods that are declared in the interface are implemented in the classes. Multiple inheritance occurs when a class implements multiple interfaces.
In Java, multiple inheritance is achieved through interfaces rather than classes. This allows a class to implement multiple interfaces, inheriting the method signatures from each of them. Below is an example demonstrating multiple inheritance using interfaces.
Let's define two interfaces, Flyable and Swimmable, and a class Duck that implements both interfaces.
public interface Flyable { void fly(); }
public interface Swimmable { void swim(); }
public class Duck implements Flyable, Swimmable { @Override public void fly() { System.out.println("Duck is flying"); } @Override public void swim() { System.out.println("Duck is swimming"); } public static void main(String[] args) { Duck duck = new Duck(); duck.fly(); duck.swim(); } }
Interfaces:
Class:
Main Method:
Duck is flying Duck is swimming
Here's a simple diagram to illustrate the relationship:
+----------------+ | Flyable |<--------------->Interface |----------------| | + fly() | +----------------+ ^ | | Implements | +----------------+ | Duck |<--------------->Class |----------------| | + fly() | | + swim() | +----------------+ ^ | | Implements | +----------------+ | Swimmable |<--------------->Interface |----------------| | + swim() | +----------------+
In this example, the Duck class demonstrates multiple inheritance by implementing both the Flyable and Swimmable interfaces. This allows the Duck class to inherit and provide implementations for the methods defined in both interfaces, showcasing how Java achieves multiple inheritance through interfaces.
Abstract classes in Java are used to provide a common base for a family of related classes. They can contain both abstract methods (methods without a body) and concrete methods (methods with a body). Below is an example demonstrating the use of an abstract class.
Let's define an abstract class Animal and two subclasses Dog and Cat that extend the Animal class.
public abstract class Animal { // Abstract method (does not have a body) public abstract void makeSound(); // Concrete method (has a body) public void sleep() { System.out.println("The animal is sleeping"); } }
public class Dog extends Animal { @Override public void makeSound() { System.out.println("Dog says: Woof!"); } public static void main(String[] args) { Dog dog = new Dog(); dog.makeSound(); dog.sleep(); } }
public class Cat extends Animal { @Override public void makeSound() { System.out.println("Cat says: Meow!"); } public static void main(String[] args) { Cat cat = new Cat(); cat.makeSound(); cat.sleep(); } }
Abstract Class: Animal
Subclass: Dog
Subclass: Cat
For the Dog class:
public interface Flyable { void fly(); }
For the Cat class:
public interface Swimmable { void swim(); }
Here's a simple diagram to illustrate the relationship:
public class Duck implements Flyable, Swimmable { @Override public void fly() { System.out.println("Duck is flying"); } @Override public void swim() { System.out.println("Duck is swimming"); } public static void main(String[] args) { Duck duck = new Duck(); duck.fly(); duck.swim(); } }
In this example, the Animal abstract class provides a common base for the Dog and Cat subclasses. The Animal class defines an abstract method makeSound() that must be implemented by any subclass, and a concrete method sleep() that provides a default implementation. The Dog and Cat classes extend the Animal class and provide their own implementations of the makeSound() method.
Duck is flying Duck is swimming
Interfaces are commonly used to define APIs, frameworks, and libraries. For example, the java.util.List interface provides a contract for list implementations, such as ArrayList and LinkedList.
+----------------+ | Flyable |<--------------->Interface |----------------| | + fly() | +----------------+ ^ | | Implements | +----------------+ | Duck |<--------------->Class |----------------| | + fly() | | + swim() | +----------------+ ^ | | Implements | +----------------+ | Swimmable |<--------------->Interface |----------------| | + swim() | +----------------+
Abstract classes are often used to provide a base class for a family of related classes. For example, the java.util.AbstractList class provides a skeletal implementation of the List interface, reducing the amount of code that subclasses need to implement.
public interface Flyable { void fly(); }
SNo | Interface | Abstract Class |
---|---|---|
1 | Interfaces cannot be instantiated | Abstract classes cannot be instantiated |
2 | It can have both abstract and non-abstract methods | It can have both abstract and non-abstract methods |
3 | In interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public | In abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods |
4 | Interface supports multiple inheritance. Multiple interfaces can be implemented | Abstract class or class can extend only one class |
5 | It is used if you expect that unrelated classes would implement your interface. Eg, the interfaces Comparable and Cloneable are implemented by many unrelated classes | It is used if you want to share code among several closely related classes |
6 | It is used if you want to specify the behavior of a particular data type, but not concerned about who implements its behavior. | It is used if you expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private) |
Ref: https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
According to Joshua Bloch, author of "Effective Java," interfaces are preferred over abstract classes for defining types because they are more flexible and support multiple inheritance. However, abstract classes are useful for providing shared functionality and reducing code duplication.
"Interfaces are ideal for defining mixins. Classes, by contrast, are ideal for defining objects that have intrinsic properties."
- Joshua Bloch
Explore the power of interfaces and abstract classes in your own Java projects. Experiment with defining contracts using interfaces and providing shared functionality using abstract classes. Share your insights and experiences with the Java community to contribute to the collective knowledge and growth.
Any corrections or additions to this post are welcome.
public interface Flyable { void fly(); }
The above is the detailed content of Interfaces and Abstract Classes in Java. For more information, please follow other related articles on the PHP Chinese website!