DEV Community

Cover image for Java Quickie. Abstract classes vs interfaces
Tristan Elliott
Tristan Elliott

Posted on • Edited on

Java Quickie. Abstract classes vs interfaces

Introduction

  • This series is going to be dedicated to the basic understanding of Java. When ever I find myself asking, "How does this work ?". I will create a blog post and put it here. This series will not be in order so feel free to read what ever post you find most relevant.

  • All the information I used for this blog post can be found Here

Abstract methods and classes

  • An abstract class is a class that has the abstract keyword in its declaration and it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub classed.
  • A abstract method is a method that is declared without an implementation (no method body) and is followed by a semi colon, for example:
 abstract void abstractMethod(int num);
Enter fullscreen mode Exit fullscreen mode
  • If a class includes an abstract method, then the class itself must be declared abstract. When an abstract class is sub classed, the subclass has to provide implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

Abstract classes compared to Interfaces

  • Abstract classes are similar to interfaces. You can not instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with a abstract classes, you can define fields that are not static and final, and define public, protected and private concrete methods. With interfaces, all fields are automatically public, static and final, all methods that we declare are public, for example:
public interface TestingInterface{
    public static final int nine =9;
    private int ten = 10;

    public abstract void publicVoidMethod();
    private void privateMethod();
     void normalMethod(int another);
}

Enter fullscreen mode Exit fullscreen mode
  • If you copy and paste the code above into your Java IDE of choice, notice the warnings and errors. First, lets look at the warnings, public static final int nine =9;, gives us a warning stating the modifiers are redundant. This verifies to us that all variables inside of an interface are already defined as public, static, and final. The second warning is from the publicVoidMethod() method, again stating that the modifiers are redundant because every method inside of an interface is automatically declared as public and abstract.

  • Now we can look at the errors our code is giving us. The errors are coming from the variable ten and the method privateMethod(), stating that the private modifier is not allowed. This error comes from access modifiers(private and protected) and proves that we can not change the access modifiers inside of an interface. So by default all methods are declared public and abstract and all variables are declared public, static and final.

  • The only modifiers that are allowed on methods are the static and default modifiers. However, I won't explain them here because they deserve a whole post to themselves.

Why is everything public in an interface?

  • The main reason is interfaces are meant for public consumption. The whole point of an interface is to provide and enforce a public contract between any class that implements the interface. If you want to hide methods or member variables, then you should consider using an abstract class.

Abstract class

public abstract class TestingInterface{
    public static final int nine =9;
    private int ten = 10;

    public abstract void publicVoidMethod();
    private void privateMethod();
     void normalMethod(int another);
}
Enter fullscreen mode Exit fullscreen mode
  • Now to switch our interface over to an abstract class, we simply change interface to abstract class. Notice that we get no warnings and only two errors from privateMethod() and normalMethod(), both have the same error, Missing method body, or declare abstract. To remedy the errors all we have to do is declare both methods as abstract.
     private abstract void privateMethod();
     abstract void normalMethod(int another);
Enter fullscreen mode Exit fullscreen mode
  • As you will notice we still get one more error, Illegal combination of modifiers: 'abstract' and 'private'. Obviously a private method can not be abstract. If we had a base class that contained a private abstract method, we could never implement the abstract method because of its private modifier. To get rid of the error, simple remove the private from the privateMethod().
abstract void privateMethod();
Enter fullscreen mode Exit fullscreen mode

When to use an Abstract class

  • Below are 3 instances that if you ever come across you should consider using an abstract class over an interface.

1) You want to share code among several closely related classes.

2) Classes that extend our abstract class have many common methods or fields, or require access to modifiers other than public(private, protected).

3) You want to declare non-static or non-final fields. This enables us to define methods that can access and modify the state of an object to which they belong.

When to use an Interface

  • Below are 3 instances that if you ever come across you should consider using an interface over an abstract class.

1) You expect that unrelated classes would implement the interface.

2) You want to specify the method signature but are not concerned about how the methods behaviour is implemented

3) You want to take advantage of multiple inheritance.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Top comments (0)