aspose file tools*
The moose likes Beginning Java and the fly likes Constructors and interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructors and interfaces" Watch "Constructors and interfaces" New topic
Author

Constructors and interfaces

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2628
    
    9

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.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Jon Parise
Ranch Hand

Joined: Jul 03, 2007
Posts: 81
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

Joined: Sep 13, 2007
Posts: 2628
    
    9

Thanks Jon
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
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.


Joanne
Jon Parise
Ranch Hand

Joined: Jul 03, 2007
Posts: 81
Two good nitpicks.

Thanks for clearing it up.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40071
    
  28
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

Joined: Sep 13, 2007
Posts: 2628
    
    9

thanks Ritchie indeed that's not a constructor, should have been
public class A{ protected A(){} }
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors and interfaces