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
(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.