Professional Documents
Culture Documents
Java is a popular programming language, created in 1995. It is owned by Oracle, and more
than 3 billion devices run Java. It is used for:
To create a file in a specific directory (requires permission), specify the path of the file and
use double backslashes to escape the "\" character (for Windows).
Java Exceptions:
When executing Java code, different errors can occur: coding errors made by the
programmer, errors due to wrong input, or other unforeseeable things.
When an error occurs, Java will normally stop and generate an error message. The technical
term for this is: Java will throw an exception (throw an error).
An exception can occur for many different reasons. Following are some scenarios where an
exception occurs.
An exception (or exceptional event) is a problem that arises during the execution of a
program. When an Exception occurs the normal flow of the program is disrupted and the
program/Application terminates abnormally, which is not recommended, therefore, these
exceptions are to be handled.
Some of these exceptions are caused by user error, others by programmer error, and others
by physical resources that have failed in some manner.
Polymorphism in Java:
Polymorphism means "many forms", and it occurs when we have many classes that are
related to each other by inheritance.
Inheritance lets us inherit attributes and methods from another class. Polymorphism uses
those methods to perform different tasks. This allows us to perform a single action in
different ways.
Compile time Polymorphism (or Static polymorphism)
Polymorphism that is resolved during compiler time is known as static polymorphism.
Method overloading is an example of compile time polymorphism.
Runtime Polymorphism (or Dynamic polymorphism):
Dynamic polymorphism is a process in which a call to an overridden method is resolved at
runtime, that's why it is called runtime polymorphism.
Java Inner Classes:
Java inner class or nested class is a class which is 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.
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.
Java Package:
A package as the name suggests is a pack(group) of classes, interfaces and other packages.
In java we use packages to organize our classes and interfaces.
We have two types of packages in Java: built-in packages and the packages we can create
(also known as user defined package).
Types of packages in Java:
1) User defined package: The package we create is called user-defined package.
2) Built-in package: The already defined package like java.io.*, java.lang.* etc are known as
built-in packages.
Advantages of using a package in Java:
These are the reasons why you should use packages in Java:
Reusability: While developing a project in java, we often feel that there are few things that
we are writing again and again in our code. Using packages, you can create such things in
form of classes inside a package and whenever you need to perform that same task, just
import that package and use the class.
Better Organization: Again, in large java projects where we have several hundreds of
classes, it is always required to group the similar types of classes in a meaningful package
name so that you can organize your project better and when you need something you can
quickly locate it and use it, which improves the efficiency.
Name Conflicts: We can define two classes with the same name in different packages so to
avoid name collision, we can use packages