Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding default interface method with abstract method

I find it weird and was wondering if it is something that is regularly used. When can it be useful?

public interface InterA { 
   Object getInfo() throws Exception1; 
}
public interface InterB {
   public default Integer getInfo(Object s) {return 67;} 
}

public interface InterC extends InterA, InterB {
   @Override public abstract Integer getInfo(Object s);
}
like image 648
Stanko Avatar asked Sep 01 '15 17:09

Stanko


People also ask

Can you override default method in interface?

A default method cannot override a method from java.

Can an abstract class override default methods?

An abstract class can override Object class methods, but an interface can't. An abstract class can declare instance variables, with all possible access modifiers, and they can be accessed in child classes. An interface can only have public, static, and final variables and can't have any instance variables.

Can abstract class override interface?

An abstract class permits you to make functionality that subclasses can implement or override whereas an interface only permits you to state functionality but not to implement it. A class can extend only one abstract class while a class can implement multiple interfaces.

Do you need to override default methods?

It is not mandatory to override the default method in Java.


1 Answers

This exists before default interface methods. For example, an abstract class can make

    @Override
    abstract public int hashCode();

forcing subclasses to provide implementations for hashCode, possibly because of additional requirements imposed by the abstract class.

like image 152
ZhongYu Avatar answered Oct 05 '22 01:10

ZhongYu