1676473724176 - What are Abstract class and Interface? Difference between them?

What are Abstract class and Interface? Difference between them?
What are Abstract class and Interface? Difference between them?




In Java, an abstract class and an interface are two distinct but related concepts that allow you to define common behavior for classes.

An abstract class is a class that cannot be instantiated directly and serves as a base class for other classes. It can contain both concrete and abstract methods, and can also have instance variables and constructors. An abstract method is a method that is declared but not implemented in the abstract class, and must be implemented in the subclasses that inherit from the abstract class. An abstract class can be thought of as a partially implemented class, and it can be extended by other classes to provide a complete implementation.

Here is an example of an abstract class in Java:

public abstract class Animal {
protected int legs;

public Animal(int legs) {
this.legs = legs;
}

public abstract void makeSound();

public void walk() {
System.out.println("This animal is walking on " + legs + " legs.");
}
}

In this example, Animal is an abstract class that has an abstract method makeSound() and a concrete method walk(). The legs instance variable and constructor are also included.

An interface, on the other hand, is a collection of abstract methods and constants that can be implemented by any class. It defines a contract that classes must follow in order to provide a certain functionality. An interface can only contain abstract methods, constants, and default methods (which are concrete methods with a default implementation). An interface can be thought of as a blueprint for a class, and it is used to provide a common behavior among unrelated classes.

Here is an example of an interface in Java:

public interface Vehicle {
void start();
void stop();
default void drive() {
System.out.println("This vehicle is driving.");
}
}

In this example, Vehicle is an interface that defines three methods: start(), stop(), and drive(). The drive() method has a default implementation.

The main differences between an abstract class and an interface in Java are as follows:

  1. An abstract class can have both abstract and concrete methods, while an interface can only have abstract methods (with the exception of default methods).
  2. A class can only extend one abstract class, but it can implement multiple interfaces.
  3. An abstract class can have instance variables and constructors, while an interface cannot.
  4. An abstract class is used when you want to provide a partial implementation for a class hierarchy, while an interface is used when you want to provide a common behavior among unrelated classes.

In summary, an abstract class and an interface in Java are two related but distinct concepts that allow you to define common behavior for classes. The main difference between them is that an abstract class is a partially implemented class that can be extended by other classes, while an interface is a collection of abstract methods and constants that can be implemented by any class.









Pages