Académique Documents
Professionnel Documents
Culture Documents
The type of data returned by a method must be compatible with the return type
specified by the method. For example, if the return type of some method is boolean,
you could not return an integer.
The variable receiving the value returned by a method (such as vol, in this case) must
also be compatible with the return type specified for the method.
Constructors
A constructor initializes an object immediately upon creation & allocates memory.
Constructors look a little strange because they have no return type,
not even void. This is because the implicit return type of a class' constructor is the class
type itself.
When you do not explicitly define a
constructor for a class, then Java creates a default constructor for the class.
Once you define your own constructor, the default constructor
is no longer used.
Garbage Collection
when no references to an object exist, that object is assumed
to be no longer needed, and the memory occupied by the object can be reclaimed.
Overloading Methods
In Java it is possible to define two or more methods within the same class that share the
same name, as long as their parameter declarations are different. When this is the case,
the methods are said to be overloaded, and the process is referred to as method
overloading. Method overloading is one of the ways that Java implements polymorphism.
When an overloaded method is invoked, Java uses the type and/or number of arguments
as its guide to determine which version of the overloaded method to actually call .
Overloading Constructors
In addition to overloading normal methods, you can also overload constructor methods.
In fact, for most real-world classes that you create, overloaded constructors will be the
norm, not the exception.
Returning Objects
A method can return any type of data, including class types that you create.
Recursion
Java supports recursion. Recursion is the process of defining something in terms of itself.
As it relates to Java programming, recursion is the attribute that allows a method to call
itself. A method that calls itself is said to be recursive
A recursive call does not make a new copy of the method. Only the arguments are new. As
each recursive call returns, the old local variables and parameters are removed from the
stack, and execution resumes at the point of the call inside the method. Recursive
methods could be said to "telescope" out and back.
Understanding static
There will be times when you will want to define a class member that will be used
independently of any object of that class.
Methods declared as static have several restrictions:
They can only call other static methods.
They must only access static data.
They cannot refer to this or super in any way.
Introducing final
A variable can be declared as final. Doing so prevents its contents from being modified.
This means that you must initialize a final variable when it is declared.
nested class. It has access to all of the variables and methods of its outer class and may
refer to them directly in the same way that other non-static members of the outer class
do. Thus, an inner class is fully within the scope of its enclosing class.
Inheritance
Overview
Inheritance is one of the cornerstones of object-oriented programming because it allows
the creation of hierarchical classifications. Using inheritance, you can create a general
class that defines traits common to a set of related items. This class can then be inherited
by other, more specific classes, each adding those things that are unique to it. In the
terminology of Java, a class that is inherited is called a superclass. The class that does the
inheriting is called a subclass.
Inheritance Basics
To inherit a class, you simply incorporate the definition of one class into another by using
the extends keyword
Using super
Whenever a subclass needs to refer to its immediate superclass, it can do so by use of
the keyword super.
super has two general forms. The first calls the superclass' constructor. The second is
used to access a member of the superclass that has been hidden by a member of a
subclass. Each use is examined here.
Understanding CLASSPATH
Before an example that uses a package is presented, a brief discussion of the
CLASSPATH environmental variable is required. While packages solve many problems
from an access control and name-space-collision perspective, they cause some curious
difficulties when you compile and run programs.