Now Reading: What is abstract vs interface?

Loading

What is abstract vs interface?

svgMarch 9, 2023Csharp(C#)leetcode

What is Abstract vs Interface?

When it comes to object-oriented programming, abstraction is an important concept. Abstraction is the process of hiding the implementation details from the user and showing only the functionality. In Java, abstraction is achieved by using Abstract classes and Interfaces. Both of these concepts are used to have abstraction but there are some differences between them. In this article, we will explore the differences between abstract classes and interfaces in Java.

What is an Abstract Class?

An abstract class is a class that contains an abstract keyword on the declaration. An abstract class is used to provide a common definition of a base class that multiple derived classes can share. It is used to define the basic structure of a class and is partially implemented. An abstract class can contain both abstract and non-abstract methods. Abstract methods are methods that are declared but not implemented. They are implemented by the derived classes.

Example of an Abstract Class

Let’s take an example of an abstract class Animal.

public abstract class Animal {

public abstract void eat();
public abstract void sleep();
public void run() {
System.out.println(“Animal is running”);
}
}

In the above example, the Animal class is an abstract class. It contains two abstract methods eat() and sleep() and one non-abstract method run(). The eat() and sleep() methods are abstract methods as they are declared but not implemented. The run() method is a non-abstract method as it is both declared and implemented.

What is an Interface?

An interface is a sketch that is used to implement a class. It is used to provide a common definition of a set of related methods with empty bodies. An interface can contain only abstract methods. It is used to achieve abstraction and multiple inheritance in Java.

Example of an Interface

Let’s take an example of an interface Animal.

public interface Animal {

public void eat();
public void sleep();
public void run();
}

In the above example, the Animal interface is used to implement the Animal class. It contains three abstract methods eat(), sleep() and run(). All the methods are abstract methods as they are declared but not implemented.

Differences between Abstract Class and Interface

Now that we have a basic understanding of abstract classes and interfaces, let’s explore the differences between them.

1. Definition

The first difference between abstract classes and interfaces is their definition. An abstract class is a class that contains an abstract keyword on the declaration. It is used to provide a common definition of a base class that multiple derived classes can share. An interface is a sketch that is used to implement a class. It is used to provide a common definition of a set of related methods with empty bodies.

2. Methods

The second difference between abstract classes and interfaces is the methods they contain. An abstract class can contain both abstract and non-abstract methods. An interface can contain only abstract methods.

3. Multiple Inheritance

The third difference between abstract classes and interfaces is multiple inheritance. An abstract class can extend only one class whereas an interface can extend multiple interfaces.

4. Implementation

The fourth difference between abstract classes and interfaces is the implementation. An abstract class can provide the implementation of some of its methods whereas an interface can’t provide the implementation of any of its methods.

Conclusion

In conclusion, abstract classes and interfaces are both used to have abstraction in Java. However, there are some differences between them. An abstract class is a class that contains an abstract keyword on the declaration. It can contain both abstract and non-abstract methods. An interface is a sketch that is used to implement a class. It can contain only abstract methods. An abstract class can extend only one class whereas an interface can extend multiple interfaces. An abstract class can provide the implementation of some of its methods whereas an interface can’t provide the implementation of any of its methods.

svg

What do you think?

Show comments / Leave a comment

Leave a reply

Loading
svg
Quick Navigation
  • 01

    What is abstract vs interface?