-1

If we are not adding any access specifier to method by default it is of default type.

but we are adding default keyword then it is giving me error, like we can have default method in interface only. I am aware about the default method in functional interface but strange why it is showing error in eclipse.

public class Test
{
    
    default void test() { //Default methods are allowed only in interfaces.
        
    }
}

this one is working fine

public class Test
{
    void test()
    {
    }
}

any reason for it?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
sparsh610
  • 1,552
  • 3
  • 27
  • 66
  • 6
    A `class` cannot have a `default` method, just `interface`s can... The comment in your first example literally says this! – deHaar Jul 06 '20 at 13:58
  • Default methods are only applicable to interfaces – ZhekaKozlov Jul 06 '20 at 13:59
  • Why do you think it is strange? As you said, default method can be declared in interfaces only - declaring it normal class results in an error. That is something I'd expect. – Amongalen Jul 06 '20 at 14:00
  • Is this relevant to your question ? https://stackoverflow.com/questions/52620127/java-define-a-explicit-package-private-modifier – Ori Marko Jul 06 '20 at 14:00
  • 4
    Why is it strange to indicate an error when there's an error? – Dave Newton Jul 06 '20 at 14:00
  • Ah, now I understand the confusion. – Dave Newton Jul 06 '20 at 14:04
  • Note that the `default` keyword as used in interfaces does not have anything to do with access specifiers. It does not mean "default access specifier" but "default implementation". – Jesper Jul 06 '20 at 14:11
  • So default keyword came after java 8 or we were using it somewhere before ? Because we didnt have default method before java 8 – sparsh610 Jul 06 '20 at 14:15
  • strange, if someone not aware about something, you will down-vote and close the same. – sparsh610 Jul 06 '20 at 17:13

1 Answers1

6

The default keyword has nothing to do with default 'package private' access that derives from not specifying the access. The default keyword only applies to interfaces (and not just to functional interfaces), to supply a default implementation in the interface. This is a feature introduced in Java 8 to allow for easier interface evolution.

Overloading the default keyword to mean 'package private' access when used in classes would only be confusing and serve no real value, as the same is achieved by not specifying access.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I didn't get it. Let me understand that by asking questions. What is access specifier for void test(){} – sparsh610 Jul 06 '20 at 14:10
  • @sparsh610: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html - There is no access modifier specified, therefor it has the "packave-private" modifier. – OH GOD SPIDERS Jul 06 '20 at 14:17
  • @sparsh610 There is no access specified in that case, so it gets the default access, which is 'package private'. The word 'default' in that case has no relation to the `default` keyword. Similarly, in interfaces, you can use the access specified `public`, but as it is the only valid access specifier, not including the access specifier in interfaces results in the default access, which is 'public'. – Mark Rotteveel Jul 06 '20 at 14:17
  • So can i conclude that default keyword came from java 8 – sparsh610 Jul 06 '20 at 14:20
  • @sparsh610 Yes, the `default` keyword was introduced in Java 8, and the only meaning it has is that the method in the interface defines not just the method, but also its default implementation. – Mark Rotteveel Jul 06 '20 at 14:20
  • Thank you so much. Although it was a small concept and still after having a good experience in Java, i was not aware of same. – sparsh610 Jul 06 '20 at 14:24