Sunteți pe pagina 1din 35

UNIT-IV

EXTENDING JAVA CLASSES


TOPICS

The Final Keyword


Static Members& Methods
Interface& Abstract Class
Nested Classes
Enum in Java

Exception Handling

Final Keyword In Java


1. Final variable
2. Final method
3. Final class
The final keyword in java is used to restrict the user. The java final keyword can be used in
many contexts. Final can be:
1. variable
2. method
3. class
The final keyword can be applied with the variables, a final variable that have no value it is
called blank final variable or uninitialized final variable. It can be initialized in the constructor
only. The blank final variable can be static also which will be initialized in the static block only.

1) Java final variable


If you make any variable as final, you cannot change the value of final variable(It will be
constant).

Example of final variable

There is a final variable speedlimit, we are going to change the value of this variable, but It can't
be changed because final variable once assigned a value can never be changed.
1. class Bike9{
2.

final int speedlimit=90;//final variable

3.

void run(){

4.

speedlimit=400;

5.

6.

public static void main(String args[]){

7.

Bike9 obj=new Bike9();

8.

obj.run();

9.

10. }//end of class


Output:Compile Time Error

2) Java final method


If you make any method as final, you cannot override it.

Example of final method


1. class Bike{
2.

final void run(){System.out.println("running");}

3. }
4.
5. class Honda extends Bike{
6.

void run(){System.out.println("running safely with 100kmph");}

7.
8.

public static void main(String args[]){

9.

Honda honda= new Honda();

10.

honda.run();

11.

12. }
Output:Compile Time Error

3) Java final class


If you make any class as final, you cannot extend it.

Example of final class


1. final class Bike{}
2.
3. class Honda1 extends Bike{
4.

void run(){System.out.println("running safely with 100kmph");}

5.
6.

public static void main(String args[]){

7.

Honda1 honda= new Honda();

8.

honda.run();

9.

10. }
Output:Compile Time Error

Static Members & Mehtods


Java static keyword

The static keyword in java is used for memory management mainly. We can apply java static
keyword with variables, methods, blocks and nested class. The static keyword belongs to the
class than instance of the class.
The static can be:
1. variable (also known as class variable)
2. method (also known as class method)
3. block
4. nested class

1) Java static variable


If you declare any variable as static, it is known static variable.

The static variable can be used to refer the common property of all objects (that is not
unique for each object) e.g. company name of employees,college name of students etc.

The static variable gets memory only once in class area at the time of class loading.

Advantage of static variable


It makes your program memory efficient (i.e it saves memory).
Understanding problem without static variable
1. class Student{
2.

int rollno;

3.

String name;

4.

String college="ITS";

5. }
Suppose there are 500 students in my college, now all instance data members will get memory
each time when object is created.All student have its unique rollno and name so instance data
member is good.Here, college refers to the common property of all objects.If we make it
static,this field will get memory only once.
Java static property is shared to all objects.

Example of static variable


1. //Program of static variable
2.
3. class Student8{
4.

int rollno;

5.

String name;

6.

static String college ="ITS";

7.
8.

Student8(int r,String n){

9.

rollno = r;

10.

name = n;

11.

12. void display (){System.out.println(rollno+" "+name+" "+college);}


13.
14. public static void main(String args[]){
15. Student8 s1 = new Student8(111,"Karan");
16. Student8 s2 = new Student8(222,"Aryan");
17.
18. s1.display();
19. s2.display();
20. }
21. }
Output:111 Karan ITS
222 Aryan ITS

Program of counter without static variable


In this example, we have created an instance variable named count which is incremented in the
constructor. Since instance variable gets the memory at the time of object creation, each object
will have the copy of the instance variable, if it is incremented, it won't reflect to other objects.
So each objects will have the value 1 in the count variable.
1. class Counter{
2. int count=0;//will get memory when instance is created
3.
4. Counter(){
5. count++;
6. System.out.println(count);
7. }
8.
9. public static void main(String args[]){
10.
11. Counter c1=new Counter();
12. Counter c2=new Counter();

13. Counter c3=new Counter();


14.
15. }
16. }
Output:1
1
1

Program of counter by static variable


As we have mentioned above, static variable will get the memory only once, if any object
changes the value of the static variable, it will retain its value.
1. class Counter2{
2. static int count=0;//will get memory only once and retain its value
3.
4. Counter2(){
5. count++;
6. System.out.println(count);
7. }
8.
9. public static void main(String args[]){
10.
11. Counter2 c1=new Counter2();
12. Counter2 c2=new Counter2();
13. Counter2 c3=new Counter2();
14.
15. }

16. }
Output:1
2
3

2) Java static method


If you apply static keyword with any method, it is known as static method.

A static method belongs to the class rather than object of a class.

A static method can be invoked without the need for creating an instance of a class.

static method can access static data member and can change the value of it.

Example of static method


1. //Program of changing the common property of all objects(static field).
2.
3. class Student9{
4.

int rollno;

5.

String name;

6.

static String college = "ITS";

7.
8.

static void change(){

9.

college = "BBDIT";

10.

11.
12.

Student9(int r, String n){

13.

rollno = r;

14.

name = n;

15.

16.
17.

void display (){System.out.println(rollno+" "+name+" "+college);}

18.
19.

public static void main(String args[]){

20.

Student9.change();

21.
22.

Student9 s1 = new Student9 (111,"Karan");

23.

Student9 s2 = new Student9 (222,"Aryan");

24.

Student9 s3 = new Student9 (333,"Sonoo");

25.
26.

s1.display();

27.

s2.display();

28.

s3.display();

29.

30. }
Output:111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT

Another example of static method that performs normal calculation


1. //Program to get cube of a given number by static method
2.
3. class Calculate{
4.

static int cube(int x){

5.

return x*x*x;

6.

7.
8.

public static void main(String args[]){

9.

int result=Calculate.cube(5);

10. System.out.println(result);
11. }
12. }
Output:125

Restrictions for static method


There are two main restrictions for the static method. They are:
1. The static method can not use non static data member or call non-static method directly.
2. this and super cannot be used in static context.
1. class A{
2.

int a=40;//non static

3.
4.

public static void main(String args[]){

5.

System.out.println(a);

6.

7. }
Output:Compile Time Error

Java static method example program


class Languages {
public static void main(String[] args) {

display();
}
static void display() {
System.out.println("Java is my favorite programming language.");
}
}

Interface& Abstract Class

An interface in java is a blueprint of a class. It has static constants and abstract methods
only. Interface declares a set of methods & their Signatures.
The methods that are declared in an interface should not have implementation codes.
A class that makes use of an interface should provide the codes for the method declared
in that interface.
Java Interface also represents IS-A relationship.
It cannot be instantiated just like abstract class

Why use Java interface?

There are mainly two reasons to use interface. They are given below.

It is used to achieve fully abstraction.

By interface, we can support the functionality of multiple inheritance.

The java compiler adds public and abstract keywords before the interface method and public, static
and final keywords before data members.

In other words, Interface fields are public, static and final by default, and methods are public and
abstract.

Understanding relationship between classes and interfaces

As shown in the figure given below, a class extends another class, an interface extends another
interface but a class implements an interface.

Structure of Interface:
Interface_name
{
Type variable-name1=value1;
..
..
Type variable-nameN=valueN;
Return-Type method-Name1(parameter-list);
..
..
Return-Type method-NameN(parameter-list);
}
Simple example of Java interface

In this example, Printable interface have only one method, its implementation is provided in the A class.
1. interface printable
2. {
3. void print();
4. }
5.
6. class A6 implements printable{
7. public void print()
8. {
9. System.out.println("Hello");
10. }
11.
12. public static void main(String args[]){
13. A6 obj = new A6();
14. obj.print();
15. }
16. }
Output:Hello

Multiple inheritance in Java by interface


If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known
as multiple inheritance.

Multiple Inheritance
1. interface Printable{
2. void print();
3. }
4.
5. interface Showable{
6. void show();
7. }
8.
9. class A7 implements Printable, Showable{
10.
11. public void print(){System.out.println("Hello");}
12. public void show(){System.out.println("Welcome");}
13.
14. public static void main(String args[]){

15. A7 obj = new A7();


16. obj.print();
17. obj.show();
18. }
19. }
Output:Hello
Welcome

Abstract class in Java


A class that is declared with abstract keyword, is known as abstract class in java. It can have
abstract and non-abstract methods (method with body).
Before learning java abstract class, let's understand the abstraction in java first.
Abstraction in Java

Abstraction is a process of hiding the implementation details and showing only functionality to
the user.
Another way, it shows only important things to the user and hides the internal details for example
sending sms, you just type the text and send the message. You don't know the internal processing
about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
Ways to achieve Abstaction

There are two ways to achieve abstraction in java


1. Abstract class (0 to 100%)
2. Interface (100%)
Abstract class in Java

A class that is declared as abstract is known as abstract class. It needs to be extended and its
method implemented. It cannot be instantiated.

If a class contain any abstract method then the class is declared as abstract class. An abstract
class is never instantiated. It is used to provide abstraction. Although it does not provide 100%
abstraction because it can also have concrete method.
Syntax :
abstract class class_name { }

Example abstract class


1. abstract class A{}
abstract method
A method that is declared as abstract and does not have implementation is known as abstract method.

Method that are declared without any body within an abstract class are called abstract method.
The method body will be defined by its subclass. Abstract method can never be final and static.
Any class that extends an abstract class must implement all the abstract methods declared by the
super class.
Syntax :
abstract return_type function_name ();

// No definition

Example abstract method


1. abstract void printStatus();//no body and abstract

Example of abstract class that has abstract method

In this example, Bike the abstract class that contains only one abstract method run. It
implementation is provided by the Honda class.
1. abstract class Bike{
2.

abstract void run();

3. }
4. class Honda4 extends Bike{
5. void run()
6. {

7. System.out.println("running safely..");
8. }
9. public static void main(String args[]){
10. Bike obj = new Honda4();
11. obj.run();
12. }
13. }
O/p:running safely..

Understanding the real scenario of abstract class

In this example, Shape is the abstract class, its implementation is provided by the Rectangle and
Circle classes. Mostly, we don't know about the implementation class (i.e. hidden to the end user)
and object of the implementation class is provided by the factory method.
A factory method is the method that returns the instance of the class. We will learn about the
factory method later.
In this example, if you create the instance of Rectangle class, draw() method of Rectangle class
will be invoked.
File: TestAbstraction1.java
1. abstract class Shape{
2. abstract void draw();
3. }
4. //In real scenario, implementation is provided by others i.e. unknown by end user
5. class Rectangle extends Shape{
6. void draw(){System.out.println("drawing rectangle");}
7. }
8. class Circle1 extends Shape{
9. void draw(){System.out.println("drawing circle");}

10. }
11. //In real scenario, method is called by programmer or user
12. class TestAbstraction1{
13. public static void main(String args[]){
14. Shape s=new Circle1();//In real scenario, object is provided through method e.g. getShape() meth
od
15. s.draw();
16. }
17. }
drawing circle

Another example of abstract class in java

File: TestBank.java
1. abstract class Bank{
2. abstract int getRateOfInterest();
3. }
4. class SBI extends Bank{
5. int getRateOfInterest(){return 7;}
6. }
7. class PNB extends Bank{
8. int getRateOfInterest(){return 8;}
9. }
10.
11. class TestBank{
12. public static void main(String args[]){
13. Bank b;

14. b=new SBI();


15. System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
16. b=new PNB();
17. System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
18. }}
Rate of Interest is: 7 %
Rate of Interest is: 8 %

Abstract class having constructor, data member, methods etc.

An abstract class can have data member, abstract method, method body, constructor and even
main() method.
File: TestAbstraction2.java
1. //example of abstract class that have method body
2.

abstract class Bike{

3.

Bike(){System.out.println("bike is created");}

4.

abstract void run();

5.

void changeGear(){System.out.println("gear changed");}

6.

7.
8.

class Honda extends Bike{

9.

void run(){System.out.println("running safely..");}

10. }
11. class TestAbstraction2{
12. public static void main(String args[]){
13. Bike obj = new Honda();
14. obj.run();

15. obj.changeGear();
16. }
17. }
bike is created
running safely..
gear changed

Rule: If there is any abstract method in a class, that class must be abstract.
1. class Bike12{
2. abstract void run();
3. }
compile time error

Rule: If you are extending any abstract class that have abstract method, you must either provide the
implementation of the method or make this class abstract.
When to use Abstract Methods & Abstract Class?

Abstract methods are usually declared where two or more subclasses are expected to do a similar
thing in different ways through different implementations. These subclasses extend the same
Abstract class and provide different implementations for the abstract methods.
Abstract classes are used to define generic types of behaviors at the top of an object-oriented
programming class hierarchy, and use its subclasses to provide implementation details of the
abstract class.

Nested Classes/Inner Class


Java Nested/Inner Class
1. Java Inner classes
2. Advantage of Inner class
3. Difference between nested class and inner class
4. Types of Nested classes

Java inner class or nested class is a class i.e. declared inside the class or interface.

We use inner classes to logically group classes and interfaces in one place so that it can be more
readable and maintainable.
Additionally, it can access all the members of outer class including private data members and methods.
Syntax of Inner class
1. class Java_Outer_class{
2.

//code

3.

class Java_Inner_class{

4.

//code

5.

6. }

Advantage of java inner classes


There are basically three advantages of inner classes in java. They are as follows:
1) Nested classes represent a special type of relationship that is it can access all the members
(data members and methods) of outer class including private.
2) Nested classes are used to develop more readable and maintainable code because it
logically group classes and interfaces in one place only.
3) Code Optimization: It requires less code to write.
Difference between nested class and inner class in Java

Inner class is a part of nested class. Non-static nested classes are known as inner classes.

Types of Nested classes


There are two types of nested classes non-static and static nested classes.The non-static nested classes are
also known as inner classes.
1. Non-static nested class(inner class)
o

a)Member inner class

b)Annomynous inner class

c)Local inner class

2. Static nested class


Type
Member Inner Class
Anonymous Inner
Class
Local Inner Class
Static Nested Class
Nested Interface

Description
A class created within class and outside method.
A class created for implementing interface or extending class. Its name is
decided by the java compiler.
A class created within method.
A static class created within class.
An interface created within class or interface.

Enum in Java
Enum in java is a data type that contains fixed set of constants.
It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc.
The java enum constants are static and final implicitly. It is available from JDK 1.5.
Java Enums can be thought of as classes that have fixed set of constants.

Points to remember for Java Enum

enum improves type safety

enum can be easily used in switch

enum can be traversed

enum can have fields, constructors and methods

enum may implement many interfaces but cannot extend any class because it internally
extends Enum class

Simple example of java enum


1. class EnumExample1{
2. public enum Season { WINTER, SPRING, SUMMER, FALL }
3.

4. public static void main(String[] args) {


5. for (Season s : Season.values())
6. System.out.println(s);
7.
8. }}
Output:WINTER
SPRING
SUMMER
FALL

What is the purpose of values() method in enum?

The java compiler internally adds the values() method when it creates an enum. The values()
method returns an array containing all the values of the enum.

Defining Java enum

The enum can be defined within or outside the class because it is similar to a class.
Java enum example: defined outside class
1. enum Season { WINTER, SPRING, SUMMER, FALL }
2. class EnumExample2{
3. public static void main(String[] args) {
4. Season s=Season.WINTER;
5. System.out.println(s);
6. }}
Output:WINTER

Java enum example: defined inside class


1. class EnumExample3{
2. enum Season { WINTER, SPRING, SUMMER, FALL; }//semicolon(;) is optional here
3. public static void main(String[] args) {

4. Season s=Season.WINTER;//enum type is required to access WINTER


5. System.out.println(s);
6. }}
Output:WINTER
Java enum in switch statement

We can apply enum on switch statement as in the given example:


Example of applying enum on switch statement
1. class EnumExample5{
2. enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURD
AY}
3. public static void main(String args[]){
4. Day day=Day.MONDAY;
5.
6. switch(day){
7. case SUNDAY:
8.

System.out.println("sunday");

9.

break;

10. case MONDAY:


11. System.out.println("monday");
12. break;
13. default:
14. System.out.println("other day");
15. }
16. }}
Output:monday

Exception Handling in Java


Exception Handling is the mechanism to handle runtime malfunctions. The exception handling
in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the
application can be maintained Exception Exception Handling is a mechanism to handle runtime
errors such as ClassNotFound, IO, SQL, Remote etc.
Exception:
Dictionary Meaning: Exception is an abnormal condition.
In java, exception is an event that disrupts the normal flow of the program. It is an object which
is thrown at runtime.
code that's responsible for doing something about the exception is called an exception handler.

Advantage of Exception Handling


The core advantage of exception handling is to maintain the normal flow of the application.
Exception normally disrupts the normal flow of the application that is why we use exception
handling. Let's take a scenario:
1. statement 1;
2. statement 2;
3. statement 3;
4. statement 4;
5. statement 5;//exception occurs
6. statement 6;
7. statement 7;
8. statement 8;
9. statement 9;
10. statement 10;

Suppose there is 10 statements in your program and there occurs an exception at statement 5, rest
of the code will not be executed i.e. statement 6 to 10 will not run. If we perform exception
handling, rest of the statement will be executed. That is why we use exception handling in java.

Hierarchy of Java Exception classes

Exception class Hierarchy


All exception types are subclasses of class Throwable, which is at the top of exception class
hierarchy.

Exception class is for exceptional conditions that program should catch. This class is
extended to create user specific exception classes.

RuntimeException is a subclass of Exception. Exceptions under this class are


automatically defined for programs.

Exception are categorized into 3 category.

Checked Exception
The exception that can be predicted by the programmer.Example : File that need to be
opened is not found. These type of exceptions must be checked at compile time.

Unchecked Exception
Unchecked exceptions are the class that extends RuntimeException. Unchecked
exception are ignored at compile time. Example : ArithmeticException,
NullPointerException, Array Index out of Bound exception. Unchecked exceptions are
checked at runtime.

Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
Example : if stack overflow occurs, an error will arise. This type of error is not possible
handle in code.

Difference between checked and unchecked exceptions


Checked Exception

Unchecked Exception

The classes that extend


Throwable class except
RuntimeException and Error
are known as checked
exceptions e.g.IOException,
SQLException etcx

The classes that extend


RuntimeException are known as
unchecked exceptions e.g.
ArithmeticException,
NullPointerException,
ArrayIndexOutOfBoundsException
etc.

Checked exceptions are


checked at compile-time

Unchecked exceptions are not


checked at compile-time rather
they are checked at runtime

Common scenarios where exceptions may occur


There are given some scenarios where unchecked exceptions can occur. They are as follows:

1) Scenario where ArithmeticException occurs


If we divide any number by zero, there occurs an ArithmeticException.
1. int a=50/0;//ArithmeticException

2) Scenario where NullPointerException occurs


If we have null value in any variable, performing any operation by the variable occurs an
NullPointerException.
1. String s=null;
2. System.out.println(s.length());//NullPointerException

3) Scenario where NumberFormatException occurs


The wrong formatting of any value, may occur NumberFormatException. Suppose I have a
string variable that have characters, converting this variable into digit will occur
NumberFormatException.
1. String s="abc";
2. int i=Integer.parseInt(s);//NumberFormatException

4) Scenario where ArrayIndexOutOfBoundsException occurs


If you are inserting any value in the wrong index, it would result
ArrayIndexOutOfBoundsException as shown below:
1. int a[]=new int[5];
2. a[10]=50; //ArrayIndexOutOfBoundsException

Java Exception Handling Keywords


There are 5 keywords used in java exception handling.
1. try
2. catch
3. finally
4. throw
5. throws
Exception handling is done by transferring the execution of a program to an appropriate
exception handler when exception occurs.

Using try and catch


Try is used to guard a block of code in which exception may occur. This block of code is called
guarded region.
A catch statement involves declaring the type of exception you are trying to catch.
If an exception occurs in guarded code, the catch block that follows the try is checked, if the type
of exception that occured is listed in the catch block then the exception is handed over to the
catch block which then handles it.
Example using Try and catch
class Excp
{
public static void main(String args[])
{
int a,b,c;
try
{
a=0;
b=10;
c=b/a;
System.out.println("This line will not be executed");
}
catch(ArithmeticException e)
{
System.out.println("Divided by zero");
}
System.out.println("After exception is handled");
}
}

Output :

Divided by zero
After exception is handled

An exception will thrown by this program as we are trying to divide a number by zero inside try
block. The program control is transfered outside try block. Thus the line "This line will not be
executed" is never parsed by the compiler. The exception thrown is handle in catch block. Once
the exception is handled the program controls continue with the next line in the program. Thus
the line "After exception is handled" is printed.

Multiple catch blocks:

A try block can be followed by multiple catch blocks. You can have any number of catch blocks
after a single try block.If an exception occurs in the guarded code the exception is passed to the
first catch block in the list. If the exception type of exception, matches with the first catch block
it gets caught, if not the exception is passed down to the next catch block. This continue until the
exception is caught or falls through all catches.

Example for Multiple Catch blocks


class Excep
{
public static void main(String[] args)
{
try
{
int arr[]={1,2};
arr[2]=3/0;
}
catch(ArithmeticException ae)
{
System.out.println("divide by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array index out of bound exception");
}
}
}

Output :
divide by zero

Important points to Remember


1. If you do not explicitly use the try catch blocks in your program, java will provide a default
exception handler, which will print the exception details on the terminal, whenever exception
occurs.
2. While using multiple catch block, always make sure that exception subclasses comes before any
of their super classes. Else you will get compile time error.
3. In nested try catch, the inner try block, uses its own catch block as well as catch block of the
outer try, if required.
4. Only the object of Throwable class or its subclasses can be thrown.
Example demonstrating throw Keyword
class Test
{
static void avg()
{
try
{
throw new ArithmeticException("demo");
}
catch(ArithmeticException e)
{
System.out.println("Exception caught");
}
}
public static void main(String args[])
{
avg();
}
}

In the above example the avg() method throw an instance of ArithmeticException, which is
successfully handled using the catch statement.

throws Keyword

Any method capable of causing exceptions must list all the exceptions possible during its
execution, so that anyone calling that method gets a prior knowledge about which exceptions to
handle. A method can do so by using the throws keyword.
Syntax :
type method_name(parameter_list) throws exception_list
{
//definition of method
}

NOTE : It is necessary for all exceptions, except the exceptions of type Error and
RuntimeException, or any of their subclass.

Example demonstrating throws Keyword


class Test
{
static void check() throws ArithmeticException
{
System.out.println("Inside check function");
throw new ArithmeticException("demo");
}
public static void main(String args[])
{
try
{
check();
}
catch(ArithmeticException e)
{
System.out.println("caught" + e);
}
}
}

Finally clause

A finally keyword is used to create a block of code that follows a try block. A finally block of
code always executes whether or not exception has occurred. Using a finally block, lets you run
any cleanup type statements that you want to execute, no matter what happens in the protected
code. A finally block appears at the end of catch block.

Example demonstrating finally Clause


Class ExceptionTest
{
public static void main(String[] args)
{
int a[]= new int[2];
System.out.println("out of try");
try
{
System.out.println("Access invalid element"+ a[3]);
/* the above statement will throw ArrayIndexOutOfBoundException */
}
finally
{
System.out.println("finally is always executed.");
}
}
}

Output :
Out of try
finally is always executed.
Exception in thread main java. Lang. exception array Index out of bound exception.

You can see in above example even if exception is thrown by the program, which is not handled
by catch block, still finally block will get executed.
User defined Exception subclass

You can also create your own exception sub class simply by extending java Exception class. You
can define a constructor for your Exception sub class (not compulsory) and you can override the
toString() function to display your customized message on catch.
class MyException extends Exception
{
private int ex;
MyException(int a)
{
ex=a;
}
public String toString()
{
return "MyException[" + ex +"] is less than zero";
}
}
class Test
{
static void sum(int a,int b) throws MyException
{
if(a<0)
{

throw new MyException(a);


}
else
{
System.out.println(a+b);
}
}
public static void main(String[] args)
{
try
{
sum(-10, 10);
}
catch(MyException me)
{
System.out.println(me);
}
}
}

Points to Remember
1. Extend the Exception class to create your own ecxeption class.
2. You don't have to implement anything inside it, no methods are required.
3. You can have a Constructor if you want.
4. You can override the toString() function, to display customized message.

S-ar putea să vă placă și