Core Java Inheritance and Exception Handling
Core Java Inheritance and Exception Handling
Concept of Inheritance
Inheritance Terminology
Extending Classes
• parent class object does not have access to its child’s data and
methods.
• When you create any object, as in the following statement, you are
calling a constructor:
SomeClass anObject = new SomeClass();
• When you write your own constructor, you replace the automatically
supplied version.
• When you use a class as a superclass and the class has only
constructors that require arguments, you must be certain that any
subclasses provide the superclass constructor with the arguments
it needs.
static methods
final methods
Methods within final classes
• Class such as Employee that you create only to extend from is not
a concrete class; it is an abstract class.
• When you create a class that uses an interface, you include the
keyword implements and the interface name in the class header.
• Interfaces can contain data fields, but they must be public, static,
and final.
• It also makes sense that the data fields in an interface are static
because you cannot create interface objects.
• The Error class represents more serious errors from which your
program usually cannot recover.
• For example, there might be insufficient memory to execute a
program. Usually, you do not use or implement Error objects in
your programs. A program cannot recover from Error conditions on
its own.
• The Exception class comprises less serious errors that represent
unusual conditions that arise while a program is running and from
which the program can recover.
• Using a try block can allow you to handle potential data conversion
exceptions caused by careless users. You can place conversion
attempts, such as calling nextInt() or nextDouble(), in a try block
and then handle any generated errors.
Declaring and Initializing Variables in try…catch Blocks
.
• If you try more than one statement, only the first error-
generating statement throws an exception.
• Most of the time, you let Java handle any Exception by shutting
down the program. Imagine how unwieldy your programs would
become if you were required to provide instructions for handling
every possible error, including equipment failures and memory
problems. Most exceptions never have to be explicitly thrown or
caught, nor do you have to include a throws clause in the headers
of methods that automatically throw these Exceptions.