Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors and interfaces

 
K. Tsang
Bartender
Posts: 3348
12
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I want to confirm the access modifier for constructors and interfaces.

Constructor
1) Are constructors "implicitly" public, unless I explicitly say it is protected or private? If I leave out the access modifier, is this mean I can't instantiate it in a different package?

2) Since it is possible to make the access modifier protected or private, in what situations will such access modifier be used? Eg I can do:



Interface
1) I know interfaces must be non-static. Are interfaces "implicitly" public, even if I leave the access modifier (protected or private not allowed)? If I leave out the access modifier, is this mean I can't access the interface in a different package?

2) For methods declared inside interfaces, I know they are "implicitly" abstract. Are methods "implicitly" public, even if I leave out the access modifier (protected or private not allowed)? If I leave out access modifier, is this mean I can't access the interface's methods in a different package?

3) Since the methods declared inside interfaces are implicitly abstract, in what situations is the abstract keyword needed for the interface? Eg I can do:


Thanks, K.
 
Jon Parise
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java there are really 4 visibility levels they are:

Public - visible to everyone
Private - visible only within the class itself
Protected - Visible in this class and an implementing or extending classes
Default - public to everything in the same package, private to everything outside of th package.

If you don't specify, then only Classes in the same package as your Class will be able to see the method, in this case the constructor.

It is good practice to specify the visibility you want explicitly, I rarely use the default level and it usually causes me problems.

This answers 1 & 2

As for 3, you never use the abstract keyword in an interface. It is used in Abstract Classes. Interfaces only provide the heading of a function, but never the body. Every class that implements the interface must provide the body.

In an Abstract Class, one of more methods have the abstract keyword and no body. Extending classes must implement these abstract methods.

This is useful in situations where several Classes will share certain methods, but not others. You can write the shared methods as ordinary methods in the abstract class. For the unshared methods you would create an abstract declaration of them, which forces the child classes to implement them.

Hope that helps,

Jon
 
K. Tsang
Bartender
Posts: 3348
12
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jon
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couple of minor nitpicks

Originally posted by Jon Parise:
Every class that implements the interface must provide the body.


Abstract classes that implement an interface don't have to implement every method in the interface.

Originally posted by Jon Parise:
In an Abstract Class, one of more methods have the abstract keyword and no body.


Abstract classes can actually have no abstract methods.

Originally posted by Jon Parise:
Extending classes must implement these abstract methods.


Unless they are also abstract.
 
Jon Parise
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two good nitpicks.

Thanks for clearing it up.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another nitpick:

Originally posted by Jon Parise:

As for 3, you never use the abstract keyword in an interface.


It is totally ok to use abstract for interfaces:



It's also totally redundant.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by K. Tsang:
I can do:
yet another nitpick!

You have not got a constructor in that class; because you provided it with a return type it has been converted into a method.
 
K. Tsang
Bartender
Posts: 3348
12
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ritchie indeed that's not a constructor, should have been
public class A{ protected A(){} }
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic