This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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:
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.
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