Win a copy of Spark in Action this week in the Open Source Projects forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Why is there a need to declare the default access modifier in the below code please?

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas why there is a need to declare a default in the below code? This has come up in a mock exam question. Thanks in advance.

interface Animal
{
public default String getName();
}
 
Marshal
Posts: 3144
465
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean why it is necessary to provide a body for the default method?
 
Saloon Keeper
Posts: 22245
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to look that one up, since it's a newer feature that I've never seen or required myself.

The "default" method declaration enhances Java's ability to support legacy code without the dreaded 3 AM total rewrite that was the bane of my C/C++ days. Oracle's explanation is here: https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

There are 2 questionable things in your example. First, declaring the method "public" is redundant according to Oracle's documentation. Secondly, it seems like a default method would require a method implementation.
 
Mark S.Raphael
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:I had to look that one up, since it's a newer feature that I've never seen or required myself.

The "default" method declaration enhances Java's ability to support legacy code without the dreaded 3 AM total rewrite that was the bane of my C/C++ days. Oracle's explanation is here: https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

There are 2 questionable things in your example. First, declaring the method "public" is redundant according to Oracle's documentation. Secondly, it seems like a default method would require a method implementation.




Yes Tim. The question to be precise was: "What individual changes if any, would allow the below code to compile"

/* there can only be abstract methods in
* a Java interface and not a method body */
interface Animal
{
public default String getName() { return null; } // error
}


interface Mammal
{
/* there is no point in declaring the access
* modifier for the method as default [CLARIFY THIS POINT]  */
public default String getName() { return null; }    // error
}


abstract class Otter implements Mammal, Animal
{
/* as the class is abstract, we do not have
* to override all of the methods in the
* different interfaces. However, we should
* override at least one of them */
@Override
public String getName()
{
return Animal.class.getName();
}
}


I have annotated the code with what I have learnt
 
Mark S.Raphael
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Do you mean why it is necessary to provide a body for the default method?


Why, please?
 
Tim Holloway
Saloon Keeper
Posts: 22245
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Johnstone wrote:

Ron McLeod wrote:Do you mean why it is necessary to provide a body for the default method?


Why, please?



Because as I read it, "default" is a way to add new methods to an interface without breaking the existing classes that implement that interface. But you cannot define a method without defining the code for that method somewhere.

 
Mark S.Raphael
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Mark Johnstone wrote:

Ron McLeod wrote:Do you mean why it is necessary to provide a body for the default method?


Why, please?



Because as I read it, "default" is a way to add new methods to an interface without breaking the existing classes that implement that interface. But you cannot define a method without defining the code for that method somewhere.



But the whole concept about interfaces is that they facilitate abstraction via abstract method signatures without body, that are overriden in their sub-classes, so I am confused?
 
Mark S.Raphael
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark S.Raphael wrote:

Tim Holloway wrote:

Mark Johnstone wrote:

Ron McLeod wrote:Do you mean why it is necessary to provide a body for the default method?


Why, please?



Because as I read it, "default" is a way to add new methods to an interface without breaking the existing classes that implement that interface. But you cannot define a method without defining the code for that method somewhere.



But the whole concept about interfaces is that they facilitate abstraction via abstract method signatures without body, that are overriden in their sub-classes, so I am confused?



And my second question, is what must be removed from the below code to make it compile which is what the mock exam question actually was.

interface Animal
{
public default String getName() { return null; }
}


interface Mammal
{

public default String getName() { return null; }  
}


abstract class Otter implements Mammal, Animal
{
public String getName()
{
return Animal.class.getName();
}
}
 
Mark S.Raphael
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, Ive got it. Many thanks.
 
Tim Holloway
Saloon Keeper
Posts: 22245
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I worked with C++ from shortly after it was released by AT&T and I can tell you that one of the most annoying things about C++ was multiple base classes (multiple inheritance) with overlapping functions and/or variables. The authors of Java felt the same way, so interfaces are a sort of kludge where a class can have only one base class yet still promise things as though it had multiple base classes.

The "default" mechanism is an additional kludge. As I said earlier, it's a good way to add features to new classes while keeping the all-important backwards compatibility that is a hallmark of Java. But since it is a back-door version of multiple inheritance, it also allows some of the same problems that multiple inheritance has.
 
Marshal
Posts: 25669
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:The "default" mechanism is an additional kludge. As I said earlier, it's a good way to add features to new classes while keeping the all-important backwards compatibility that is a hallmark of Java. But since it is a back-door version of multiple inheritance, it also allows some of the same problems that multiple inheritance has.



One of the kludgy rules prevents a class from implementing two interfaces which have default methods with the same signature... or something like that which prevents the "diamond problem" from happening.
 
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . One of the kludgy rules . . . .

I had forgotten about that rule. Remind me: doesn't the implementing class have to override that duplicated method regardless? No, it would appear from this JLS section that such code may fail to compile.
 
I knew that guy would be trouble! Thanks tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic