can abstract method do System.out.println?

240 Views Asked by At

I am a beginner in Java. I have a question about the abstract method in Interface. Is the following sampleMethod() an abstract method? My understanding is that it is not an abstract method due to System.out.println. Am I correct?

default void sampleMethod() {
            System.out.println("Am I abstract method?");
}

Thanks in advance!

1

There are 1 best solutions below

0
xthe_white_lionx On

The sampleMethod() isn't an abstract method it is a default methode, which means that you can call this Methode at any implementation, because this is the default methode which will be called. Furthermore you can override this methode in your implementation but if you do not it wil simply call your default methode from the interface. So you don't have to override/implement it in your implementation class.

But keep in mind that even with a default methode in your interface you can't manage any variables if you wont to do this use an abstract class like this

public abstract class SomeAbstractClass{
    String thing;

    protected SomeAbstractClass(String thing) {
        this.thing = thing;
    }

    void sampleMethod() {
        System.out.println(thing);
        //or do other stuff
    }

    abstract void otherMethode();
}