0

I got confused while stuyding java in deep with the core concept.

what i have studied is, if you have interface

public interface interfaceconcept {

    public void heyyou(String s);
    public abstract void nono(String s);
    public void kolk(int i);


}

and you have a class which implements the interface then you have to write the body of all the methods

  public class implementation implements interfaceconcept{

    @Override
    public void heyyou(String s) {
        // TODO Auto-generated method stub

    }

    @Override
    public void nono(String s) {
        // TODO Auto-generated method stub

    }

    @Override
    public void kolk(int i) {
        // TODO Auto-generated method stub

    }

    }

if you dont want to write the methods you can use abstract class

Now i was studing about comparator, and while inpecting its class i found it is an interface

public interface Comparator<T> { 
//
}

now it implemented it in another class, firstly it gave me an error saying.

The type Order must implement the inherited abstract method Comparator.compare(Order, Order)

i have added all the method by clicking add unimplemented method and remove all but one.

but when i compiled it, it simply compiled why? am i not supppose to add all the unimplemented methods of comparator interface as per the rules of java?

Also one of its method in comparator class is

default Comparator<T> reversed() {
        return Collections.reverseOrder(this);
    }

what is the point if you have written the implementation of one method in interface itself? you cant write any body of the method, if i m correct.

public class Order implements Comparator<Order>{

    @Override
    public int compare(Order o1, Order o2) {
        // TODO Auto-generated method stub
        return 0;
    }


    }
Aman
  • 806
  • 2
  • 12
  • 38
  • what is confusing for you? Comparator interface has one method signature compare(T o1, T o2) and you implemented it. I don't understand your question – LeTex Mar 30 '17 at 20:13
  • @LeTex comparator interface have 17 of methods for example java.util.Comparator.compare() - java.util.Comparator.reversed() - java.util.Comparator.thenComparing() - java.util.Comparator.thenComparing() - java.util.Comparator.thenComparing() - java.util.Comparator.thenComparingInt() - java.util.Comparator.thenComparingLong() etc – Aman Mar 30 '17 at 20:15
  • you are correct! I am talking about java 7. with java 8, the Comparator interface come up with more methods. BUT thanks to the new default method introduced in java 8 interfaces, you can now keep backward compatibility. Notice all the new methods are default methods. the only one which is still abstract is the compare() method. have a look at this : http://stackoverflow.com/questions/22572762/using-java-7-comparators-in-java-8 – LeTex Mar 30 '17 at 20:23
  • @doe please follow the Java naming conventions. Type names should begin with an upper-case letter and use camel case (`CamelCase`). Method names and non-constant variable names should begin with a lower-case letter and use camel case (`camelCase`). – Lew Bloch Mar 30 '17 at 21:02
  • Yes i know , was writing the code fastly to understanding the concept​ ,anyway thanks – Aman Mar 30 '17 at 21:24

1 Answers1

1

If you look at the Javadocs for Comparator<T> https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html you'll see that the only abstract method that is not default, not static, and not already implemented via inheritance from Object is compare(T, T). So it's the only method that is unimplemented in the interface, and therefore when you implement it you have implemented all the unimplemented methods in the interface.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10
  • again im confused you said at last "you have implemented all the unimplemented methods in the interface" but it has 17 methods and i have used only one and its working how can it be possible? – Aman Mar 30 '17 at 20:17
  • interface doesn't inherit from object! – LeTex Mar 30 '17 at 20:17
  • @LeTex Also one of its method in comparator class is default Comparator reversed() { return Collections.reverseOrder(this); } what is the point if you have written the implementation of method in interface itself? you cant write any body of the method, if i m correct. – Aman Mar 30 '17 at 20:22
  • No one said that interfaces inherit from `Object`, @LeTex. – Lew Bloch Mar 30 '17 at 20:23
  • Default methods are not abstract methods, @doe. – Lew Bloch Mar 30 '17 at 20:23
  • @LeTex default Comparator reversed() {} so you mean to say reversed() is an object? – Aman Mar 30 '17 at 20:24
  • No, `reversed` is a default method, as I said. https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.4 – Lew Bloch Mar 30 '17 at 20:26
  • @doe - please have a look at my comment under your question – LeTex Mar 30 '17 at 20:26
  • @LewBloch oki got you Default methods are not abstract methods, but when you write any kind of method in interface you have to write the body in the another class where you have implemented it,, right or wrong? – Aman Mar 30 '17 at 20:27
  • Wrong. You don't have to write a body for `default` methods in an implementing class. _Please_ read the documentation! – Lew Bloch Mar 30 '17 at 20:28
  • @LewBloch THanks for the documentation. i got it i think its a feature of java8 if you define your method with default you dont have to write the body of it in implemented class...... if im not wrong – Aman Mar 30 '17 at 20:38
  • @doe that is the idea behind the default keyword introduced in java 8. Without it you can not add methods to interfaces without breaking backward compatibility (unless it is a static method which is also introduced in java 8). So yes you can use default methods in the interface and you are not obliged to implement such methods unless you want to override it . I suggest you do a little study of what important changes java 8 brought. – LeTex Mar 30 '17 at 20:38
  • Do not get carried away with `default` methods. They are meant to orchestrate high-level behaviors, such as algorithms around the abstract methods in the interface. Don't make the mistake of putting low-level implementation in a `default` method. That is an antipattern. One should use `default` methods sparingly if at all, and so as to avoid dependencies on specific implementation details or too many other classes. – Lew Bloch Mar 30 '17 at 20:47
  • @LewBloch Noted and understood. just wondering why my eclipse is not allowing me to write the method in default or static , jdk8 is already there but will find a way not a problem thanks for the answer :) – Aman Mar 30 '17 at 20:59
  • What do you mean by "write the method in default or static"? – Lew Bloch Mar 30 '17 at 21:00
  • I just wanted to write a method in interface using default and static keyword but eclipse is not allowing me to do that.. @LewBloch – Aman Mar 30 '17 at 21:23
  • Show us your code. Why do you want to write a default or static method; what is the engineering logic behind that? Your question didn't originally ask about writing interfaces, so you'll to provide more detail for your new question to make any sense. – Lew Bloch Mar 30 '17 at 23:39
  • Also, Eclipse isn't forbidding you to do anything. – Lew Bloch Mar 30 '17 at 23:41