Sie sind auf Seite 1von 23

Collaborate

Knowledge Byte
In this section, you will learn about:

The super and this keywords


The nested try and catch block

NIIT
Collaborate Lesson 1C /
Collaborate

The super and this Keywords


The super keyword
Java provides the super keyword that enables a subclass to refer to its
superclass. The super keyword is used to access:
superclass constructors
superclass methods and variables
The syntax to invoke the constructor of a superclass using the super()
method is:
super (<parameter1>, <parameter 2>,..,<parameterN>);
In the above syntax, <parameter1>, <parameter 2>,..,<parameterN>
refer to the list of parameters that you need to pass to the constructor of
the superclass.

NIIT
Collaborate Lesson 1C /
Collaborate

The super and this Keywords (Contd.)


The super Keyword (Contd.)
If no parameters are passed to the super() method, it invokes the default
constructor of the superclass.
If the superclass contains the overloaded constructor, you can pass
parameters to the super() method to invoke a particular constructor.
When you use the super() method in the constructor of the subclass, it
should be the first executable statement in the constructor.

NIIT
Collaborate Lesson 1C /
Collaborate

The super and this Keywords (Contd.)


The super Keyword (Contd.)
The syntax to access the member variable of a superclass is:
super.<variable>;
The subclass can also access the member methods of the superclass using
the super keyword.

NIIT
Collaborate Lesson 1C /
Collaborate

The super and this Keywords (Contd.)


The this Keyword
The this keyword is used to refer to the current object.
You can use the this keyword when a method defined in a Java class
needs to refer to the object used to invoke that method.
The following code snippet shows how to use the this keyword:
Book(int bcode, double bprice)
{
this.bookCode=bcode;
this.bookPrice=bprice;
}
In the above code snippet, the this keyword refers to the object that
invokes the Book() constructor.

NIIT
Collaborate Lesson 1C /
Collaborate

The super and this Keywords (Contd.)


The this Keyword (Contd.)
Another situation where you can use the this keyword is when the local
and instance variables have the same name.
The following code snippet shows how to use the this keyword when
instance and formal parameters have the same name:
Book(int bcode, double bprice)
{
this.bcode=bcode;
this.bprice=bprice;
}
In the above code snippet, this keyword refers to the instance variables,
bcode and bprice. The values of the formal parameters, bcode and
bprice of the Book() constructor are assigned to the instance variables.

NIIT
Collaborate Lesson 1C /
Collaborate

The super and this Keywords (Contd.)


The this Keyword (Contd.)
In addition, you can use the this keyword in a constructor of a class to
invoke another constructor of the class.
Unlike the super keyword, the this keyword can invoke the
constructor of the same class.
The following code snippet shows how to invoke a constructor using
the this keyword:
class Book {
public Book(String bname) {
this(bname, 1001); }
public Book(String bname, int bcode) {
bookName=bname;
bookCode=bcode; }
}

NIIT
Collaborate Lesson 1C /
Collaborate

The Nested try-catch Block


The try-catch block is used to handle exceptions in Java applications.
You can enclose a try-catch block in an existing try-catch block.
The enclosed try-catch block is called the inner try-catch block, and the
enclosing block is called the outer try-catch block.
If the inner try block does not contain the catch statement to handle an
exception then the catch statement in the outer block is checked for the
exception handler.
The following syntax shows how to create a nested try-catch block:
class SuperClass
{
public static void main(String a[])
{
<code>;
try
{

NIIT
Collaborate Lesson 1C /
Collaborate

The Nested try-catch Block (Contd.)


The following syntax shows how to create a nested try-catch block: (Contd.)
<code>;
try
{
<code>;
}
catch(<exception_name> <var>)
{
<code>;
}
}
catch(<exception_name> <var>)
{
<code>;
} } }
NIIT
Collaborate Lesson 1C /
Collaborate

From the Experts Desk

In this section, you will learn:

Best practices on:


The Use of final Keyword
Tips on:
The Use of Constructors
The finally Block
FAQs

NIIT
Collaborate Lesson 1C /
Collaborate

Best Practices
The Use of final Keyword

The final keyword can be used with:


A variable
A method
A class
The use of the final keyword prevents you from changing the value of a
variable.
When you use the final keyword with a variable, the variable acts as a
constant.
It is recommended that you name the final variable in uppercase.

NIIT
Collaborate Lesson 1C /
Collaborate

Best Practices
The Use of final Keyword (Contd.)

The syntax to declare the final variable is:


final int MY_VAR=10;
In the above syntax, the MY_VAR variable is declared as final. As a result,
the value of the variable cannot be modified.
The final keyword is used with methods and classes to prevent method and
class overriding while implementing inheritance in a Java application.
If a method in a subclass has the same name as the final method in a
superclass, a compile time error is generated.

NIIT
Collaborate Lesson 1C /
Collaborate

Best Practices
The Use of final Keyword (Contd.)

The following syntax shows how to declare the final method:


class SuperClass
{
final void finalMethod()
{}
}
Similarly, if a class is declared final, you cannot extend that class.
If you extend the final class, a compile time error is generated.

NIIT
Collaborate Lesson 1C /
Collaborate

Best Practices
The Use of final Keyword (Contd.)

The following syntax shows how to declare the final class:


final class SuperClass
{
//This is a final class.
}
In the preceding syntax, the SuperClass class is declared as final.

NIIT
Collaborate Lesson 1C /
Collaborate

Tips
The Use of Constructors

Consider a Java application that contains multiple classes. Each class is


extended from another class in the application and contains a default
constructor.
When the application is executed, the constructors are called in the order in
which the classes are defined.
For example, there are three classes in a Java application: Class1, Class2,
and Class3. Class2 extends Class1, and Class3 extends Class2. When you
create an instance of Class 3, the constructors are called in the following
order:
Class1 constructor
Class2 constructor
Class3 constructor

NIIT
Collaborate Lesson 1C /
Collaborate

Tips
The finally Block

The finally block associated with a try block is executed after the try or catch
block is completed.
It is optional to associate the finally block with a try block, but if the try block
does not have a corresponding catch block, you should provide the finally
block.

NIIT
Collaborate Lesson 1C /
Collaborate

FAQs
Can classes inherit from more than one class in Java?

No, a class cannot be inherited from more than one class in Java. A class is
inherited from a single class only. However, multi-level inheritance is
possible in Java. For example, if class B extends class A and class C extends
class B, class C automatically inherits the properties of class A.

What is the difference between an interface and a class?

Both, class and interface, contain constants and methods. A class not only
defines methods but also provides their implementation. However, an
interface only defines the methods, which are implemented by the class that
implements the interface.

NIIT
Collaborate Lesson 1C /
Collaborate

FAQs (Contd.)
Do you have to catch all types of exceptions that might be thrown by Java?

Yes, you can catch all types of exceptions that are thrown in a Java
application using the Exception class.

How many exceptions can you associate with a single try block?

There is no limit regarding the number of exceptions that can be associated


with a single try block. All the exceptions associated with a try block should
be handled using multiple catch blocks.

NIIT
Collaborate Lesson 1C /
Collaborate

FAQs (Contd.)
What are the disadvantages of inner classes?

The inner classes have various disadvantages. The use of inner class
increases the total number of classes in your code. The Java developers also
find it difficult to understand to implement the concept of inner class within
the programs.

What is the level of nesting for classes in Java?

There is no limit of nesting classes in Java.

NIIT
Collaborate Lesson 1C /
Collaborate

Challenge
1. You can access particular constructors in the superclass using
______operator.
2. Which of the following exceptions is raised when a number is divided by
zero:
a. NullPointerException
b. ArithmeticException
c. ArrayIndexOutOfBoundsException
d. NumberFormatException
3. You can have more than one finally block for an exception-handler.
(True/False)
4. Match the following:
a. public 1. Ensures the value is not changed in the classes that
implement an interface.
b. static 2. Ensures that are able to override the data members in
unrelated classes.
c. final 3. Ensures that you cannot create an object of the interface.
NIIT
Collaborate Lesson 1C /
Collaborate

Challenge (Contd.)
5. The following code is a jumbled code. Rearrange the following code to make it
executable:
public class Excp
{
public static void main(String args []) {
int num1, num2, num3;
num1 = 10;
num2 = 0;
catch(ArithmeticException e)
{
System.out.println(" Error Message");
} try
{
num3 = num1/num2;
}
NIIT
Collaborate Lesson 1C /
Collaborate

Solutions to Challenge

1. dot
2. b. ArithmeticException
3. False
4. a. 2
b. 1
c. 3
5. public class Excp
{
public static void main(String args [])
{
int num1, num2, num3;
num1 = 10;
num2 = 0;

NIIT
Collaborate Lesson 1C /
Collaborate

Solutions to Challenge (Contd.)


try
{
num3 = num1/num2;
}
catch( ArithmeticException e)
{
System.out.println(" Error Message");
}

NIIT
Collaborate Lesson 1C /

Das könnte Ihnen auch gefallen