Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
46 views

Core Java Inheritance and Exception Handling

Notes for advanced core java. It includes inheritance , abstraction and exception handling. Covers Core java basics and deep concepts. With latest advancement and concept also included.

Uploaded by

Hitansh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Core Java Inheritance and Exception Handling

Notes for advanced core java. It includes inheritance , abstraction and exception handling. Covers Core java basics and deep concepts. With latest advancement and concept also included.

Uploaded by

Hitansh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

UNIT-III NOTES

Exception Handling and Inheritance

Concept of Inheritance

• Inheritance is a mechanism that enables one class to acquire all


the behaviors and attributes of another class, meaning that you
can create a new class simply by indicating the ways in which it
differs from a class that has already been developed and tested.

• Unified Modeling Language (UML) consists of many types of


diagrams, some of which can help illustrate inheritance.

• A class diagram is a visual tool that provides you with an overview


of a class. It consists of a rectangle divided into three sections—
the top section contains the name of the class, the middle section
contains the names and data types of the attributes, and the
bottom section contains the methods.

Inheritance Terminology

• A class that is used as a basis for inheritance, such as Employee, is


a base class.

• When you create a class that inherits from a base class it is a


derived class.

• Composition—the relationship in which a class contains one or more


members of another class, when those members would not
continue to exist without the object that contains them. (For
example, if a Business closes, its Departments do too.)
• Aggregation—the relationship in which a class contains one or
more members of another class, when those members would
continue to exist without the object that contains them.
(For example, if a business or department closed, the employees
would continue to exist.)

Extending Classes

• You use the keyword extends to achieve inheritance in Java.

• parent class object does not have access to its child’s data and
methods.

• You can use the instanceof operator to determine whether an


object is a member or descendant of a class.

• For example, if northernRep is an EmployeeWithTerritory object,


the value of each of the following expressions is true:

• northernRep instanceof EmployeeWithTerritory


northernRep instanceof Employee
If aClerk is an Employee object, the following is true:

Overriding Superclass Methods

• To override a field or method in a child class means to use the


child’s version instead of the parent’s version.
• Using the same method name to indicate different
implementations is called polymorphism.

• Object-oriented programmers use the term polymorphism when


discussing any operation that has multiple meanings, regardless of
whether inheritance is involved.
For example, the plus sign (+) is polymorphic because it operates
differently depending on its operands.

Using the @Override Tag

• The override annotation lets the compiler know that your


intention is to override a method in the parent class rather than
create a method with a new signature.

• For example, if the Employee class contains a displayRateOfPay()


method that displays a weekly pay rate and your intention is to
override the method in the child ContractEmployee class to display
a contractual pay rate, you can write the child class method as
follows:

A program will work and properly override parent class methods


without any @Override tags, but using the tags can help you
prevent errors and serves as a form of documentation for your
intentions

Calling Constructors During Inheritance

• When you create any object, as in the following statement, you are
calling a constructor:
SomeClass anObject = new SomeClass();

• When you instantiate an object that is a member of a subclass,


you are actually calling at least two constructors: the constructor
for the base class and the constructor for the extended, derived
class.

• When you create any subclass object, the superclass constructor


must execute first, and then the subclass constructor executes.

• When a superclass contains a default constructor and you


instantiate a subclass object, the execution of the superclass
constructor usually is transparent—that is, nothing calls attention
to the fact that the superclass constructor is executing unless
the constructor contains some action such as displaying a message.
However, you should realize that when you create an object such
as the following (where HourlyEmployee is a subclass of Employee),
both the Employee() and HourlyEmployee() constructors execute.
HourlyEmployee clerk = new HourlyEmployee();

Using Superclass Constructors that Require Arguments

• When you create a class and do not provide a constructor, Java


automatically supplies you with a default constructor—one that
never requires arguments.

• 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.

• When a superclass has a default constructor, you can create a


subclass with or without its own constructor. This is true whether
the default superclass constructor is the automatically supplied one
or one you have written.

• However, when a superclass contains only constructors that require


arguments, you must include at least one constructor for each
subclass you create.

• Your subclass constructors can contain any number of statements,


but if all superclass constructors require arguments, the first
statement within each subclass constructor must call one of the
superclass constructors.

• When a superclass requires constructor arguments upon object


instantiation, even if you have no other reason to create a subclass
constructor, you must write the subclass constructor so it can call
its superclass’s constructor.

• If a superclass has multiple constructors but one is a default


constructor, you do not have to create a subclass constructor unless
you want to. If the subclass contains no constructor, all subclass
objects use the superclass default constructor when they are
instantiated
Accessing Superclass Methods
Comparing this and super

• In a subclass, the keywords this and super sometimes refer to the


same method, but sometimes they do not.

• For example, if a subclass has overridden a superclass method


named someMethod(), then within the subclass,
super.someMethod() refers to the superclass version of the
method, and both someMethod() and this.someMethod() refer to
the subclass version.

• On the other hand, if a subclass has not overridden a superclass


method named someMethod(), the child can use the method name
with super (because the method is a member of the superclass),
with this (because the superclass method is a member of the
subclass by virtue of inheritance), or alone (again, because the
superclass method is a member of the subclass).

The concept of keeping data private is known as information


hiding.

Methods You Cannot Override

• The three types of methods that you cannot override in a subclass


are:

static methods
final methods
Methods within final classes

A Subclass Cannot Override static Methods in Its Superclass

• A subclass cannot override methods that are declared static in


the superclass.
• In Java, all instance method calls are virtual method calls by
default—that is, the method used is determined when the program
runs because the type of the object used might not be known until
the method executes.

• An advantage to making a method final is that the compiler knows


there is only one version of the method—the parent class version.

A Subclass Cannot Override Methods in a final Superclass

• A final class cannot be a parent.


Creating and Using Abstract Classes

• A concrete class is one from which you can instantiate objects.


Sometimes, a class is so general that you never intend to create
any specific instances of the class.
For example, you might never create an object that is “just” an
Employee; each Employee is more specifically a SalariedEmployee,
HourlyEmployee, or ContractEmployee.

• Class such as Employee that you create only to extend from is not
a concrete class; it is an abstract class.

• Classes that you declare to be t; your only purpose in creating


them is to enable other classes to extend them
• Programmers of an abstract class can include two method types
o Nonabstract methods, like those you can create in any class,
are implemented in the abstract class and are simply
inherited by its children.
o Abstract methods have no body and must be implemented in
child classes
• Abstract classes usually contain at least one abstract method.

• When you create an abstract method, you provide the keyword


abstract and the rest of the method header, including the method
type, name, and parameters. However, the declaration ends there:
you do not provide curly braces or any statements within the
method—just a semicolon at the end of the declaration

• If you create an empty method within an abstract class, the


method is an abstract method even if you do not explicitly use the
keyword abstract when defining the method, but programmers
often include the keyword for clarity.

• you declare a class to be abstract, its methods can be abstract or


not, but if you declare a method to be abstract, you must also
declare its class to be abstract.
Creating and Using Interfaces

• The capability to inherit from more than one class is called


multiple inheritance.

• An interface looks much like a class, except that all of its


methods (if any) are implicitly public and abstract, and all of its
data items (if any) are implicitly public, static, and final.

• An interface is a description of what a class does, but not how it is


done; it declares method headers, but not the instructions within
those methods.

• When you create a class that uses an interface, you include the
keyword implements and the interface name in the class header.

• This notation requires the class to include an implementation for


every method named in the interface.

• Whereas using extends allows a subclass to use nonprivate,


nonoverridden members of its parent’s class, implements requires
the subclass to implement its own version of each method.
Creating Interfaces to Store Related Constants

• Interfaces can contain data fields, but they must be public, static,
and final.

• makes sense that interface data must not be private because


interface methods cannot contain method bodies; without public
method bodies, you have no way to retrieve private data.

• It also makes sense that the data fields in an interface are static
because you cannot create interface objects.

• Finally, it makes sense that interface data fields are final


because, without methods containing bodies, you have no way,
other than at declaration, to set the data fields’ values, and you
have no way to change them

• Your purpose in creating an interface containing constants is to


provide a set of data that a number of classes can use without
having to redeclare the values.
Exception Handling

• An exception is an unexpected or error condition.

• The programs you write can generate many types of potential


exceptions:

o A program might issue a command to read a file from a disk,


but the file does not exist there.
o A program might attempt to write data to a disk, but the
disk is full or unformatted.
o A program might ask for user input, but the user enters an
invalid data type.
o A program might attempt to divide a value by 0.
o A program might try to access an array with a subscript that
is too large or too small.
• Exception handling is the name for the object-oriented techniques
that manage or resolve such errors.
• Unplanned exceptions that occur during a program’s execution are
also called runtime exceptions.

• Java includes two basic classes of errors: Error and Exception.

• 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.

• For example, one type of Exception class error occurs if a


program uses an invalid array subscript value, and the program
could recover by assigning a valid value to the subscript variable.

• Fault-tolerant applications are designed so that they continue to


operate, possibly at a reduced level, when some part of the system
fails.

• Robustness represents the degree to which a system is resilient


to stress and able to continue functioning.

• The list of error messages after each attempted execution is


called a stack trace history list, or more simply, a stack trace.

Trying Code and Catching Exceptions

• A method that detects an error condition “throws an


exception,” and if you write a block of code that processes the
error, that block is said to “catch the exception.

• When you create a segment of code in which something might


go wrong, you place the code in a try block, which is a block of
code you attempt to execute while acknowledging that an
exception might occur.

• A try block consists of the following elements:

o The keyword try followed by a pair of curly braces


o Executable statements lie between the curly braces,
including some statements that might cause exceptions
• To handle a thrown exception, you can code one or more catch
blocks immediately following a try block.

• A catch block is a segment of code that can handle an


exception that might be thrown by the try block that precedes
it. The exception might be one that is thrown automatically, or
you might explicitly write a throw statement.

• A throw statement is one that sends an Exception object out of


a block or a method so that it can be handled elsewhere.

• A thrown Exception can be caught by a catch block. Each catch


block can “catch” one type of exception—that is, one object
that is an object of type Exception or one of its child classes.

• You create a catch block by typing the following elements:

o The keyword catch followed by a pair of parentheses


o Between the parentheses, an Exception type and an
identifier for an instance
o A pair of curly braces that contain statements that take
the actions you want to use to handle the error condition
Using a try Block to Make Programs “Foolproof”

• 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
.

Throwing and Catching Multiple Exceptions

• If you try more than one statement, only the first error-
generating statement throws an exception.

• As soon as the exception occurs, the logic transfers to the catch


block, which leaves the rest of the statements in the try block
unexecuted.

• When a program contains multiple catch blocks, they are examined


in sequence until a match is found for the type of exception that
occurred. Then, the matching catch block executes, and each
remaining catch block is bypassed.
Using the finally Block

• When you have actions you must perform at the end of a


try…catch sequence, you can use a finally block.
• The code within a finally block executes regardless of whether
the preceding try block identifies an exception.
• Usually, you use a finally block to perform cleanup tasks that must
happen regardless of whether any exceptions occurred and
whether any exceptions that occurred were caught.
Understanding the Advantages of Exception Handling

• Flexibility it allows in the handling of error situations. When a


method you write throws an exception, the same method can catch
the exception, although it is not required to do so, and in most
object-oriented programs it does not.
• Ability to appropriately deal with exceptions as you decide how to
handle them. When you write a method, it can call another, catch a
thrown exception, and you can decide what you want to do.

• Methods are flexible partly because they are reusable—that is, a


well-written method might be used by any number of applications.

For example, an application that uses a method that divides values


might need to terminate if division by 0 occurs. A different
program simply might want the user to re-enter the data to be
used, and a third program might want to force division by 1. The
method that contains the division statement can throw the error,
but each calling program can assume responsibility for handling the
error detected by the method in an appropriate way.

Specifying the Exceptions a Method Can Throw

• If a method throws an Exception that it will not catch but that


will be caught by a different method, you must use the keyword
throws followed by an Exception type in the method header. This
practice is known as exception specification.

Checked and Unchecked Exceptions

• 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.

• The only exceptions that must be caught or named in a throws


clause are the type known as checked exceptions.

o Unchecked exceptions—These exceptions inherit from the


Error class or the RuntimeException class. Although you can
handle these exceptions in your programs, you are not
required to do so.

For example, dividing by zero is a type of RuntimeException,


and you are not required to handle this exception—you can
simply let the program terminate.

o Checked exceptions—These exceptions are the type that


programmers should anticipate and from which programs
should be able to recover. All exceptions that you explicitly
throw and that descend from the Exception class are
checked exceptions.

• If you throw a checked exception from a method, you must do


one of the following:
o Catch it within the method.
o Specify the exception in your method header’s throws
clause.
• If you write a method with a throws clause in the header,
then any method that uses your method must do one of the
following:
o Catch and handle the possible exception.
o Declare the exception in its throws clause. The called
method can then rethrow the exception to yet another
method that might either catch it or throw it yet again.
• If you write a method that explicitly throws a checked Exception
that is not caught within the method, Java requires that you use
the throws clause in the header of the method. Using the throws
clause does not mean that the method will throw an Exception—
everything might go smoothly. Instead, it means the method might
throw an Exception. You include the throws clause in the method
header so applications that use your methods are notified of the
potential for an Exception

• To be able to use a method to its full potential, you must know


the method’s name and three additional pieces of information:

o The method’s return type


o The type and number of arguments the method requires
o The type and number of Exceptions the method throws

Creating Your Own Exceptions

• To create your own throwable Exception, you must extend a


subclass of Throwable.

• Throwable has two subclasses, Exception and Error, which are


used to distinguish between recoverable and nonrecoverable
errors.

• Because you always want to create your own Exceptions for


recoverable errors, you should extend your Exceptions from the
Exception class.

• When you create an Exception subclass, it’s conventional to end its


name with Exception.

• The Exception class contains four constructors as follows:


o Exception()—Constructs a new exception with null as its
detail message.
o Exception(String message)—Constructs a new exception with
the specified detail message.

o Exception(String message, Throwable cause)—Constructs a


new exception with the specified detail message and cause.

o Exception(Throwable cause)—Constructs a new exception


with the specified cause and a detail message of
cause.toString(), which typically contains the class and the
detail message of cause, or null if the cause argument is null

You might also like