Learning Java: Abstracts vs Interfaces

In the past every time I heard about abstract class and interfaces I had this feeling of something kind of useless because I rarely used it or didn’t know the meaning of using it. Now that I know how useful can be both, it is changing my perspective about them

Let’s talk about abstract class, we already knows it is a class that is declared as abstract but what does it means? What are the differences between them and Interfaces? and How to get access to them?

Abstract class are known for its diversity of holding abstract and non-abstract methods. It can have constructors and it methods can be overridden.

As we can see in the example below an abstract class called human is created. This class holds one abstract method called “Action” and two non-abstract methods called “age” and “clothing” which takes parameters and have behavior.

abstract class human {
    public abstract void Action();

    String studentName;
    int actualYear = 2019;

    public int age(int yearOfBorn) {
        System.out.println(studentName + " is " + (actualYear - yearOfBorn) + " years old");
        return actualYear - yearOfBorn;
    }

    public String clothing(String Day) {
        String result = "";
        if ((Day.equals("Monday") || Day.equals("Tuesday") || Day.equals("Wednesday") || Day.equals("Thursday"))) {
            result = studentName + " is wearing button shirt";
        } else if (Day.equals("Friday")) {
            result = studentName + " is wearing T-shirt";
        } else {
            result = studentName + " is on weekend mood";
        }
        System.out.println(result);
        return result;
    }
}

Unlike abstract classes, interfaces only can hold abstract methods, it doesn’t provide access modifiers; which means everything is public.

interface Human_Behavior {
    public abstract void happy();
    public abstract void Dance(String what);
}

Abstract class can be accessed from others classes if and only if the class extends to the abstract class, as well the interfaces have to be implemented to gain access, we can see that in the code below.

    class Student extends human implements Human_Behavior {
        int yearOfBorn;

        @Override
        public void Action() {
            System.out.println(studentName + " is Coding");
        }
        // Create a constructor
        public Student(String studentName) {
            this.studentName = studentName;
        }
        // The method MUST be called in this class
        @Override
        public void happy() {
            System.out.println(studentName + " is feeling happy.");
        }
        @Override
        public void Dance(String what) {
            System.out.println(studentName + " is dancing " + what);
        }
    }
    public class AbstractDemo { 
        public static void main(String[] args) {
            Student donelys = new Student("Donelys");
            donelys.Action();
            donelys.age(1994);
            donelys.clothing("Friday");
            donelys.happy();
            donelys.Dance("Bachata");
        }
    }
This image shows the result of the code below

As we see above, unlike method from abstract classes, method from interfaces MUST be in the class that is implemented, you can override them and add some behavior and then instanciate an object from the class to gain control over such behaviors.

After a research about abstracts classes and interfaces, I could notice how organized could looks our code when we are using them, it is like having a skeleton class from where you summon methods which make our coding life much simple.

If you want to know more about abstract class or interfaces check these sources: Abstract class , Interfaces, Abstract vs Interface

Join the Conversation

  1. Unknown's avatar

1 Comment

Leave a comment

Design a site like this with WordPress.com
Get started