• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Are interfaces the new Abstract classes

 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if they are not, why not just make it so and be done with it.
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However, starting with Java 8 there are functional interfaces which may or may not contain default and static methods.
Also, starting with Java 8, Lambdas were introduced, which appear to work solely on functional interfaces.
Many people seem to struggle with Lambdas, me included, yet I'm told that once you figure them out you will not want to live without them.

Plus, as temping as it is, we should not forget about backward compatibility.
I'm sure that there is older code (pre Java 8) out that is currently using interfaces.

I do like your theory, it's just theory cannot always be reality.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, Java8 interfaces are different from abstract classes:-
  • 1: Abstract classes can have variable fields, and private fields.
  • 2: Static methods in abstract classes may be inherited by subtypes; static methods from interfaces are not inherited.
  • 3: You can write constructors in abstract classes.
  •  
    Saloon Keeper
    Posts: 15485
    363
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Are interfaces the new abstract classes? No. Interfaces can not have state.

    Why not make it so? Because while the diamond problem already makes it tough to disambiguate between behavior, disambiguation between state is going to be terrible:
     
    gunslinger & author
    Posts: 169
    16
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The addition of default and static methods in interfaces certainly blurs the distinction between interfaces and abstract classes. Interfaces were created in the first place partly because of the so-called diamond problem in multiple inheritance, i.e., if Animal has a speak() method and Horse overrides it to whinny and Bird overrides it to chirp, what does Pegasus do? If both Horse and Bird were interfaces, we don't have any conflicting implementations to worry about.

    Now that interfaces have default methods, if a class implements multiple interfaces it could get a conflict between two default methods with the same signature and different implementations. Fortunately, the rule is that any implementation in the class overrides those in the interfaces, so you just override it and you're done.

    Other languages handle that differently. The old Eiffel language (there's an obscure reference for you) let you "undefine" a particular implementation, so you could just say which one you wanted. I always thought that was an elegant solution.

    As another person mentioned, interfaces are still different from abstract classes partly because you can't have any (non constant) attributes in an interface.

    Btw, Java 9 also allows you to add private methods to interfaces, which can't even be invoked from the class that implements the interface! They are callable from other methods in that interface, though, so the whole idea is just an edge case.

    The addition of static methods to interfaces helps avoid having to constantly create utility classes that use that interface. The changes in Comparator are amazing in Java 8, letting you do all kinds of sorting and collecting as just method calls.

    In a language that's trying to maintain 20 years of backward compatibility, I thought the default and static approach was pretty useful.
     
    Pranav Sharma
    Ranch Hand
    Posts: 261
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for the responses everyone. While I understand that they are different as of Java 8 and that backward compatibility is real world practical problem. I wanted to start a conversation about how the lines between the 2 are more blurred than ever with Java 9 and how would one design a solution if we were not restrained by backward compatibility.

    Kenneth thanks for clarifying that the private methods can only be accessed inside the interface.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Kenneth A. Kousen wrote:. . . The old Eiffel language (there's an obscure reference for you) . . .

    Not to me it isn't. I tried to write an Eiffel compiler for my MSc and found it was impossible to use lex and yacc because the language isn't context‑free. That was about the time Bertrand Meyer was taking the language out of Eiffel–the Language and into ECMA‑367 and the users all complained about the changes. When Meyer aged a bit and became less active promoting the language, that was the last straw; Eiffel simply disappeared below the radar. Which is a shame because Eiffel was a good language.
    In Object‑Oriented Software Construction, Meyer was rather scathing about Java®, but most of his criticisms have been addressed by Sun and Oracle since then, so he was right.

    The addition of static methods to interfaces helps avoid having to constantly create utility classes that use that interface. . . .

    It also allows you to write factory methods in interface, like this one and this one.
     
    Kenneth A. Kousen
    gunslinger & author
    Posts: 169
    16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I learned the basics of Eiffel as part of my first OOP course in graduate school back in the late 90s. The professor used Meyer's OOSC book and we all collectively worked on a puzzle game together. I was a career changer from engineering, still getting used to the fact that about 12 years of coding in Fortran (!) hadn't taught me anything about software development. By that point I had finished the first edition of Bruce Eckel's _Thinking in Java_ book, but that was the extent of my OO experience.

    I really liked the language, and the OOSC book was amazing, but I never returned to Eiffel after the semester ended. I often think of that language like Steve Job's old NeXT machines -- they had those ads that said, "there will be ten revolutionary changes in computers in the next ten years, and here are seven of them". That's how I think about Eiffel. It never really broke through, but most of its ideas migrated into other languages, including Java.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Kenneth A. Kousen wrote:. . . Eiffel. It never really broke through, but most of its ideas migrated into other languages, including Java.

    Personalities. Any success Eiffel had was because of Meyer's personality and active advocation of it.
    reply
      Bookmark Topic Watch Topic
    • New Topic