CIS-260 Home http://www.c-jump.com/bcc/
Constructors
Overloading
Exceptions
Constructors are special operations that help to create and initialize objects.
Constructors are similar to
methods in Java
member functions in C++.
Compilers recognize that an operation is a constructor by looking at the name:
If name is the same as the class name, then this is a constructor.
A significant difference between constructors and other operations is that constructors don't have a return value.
Any time when the object is created. For example,
Planet saturn = new Planet(); // Java code
Memory is allocated before constructor is invoked.
Programmer is responsible for initialization of the object instance.
In Java keyword new creates a new instance of an object:
Planet saturn = new Planet(); // Java code
Empty parentheses in the above code specify that constructor of the Planet class does not take any arguments.
Constructor with no parameters is called default constructor.
Alternatively, constructors could have a list of parameters required to properly initialize the object.
One of the most common and rudimentary activities inside a constructor is initialization of the object memory.
That is, initialization of the data member attributes.
When data attributes are added to object implementation, each constructor needs to be updated.
public class Planet { int distance; int satellites; double mass; public Planet( int distance, int satellites, double mass ) { this.distance = distance; this.satellites = satellites; this.mass = mass; } }
The classes get a default constructor provided by Java.
The default constructor initializes
all primitive numeric attributes to 0
all object attributes to null.
Often there is no reason to code default constructor because it would have the same functionality as the compiler-provided constructor.
On the other hand, many programmers prefer to code it for documentation purposes anyway.
Our objects can be constructed in many different ways.
For example, constructor of the class Planet could take a set of initial parameters, such as
number of satellites,
distance from the Sun,
planet mass.
int distance = 50000; int satellites = 14; double mass = 12345.67; Planet saturn = new Planet ( satellites, distance, mass );
|
|
|
|
The constructor of super class (a base, a parent class) is invoked when object inherits from the parent class.
When instance of an object is allocated by new keyword, the following steps occur:
Constructor of the Parent is invoked.
If no explicit call to a parent constructor of the super class is provided by the programmer, the default constructor of the super class is invoked automatically.
public class GasGiant extends Planet { public GasGiant() { // Explicit call to a parent constructor: super(); } }
Each attribute of the object is initialized.
The rest of the constructor code is executed.
Many things can go wrong at run-time.
It would be nice to have a universal mechanism to handle various kinds of errors without degenerative modifications to the public interface.
Most OO programming languages support a feature called exception mechanism.
An indication of an unusual situation, such as a failure during execution:
division by zero
memory access violation
array out of bounds
etc.
Exception aborts the normal flow of control and causes a search for an exception handler.
Exception is an object encapsulating information about exception.
Flow of events goes like this:
Exception (error) occurs.
Execution of the current operation is abandoned.
Exception object is constructed and initialized with parameters describing the error.
A search for exception handler is performed.
Each exception handler designates an exception type that it handles.
Exception handler is executed.
Upon completion of exception handler, normal execution is restored.
If exception propagates to the root of the application, the system architect has probably done a poor job of design and the program will probably crash.
To use exceptions, programmers need to write special blocks of code designated by the keywords try and catch:
try { // possible nasty code } catch(Exception e) { // code to handle the exception }
See also: C++ Exceptions
Create folder
C:\CIS123
Download file Banking.zip into that folder.
Unzip downloaded file.
The resulting folder structure should be:
C:\CIS123\Banking
Execute commands found in file commands.txt:
"C:\Program Files\Java\jdk1.6.0_06\bin\javac.exe" C:\CIS123\Banking\*.java "C:\Program Files\Java\jdk1.6.0_06\bin\java.exe" -classpath "C:\CIS123\Banking" BankingMain
Note: you may need to adjust the path
C:\Program Files\Java\jdk1.6.0_06
to point to the correct version of Java utilities installed on your system.
|
|
|