-2

I was just wondering why cannot we have default and abstract keywords next to each other in an interface ?

public and abstract is allowed for an interface and default is public in nature when it comes to the same package. So why public abstract and not default abstract ?

Note: This is for lower versions of java 7

Jayanth
  • 506
  • 4
  • 15
  • refer this : https://stackoverflow.com/questions/16164902/what-is-the-default-access-modifier-in-java – User123456 Oct 05 '18 at 07:17
  • 4
    An `abstract` method **cannot** have a body; a `default` method **must** have a body. So how could it be possible for a method to be both? And what would that even _mean_? – Kevin Anderson Oct 05 '18 at 07:17
  • https://stackoverflow.com/a/52659979/8682068 – RubenDG Oct 05 '18 at 07:18
  • With the method, default used when you are providing an implementation if Implementing class does not want to implement . and abstract means you are delegating to implementing class to provide an implementation. both together does not make any logical conclusion. – Pandey Amit Oct 05 '18 at 07:21
  • Hi @KevinAnderson, I am referring default as default access modifier not the default method introduced in Java8. – Jayanth Oct 05 '18 at 07:41
  • 1
    `default` is not an access modifier; it has nothing to do with default access. – user2357112 supports Monica Oct 05 '18 at 07:47
  • 1
    If an access modifier is omitted, then it is sometimes referred to as "default" access. I guess that's where the confusion comes from. But those concepts are **completely unrelated**. – MC Emperor Oct 05 '18 at 07:54
  • thank you @MCEmperor for clarifying – Jayanth Oct 05 '18 at 08:11
  • This "default" access ([JLS](https://docs.oracle.com/javase/specs/jls/se11/html/jls-6.html#jls-6.6.1): "If none of the access modifiers `public`, `protected`, or `private` are specified") is called [package access](https://docs.oracle.com/javase/specs/jls/se11/html/jls-6.html#d5e10045) – LuCio Oct 05 '18 at 08:29
  • Let me ask counterquestion: why do you want to let abstract method from interface be package-private? If we allow it, such method would only be visible in same package which interface was defined in, so classes from outside of that package wouldn't be able to fully implement that interface so would need to be abstract. But even if we extend such abstract classes they still can be *fully* implemented only by classes form same package which holds that interface. So instead of that you can simply make entire interface package-private so it could be used only within package it was created. – Pshemo Oct 05 '18 at 13:25

3 Answers3

1

It seem you misconstrued something here.

in an interface, all methods without body are considered abstract method.

default method is new feature with java 8,

It's purpose to solve compatible with old java version and help you to define a default method.

whenever you want to use default, you must provide a method with a body implementation.

More information: http://ocpj8.javastudyguide.com/ch04.html

huy
  • 717
  • 4
  • 8
  • hey @huy, I am referring to default as access modifier not the latest default method implementation in java8 – Jayanth Oct 05 '18 at 07:39
  • Your content might mislead readers, when you declare interface and default, developer often think about default method, not default access modifiers. By the way, "The methods defined in an interface are by default PUBLIC and ABSTRACT, the compiler will treat them as such even if you don't specify it." You can read above article. It explain very detail about interface – huy Oct 05 '18 at 07:47
  • I went through the article and I understand that the compiler takes public abstract as default. My question is why not default abstract. Why cannot we use them together ? I did not get this part. – Jayanth Oct 05 '18 at 07:57
  • if you are talking about access modifier. even you can not put default before method which doesn't have any access modifier. the default is called here for package level access modifier. There no such default access modifier. Package level access modifier is interpreted as default if no access modifier . by the way, "abstract" is not access modifier. Access Modifier are : public, private, protected, package -> default : – Pandey Amit Oct 05 '18 at 07:59
  • why not "default abstract class"? i interpret it like "why not "only abstract instead of public abstract class. It depend on your purpose, but often, we design the abstract or interface for other class can extends or implements. So it should be public, but as i said, it depend on your purpose. – huy Oct 05 '18 at 10:14
1

The keywords are mutually exclusive. The JLS says:

It is a compile-time error if an interface method declaration has more than one of the keywords abstract, default, or static.

Regarding the keyword abstract it says:

It is a compile-time error if an interface method declaration is abstract (explicitly or implicitly) and has a block for its body.

But for methods with the keyword default it requires:

Its body is always represented by a block, which provides a default implementation for any class that implements the interface without overriding the method.

Summarizing:

  • an abstract method has no body
  • a default method provides a body

It's not possible to have the one and the other at the same time.

LuCio
  • 4,285
  • 1
  • 15
  • 29
0

(Before Java8) The confusion that happens is the word "default" access modifier. When there is no access modifier specified for a method, it is termed to be default but there is no such term for earlier versions of Java 8. We just term that method to be in default scope but there is no such keyword as default for access modifiers.

package com.stackOverflow;

import java.util.HashMap;

public abstract class Student {

    private String name ;
    private String address;
    private HashMap<Integer, Integer> testMarks;


    public Student(String name,String address) {
        this.name = name;
        this.address = address;
        testMarks.put(1, 0);
        testMarks.put(2, 0);
        testMarks.put(3, 0);

    }

     abstract void def();// no access modifier and hence it is in default state
}

package com.stackOverflow.Interface;

import com.stackOverflow.Student;

public class anumal extends Student {

    public anumal(String name, String address) {
        super(name, address);
        // TODO Auto-generated constructor stub
    }

    public void def() {
        System.out.println("hi");
    }

}

In the above example, bot the classes are in different packages and hence we cannot override the method def() as it is not in scope.

Jayanth
  • 506
  • 4
  • 15