Back to Table of Contents

In the JavaTM language, all classes inherit from Object. For fields and methods declared within a class, there are four possible types see the Four Protections below. In descending order of permissions, these are public, protected, package, and private. Note that "package" is not written in the code for package protection. Instead, if the field or method does not begin with public, protected, or private, then it is by default package.

Methods may be overridden in subclasses and if they are, they need to be at least as high in permissions as the method is in the superclass. Hence, since Object has a public toString() method, among a few others, all Objects are guaranteed to have a public toString() method.

A method in a super class and overridden in the child class can be converted from protected to public, but not to private or package. A public method can only be public in any subclass. The basic rule is that a subclass can increase the accessibility but not limit it as long as it is accessible in that subclass. Hence a protected method can always be made public but never vice versa. A package protected method (one with no protection keyword before it) can be made public or protected in a subclass if it is in the same package. However, since a package method is not visible in a subclass outside that package, a new method of the same name may be made with any permission.

When a child class extends another class, it can override the methods of the parent class, but the return type must remain the same. Since method overloading is a java feature, one can have multiple methods of the same name and return value, but with different parameter types and numbers of parameters. This is called overloading methods and is a good way of providing different implementations to different value types being submitted to the same method name. Since Java 1.5, there are now possibilities of using variable numbers of arguments (Varargs) in the same method.

Whatever class or interface a class extends or implements, that child class is an Object of those parent types and can be converted to those types with what is called a cast. A cast consists of the type between parentheses. Example:

public Object getObj(){
	String S = "hi";
	return (Object) S;
//This is answered by:
String S2 = (String) getObj();

An Object in memory remains all its types until it is set to null and garbage collected. But to assign it to a type or to use it of some type, you need to cast it the specific type. Note: an Object can only be casted to the following types: the original type it was instantiated as using the new operator, all the super classes of that class, and all the interfaces of that class, and all the interfaces of its super classes.

All classes have only one direct parent class. If that parent class (super class) is Object, then it is implied and does not need to be specifically extended in the code. Otherwise you need to use the extends keyword:

public myGui extends JFrame, implements Runnable, ActionListener {

As the above example demonstrates, any number of interfaces. can be implemented by a class, but only one class can be extended.

The Four Possible Protections:

public Used to apply to methods and fields to make them accessable to all other classes, whether in the same package or outside. This is the greatest level of permissions. Only one class can be made public in a java source file. A public class is also accessible to any other class whether in the same package or outside.
protected Used to apply to methods and fields to limit their scope to other classes within the same package and to descendants regardless of whether the descendant is within the same package.
  If none of the three protection keywords are there (public, protected, or private), it is called package protection. Classes, methods, and fields that are under package protection can only be accessed by other classes within the same package. Descendants outside that package cannot access such classes, methods, or fields.
private Methods and fields marked private can only be accessed within that class. They cannot be accessed outside that class. However, one Object of such a class can access private fields and methods of another Object of the same class.

*OracleTM and JavaTM are registered trademarks of Oracle and or its affiliates. Other names may be trademarks of their respective owners.*