I have an interface and an abstract class:
interface MyInterface {int presents();};
public abstract class MyAbstractClass implements MyInterface {
abstract void presents();
abstract void chill();
}
The compiler complains that in
abstract void presents();
The return type is incompatible with
MyInterface.presents()
Offering the following options:
make it int type
make MyInterface.presents void type
Why is this happening?
You defined method presents() that returns int in Gift interface.
Then you try to redefine method presents() with return type void in class named myClass.
It is pretty obvious that int is not void. Thism means that return type is incompatible.
You are confusing with methods inheritance. The problem is that syntax of java (and all c-like languages) does not dictate usage of return value, so even if you method returns int you can still call it like following:
presents();
So, assume that compiler allows you to have 2 different methods named presents(): one that returns int, other returns void. How can compiler understand which one of the methods to use in our example? The answer is no way. This is the reason that you can override methods using different parameters signature, e.g.
presents();
presents(String str);
presents(int n);
You cannot however have 2 methods with the same parameters signature and different return type.
In the interface, presents is defined as returning an int, but in your class that claims to implement the interface, it's void. The two need to match.
Because interface having the method presents() and you are trying to change the method signature here.
If you dont want that method write new one.
abstract void anotherPresents();
or
satisfy the original signature
abstract int presents();
Related
Recently I started exploring Java 8 and I can't quite understand the concept of "functional interface" that is essential to Java's implementation of lambda expressions. There is a pretty comprehensive guide to lambda functions in Java, but I got stuck on the chapter that gives definition to the concept of functional interfaces. The definition reads:
More precisely, a functional interface is defined as any interface that has exactly one abstract method.
An then he proceeds to examples, one of which is Comparator interface:
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
}
I was able to test that I can use a lambda function in place of Comparator argument and it works(i.e. Collections.sort(list, (a, b) -> a-b)).
But in the Comparator interface both compare and equals methods are abstract, which means it has two abstract methods. So how can this be working, if the definition requires an interface to have exactly one abstract method? What am I missing here?
From the same page you linked to:
The interface Comparator is functional because although it declares two abstract methods, one of these—equals— has a signature corresponding to a public method in Object. Interfaces always declare abstract methods corresponding to the public methods of Object, but they usually do so implicitly. Whether implicitly or explicitly declared, such methods are excluded from the count.
I can't really say it better.
Another explanation is given in the #FunctionalInterface page:
Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.
You can test which interface is a correct functional interface using #FunctionalInterface.
E.g.:
this works
#FunctionalInterface
public interface FunctionalInterf {
void m();
boolean equals(Object o);
}
this generates an error:
#FunctionalInterface
public interface FunctionalInterf {
void m();
boolean equals();
}
Multiple non-overriding abstract methods found in interface FunctionalInterf
Q. But in the Comparator interface both compare() and equals() methods are abstract, which means it has two abstract methods. So how can this be working, if the definition requires an interface to have exactly one abstract method? What am I missing here?
A.
A functional interface may specify any public method defined by Object, such as equals( ),
without affecting its “functional interface” status. The public Object methods are considered implicit
members of a functional interface because they are automatically implemented by an instance of a
functional interface.
An interface cannot extend Object class, because Interface has to have public and abstract methods.
For every public method in the Object class, there is an implicit public and abstract method in an interface.
This is the standard Java Language Specification which states like this,
“If an interface has no direct super interfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.”
That's how Object class' methods are declared in an interface. And according to JLS, this does not count as interface' abstract method. Hence, Comparator interface is a functional interface.
A functional interface has only one abstract method but it can have multiple default and static methods.
Since default methods are not abstract you’re free to add default methods to your functional interface as many as you like.
#FunctionalInterface
public interface MyFuctionalInterface
{
public void perform();
default void perform1(){
//Method body
}
default void perform2(){
//Method body
}
}
If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.
Comparator is a functional interface even though it declared two abstract methods. Because one of these abstract methods “equals()” which has signature equal to public method in Object class.
e.g. Below interface is a valid functional interface.
#FunctionalInterface
public interface MyFuctionalInterface
{
public void perform();
#Override
public String toString(); //Overridden from Object class
#Override
public boolean equals(Object obj); //Overridden from Object class
}
Here is a "show me the code" approach to understanding the definition:
we shall look into OpenJDK javac for how it checks validity of classes annotated with #FunctionalInterface.
The most recent (as of July, 2022) implementation lies here:
com/sun/tools/javac/code/Types.java#L735-L791:
/**
* Compute the function descriptor associated with a given functional interface
*/
public FunctionDescriptor findDescriptorInternal(TypeSymbol origin,
CompoundScope membersCache) throws FunctionDescriptorLookupError {
// ...
}
Class Restriction
if (!origin.isInterface() || (origin.flags() & ANNOTATION) != 0 || origin.isSealed()) {
//t must be an interface
throw failure("not.a.functional.intf", origin);
}
Pretty simple: the class must be an interface and must not be a sealed one.
Member Restriction
for (Symbol sym : membersCache.getSymbols(new DescriptorFilter(origin))) { /* ... */ }
In this loop, javac goes through the members of the origin class, using a DescriptorFilter to retrieve:
Method members (of course)
&& that are abstract but not default,
&& and do not overwrite methods from Object,
&& with their top level declaration not a default one.
If there is only one method matching all the above conditions, then surely it is a valid functional interface, and any lambda will overwrite that very method.
However, if there are multiple, javac tries to merge them:
If those methods all share the same name, related by override equivalence:
then we filter them into a abstracts collection:
if (!abstracts.stream().filter(msym -> msym.owner.isSubClass(sym.enclClass(), Types.this))
.map(msym -> memberType(origin.type, msym))
.anyMatch(abstractMType -> isSubSignature(abstractMType, mtype))) {
abstracts.append(sym);
}
Methods are filtered out if:
their enclosing class is super class of that of another previously matched method,
and the signature of that previously matched method is subsignature of that of this method.
Otherwise, the functional interface is not valid.
Having collected abstracts, javac then goes to mergeDescriptors, which uses mergeAbstracts, which I will just quote from its comments:
/**
* Merge multiple abstract methods. The preferred method is a method that is a subsignature
* of all the other signatures and whose return type is more specific {#see MostSpecificReturnCheck}.
* The resulting preferred method has a thrown clause that is the intersection of the merged
* methods' clauses.
*/
public Optional<Symbol> mergeAbstracts(List<Symbol> ambiguousInOrder, Type site, boolean sigCheck) {
// ...
}
Conclusion
Functional interfaces must be interfaces :P , and must not be sealed or annotations.
Methods are searched in the whole inheritance tree.
Methods overlapping with those from Object are ignored.
default methods are ignored, unless they are later overridden by sub-interfaces as non-default.
Matching methods must all share the same name, related by override equivalence.
Either there is only one method matching the above conditions, or matching methods can get "merged" by their class hierarchy, subsignature relations, return types and thrown clauses.
The Java docs say:
Note that it is always safe not to override Object.equals(Object).
However, overriding this method may, in some cases, improve
performance by allowing programs to determine that two distinct
comparators impose the same order.
Maybe Comparator is special? Maybe, even though it's an interface, there is somehow a default implementation of equals() that calls compare()? Algorithmically, it's trivial.
I thought all methods that were declared in interfaces were abstract (i. e. no default implementation). But maybe I'm missing something.
Definition:
If an interface contains only one abstract method, then such type of interface is called functional interface.
Usage:
Once we write Lambda expression "->" to invoke its functionality ,
then in this context we require Functional Interface.
We can use the Functional Interface reference to refer Lambda
expression.
Inside functional interface we can have one abstract method and n
number of default/static methods.
Functional interface with respect to inheritance:
If an interface extends Functional interface and the child interface does not contain any abstract method , then the child interface is also considered to be Functional Interface.
Functional interface is not new to java, its already used in following interface API's:
Runnable : contains run() method only.
Callable : contains call() method only.
Comparable : contains compareTo() method only.
Before Java 8, an interface could only declare one or more methods also known as Abstract Method (method with no implementation, just the signature). Starting with Java 8 an interface can also have implementation of one or more methods (knows as Interface Default Method) and static methods along with abstract methods. Interface Default Methods are marked default keyword.
So the question is, what is Functional Interface?
An interface with Single Abstract Method (SAM) is called Functional Interface.
Which means -
An interface with Single Abstract Method is a Functional Interface
An interface with Single Abstract Method and zero or more default
methods and zero or more static method is also a valid Functional
Interface.
More detail with example code https://readtorakesh.com/functional-interface-java8/
When I'm shooting a glance at lambda expressions, the book touches on a functional interface that has only one abstract method. My issue addresses on that quiz question
/* Which of these interfaces are functional interfaces? */
public interface Adder{
int add(int a, int b);
}
public interface SmartAdder extends Adder{
int add(double a, double b);
}
public interface Nothing{
}
I know the last one is not, but I think the first and second ones should be functional interface. But the book says second one is not. Why? Doesn't it overrides add method? So even in second, isn't there only one abstract method?
An easy way to find out would be to try to define a class that implements SmartAdder. The compiler will tell you you need to implement both add(int, int) and add(double, double).
It's understandable that you thought add(double, double) would override add(int, int), but they are in fact separate methods, and could potentially have totally unrelated implementations.
If SmartAdder had defined a default implementation of add(int, int) it would be a functional interface still:
public interface SmartAdder extends Adder {
int add(double a, double b);
default int add(int a, int b) {
return add((double)a, (double)b); // this calls the double method instead
}
}
You may also have come across the #FunctionalInterface annotation - this can be placed on an interface to enforce at compile-time that the interface has exactly one abstract method. If SmartAdder was annotated with #FunctionalInterface the interface itself would not compile.
SmartAdder has two methods. The method signatures are different. Functional Interface can have only one method.
In Java, a method in a subtype overrides a method of a parent type when it has the same signature. Signature means both name and arguments of the method. In particular, arguments must be of the same exact type and must be declared in the same exact order in both methods, i.e. the types of the arguments of the method declared in the subtype can't be subtypes or types wider than the types of the arguments declared in the parent type's method.
So, in your SmartAdder interface, the method with signature add(double a, double b) does not override the method add(int a, int b) of your Adder interface, because double is wider than int. When a type has two or more methods with the same name but with different arguments, it is called method overloading, and it's totally different than method overriding.
This is why SmartAdder ends up having two abstract methods, hence it's not a functional interface (which requires the type to have only one abstract method).
interface SmartAdder overloads, not overrides interface Adder. That's because the name is the same name, but parameters types are different. Therefore, it has 2 functions. To be a functional interface it needs to have only one function.
==> Only interface Adder is a functional interface.
While reading JLS Specification for Interfaces I came across following phrase:
If an interface has no direct superinterfaces, then the interface
implicitly declares a public abstract member method m with signature
s, return type r, and throws clause t corresponding to each public
instance method m with signature s, return type r, and throws clause t
declared in Object, unless a method with the same signature, same
return type, and a compatible throws clause is explicitly declared by
the interface.
So My question is when we implement an interface why we are not forced to override implicit methods which are declared in Object class even though they are implicitly defined as abstract in Interface.
Hope I put myself correctly.
Thanks.
All classes implicitly extend Object, either directly or through some chain of superclasses. Thus, you don't have to explicitly override the implicit methods declared in an interface because you inherited the implementations from Object.
The point of an interface is to force you to implement some method. The reason everything extends Object is that we want to have some way of dealing with all classes independent of implementation. There is no reason that each class should implement something like getClass(), because the behaviour is always going to be the same.
let's say we have this method
public class Animal {
public void eat() { }
}
and on another class that extends Animal has this method
public String eat(){}
is this considered as method overriding? because I've heard that you can use different return types in method overriding providing they have the same method arguments
This is neither overloading , nor overriding ! It is compilation error .
In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading.
Following are rules of method overriding in java which must be followed while overriding any method. private, static and final method can not be overridden.
1)Method signature must be same including return type, number of method parameters, type of parameters and order of parameters.
2)Overriding method can not throw higher Exception than original or overridden method. This rule only applies to checked Exception in Java, overridden method is free to throw any unchecked Exception.
3)Overriding method can not reduce accessibility of overridden method , means if original or overridden method is public than overriding method can not make it protected.
You can only change the return type to a subclass of the original return type.
All answers are correct from a technical point of view because Void is a class in Java that no other class can extend and when methods are overridden the return-type has to be a sub-type of the original type (parentReturnType.isAsignableFrom(overriddenReturnType)).
But from a practical point of view the question is still valid and it would have made sense to make an excuse for the special case void. The only value a variable of the type Void can hold is null. Further no variable can have the primitive type void. Finally you can not compile this code:
Void result = object.notify();
So in the end it would have made sense to specify Java differently when 1.5 was introduced and allow overriding void return types with any other type.
Therefore the only reasonable answer is IMHO it is not possible because Java was designed such that this is not allowed.
This not correct Code, as two methods with same name should return same value, only arguments can be differents
Overriding is having same method with same arguments in the subclass
Overloading having same method name with different argumens
The return type of a method is not part of that method's signature.
When you call eat() on the subclass, java looks at the signature you provided (eat with no parameters) and starts looking for a method with this signature, starting in the class of the instance type of the object you call it on (and searches through its ancestors if it fails to find it).
So, methods called on a subclass will always run the overridden methods (they're found first).
A critical 'point' of OO (specifically polymorphism) is that a subclass can be passed in where the superclass was expected.
If a method wants to call the superclass' eat() method and get a void, but you passed a subclass into that method of which eat() returned a String; you can see that method will call the subclass' eat and get an unexpected value.
You can see why this could be a problem. :I
So, overriding methods must return the same type or a subclass of the type returned by the parent.
(Similar principles apply as to why you can return a subclass; I'll let you think about it c: )
It's neither overriding nor overloading, it creates compilation error.
Two methods with same name, different param numbers or different type of params known as overload method, return type may be same or different. It may be in same class or parent, child classes.
Two methods having same param numbers, same types, same return types but one is in parent class and other is in child class are known as overrided methods.
I have n number of java classes each containing its own set of variables.
I have a method which has to accept one of these n classes as a parameter based on some condition.
How can I make all these classes as subclasses to a generic type so that my method signature accepts all these different types? I dont want to overload the method. And if use the type 'Object' for the parameter, I am losing the nature of the input param class I am passing.
Define an interface that all your classes implement. Then declare your parameter in the method's head as the type of that interface.
Is this what you want?
public interface common<T extends common<?>> {
public void commonMethod(T param, ...);
}
public class child implements common<child> {
public void commonMethod(child param, ...) {
...
}
}
How can I make all these classes as subclasses to a generic type so that my method signature accepts all these different types?
Simple make an abstract class, MySuperClass, this class gets inherited by all the classes you wanted to be passed to the Method. This super class have common functionality and attributes.
The other way, if your subclasses are likely to extend other class; or, there is nothing common among the class (except the fact that they all are passed to the method of yours) you can have an interface, say MySuperClass (well ideally, I like interface to start with I, like IMySuperClass).
I dont want to overload the method.
Once you have your super-class, or interface ready, your method signature would look something like myMethod(Param1 val1, MySuperClass myObj, Param3 val3).
Simple.
If you can't use polymorphism (as suggested by the others), then you could use Java generics to do this:
E.g. this can be used to put any object into a cache:
public <T> void doSomethingWithObject(final T object) {
...
}
And you could call it like this: e.g. using String
yourInstance.<String> doSomethingWithObject(object);