-4

What is the difference between abstract methods and default methods?

I know this question has been answered before, but not in the way Aristocrates has.

BEFORE KNOWING THE ANSWER FROM "Aristocrates", I thought default methods are different from defender methods, and that - default methods are nothing but the abstract methods(without keyword abstract) inside an interface. These two wrong conceptions led this foolish question to be asked. I never meant to trouble the folks by asking an already "well asked and well answered" question.

I wanted to delete this question but I haven't because of two reasons - 1.stackoverflow prevented me, 2.Aristocrates' answer is superb.

Neil
  • 41
  • 6
  • Both are in interface, but default method has an implementation. Simple as that. – MightyPork Aug 12 '14 at 16:48
  • 6
    "Sorry if this question has been asked before by anyone else" So you didn't even spend a second on searching for it before asking? – flotothemoon Aug 12 '14 at 16:49
  • http://stackoverflow.com/questions/19998454/interface-with-default-methods-vs-abstract-class-in-java-8 ? – Suresh Atta Aug 12 '14 at 16:50
  • 3
    They need to bring back "lacks minimal understanding" – user1231232141214124 Aug 12 '14 at 16:53
  • how it is still open? – SparkOn Aug 12 '14 at 16:54
  • @redFIVE Oh yeah.. but minimal understanding of what? Of how to google? Or how to search for questions? – flotothemoon Aug 12 '14 at 16:54
  • The thing is that I had a wrong conception of default methods - I thought default methods are different from defender methods, and that - default methods are nothing but the abstract methods(without keyword abstract) inside an interface. These two wrong conceptions led this foolish question to be asked. I never meant to trouble the folks by asking an already "well asked and well answered" question. – Neil Aug 13 '14 at 02:36

1 Answers1

4

Abstract methods

An abstract method is a method that does not have a method body. Essentially, they are just method declarations and consist only of the method signature.

Example:

public abstract void hello(int i);

Abstract methods can be in either interfaces or abstract classes. Note that in an interface, methods are abstract by default, and thus the abstract modifier is usually left out.

Default methods

A default method is a method in an interface that has a "default" implementation provided. In other words, a non-abstract class implementing the interface would not have to override a default method defined in the interface.

Example:

default public void otherHello(int i) {
    System.out.println("Hello");
}

Example

Here is a valid example of an interface with an abstract and default method and a class implementing the interface:

public interface Hello {

    public abstract void hello(int i);

    default public void otherHello(int i) {
        System.out.println("Hello");
    }
}

public class Hi implements Hello {

    @Override
    public void hello(int i) {
        System.out.println("Only have to implement this method");
    }
}
Community
  • 1
  • 1
Nick Meyer
  • 1,771
  • 1
  • 17
  • 29