Worksheet: Q.1. What Is An Interface. Explain The Characteristics of The Interface
Worksheet: Q.1. What Is An Interface. Explain The Characteristics of The Interface
Ans: An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class
implements an interface, thereby inheriting the abstract methods of the interface.
Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested
types. Method bodies exist only for default methods and static methods.
Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And
an interface contains behaviors that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the
class.
Ans: The interface keyword is used to declare an interface. Here is a simple example to declare an interface −
Example
An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an
interface.
Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
Methods in an interface are implicitly public.
Example
Ans. When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the
specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must
declare itself as abstract.
A class uses the implements keyword to implement an interface. The implements keyword appears in the class
declaration following the extends portion of the declaration.
Example
Output
Mammal eats
Mammal travels
Ans. Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating
and usage of classes, interfaces, enumerations and annotations easier, etc.
A Package can be defined as a grouping of related types (classes, interfaces, enumerations and annotations )
providing access protection and namespace management.
Programmers can define their own packages to bundle group of classes/interfaces, etc. It is a good practice to group
related classes implemented by you so that a programmer can easily determine that the classes, interfaces,
enumerations, and annotations are related.
Since the package creates a new namespace there won't be any name conflicts with names in other packages. Using
packages, it is easier to provide access control and it is also easier to locate the related classes.
Ans. While creating a package, you should choose a name for the package and include a package statement along
with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types
that you want to include in the package.
The package statement should be the first line in the source file. There can be only one package statement in each
source file, and it applies to all types in the file.
If a package statement is not used then the class, interfaces, enumerations, and annotation types will be placed in the
current default package.
To compile the Java programs with package statements, you have to use -d option as shown below.
Ans. Let us look at an example that creates a package called animals. It is a good practice to use names of packages
with lower case letters to avoid any conflicts with the names of classes and interfaces.
interface Animal {
public void eat();
public void travel();
}
Now, let us implement the above interface in the same package animals −
package animals;
/* File name : MammalInt.java */
publicintnoOfLegs() {
return 0;
}
Ans. If a class wants to use another class in the same package, the package name need not be used. Classes in the
same package find each other without any special syntax. A class file can contain any number of import statements.
The import statements must appear after the package statement and before the class declaration.
Example
Here, a class named Boss is added to the payroll package that already contains Employee. The Boss can then refer to
the Employee class without using the payroll prefix, as demonstrated by the following Boss class.
package payroll;
public class Boss {
public void payEmployee(Employee e) {
e.mailCheck();
}
}
What happens if the Employee class is not in the payroll package? The Boss class must then use one of the
following techniques for referring to a class in a different package.
The fully qualified name of the class can be used. For example −
payroll.Employee
The package can be imported using the import keyword and the wild card (*). For example −
import payroll.*;
The class itself can be imported using the import keyword. For example −
import payroll.Employee;
Ans. There are two types of modifiers in java: access modifiers and non-access modifiers.
The access modifiers in java specifies accessibility (scope) of a data member, method, constructor or class.
1. private
2. default
3. protected
4. public
There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc. Here, we
will learn access modifiers.
If you don't use any modifier, it is treated as default by default. The default modifier is accessible only within
package.
In the above example, the scope of class A and its method msg() is default so it cannot be accessed from outside the
package.
The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the
class.
The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.
In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at
runtime.
Q.11. What is exception handling. What are the advantages of Exception handling.
Ans. Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc.
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.
Ans. Java is an object oriented programming language. The exception is object created at the time of
exceptional/error condition which will be thrown from the program and halt normal execution of the program. Java
exceptions object hierarchy is as below:
All exception types are subclasses of the built-in class Throwable. Thus, Throwable is at the top of the exception
class hierarchy. Immediately below Throwable are two subclasses that partition exceptions into two distinct
branches. One branch is headed by Exception. This classic used for exceptional conditions that user programs
should catch. This is also the class that you will subclass to create your own custom exception types. There is an
important subclass of Exception, called Runtime Exception. Exceptions of this type are automatically defined for
the programs that you write and include things such as division by zero and invalid array indexing.
The other branch is topped by Error, which defines exceptions that are not expected to be caught under normal
circumstances by your program. Exceptions of type Error are used by the Java runtime system to indicate errors
having to do with the runtime environment, itself. Stack overflow is an example of such an error. Java’s exceptions
can be categorized into two types:
Checked exceptions
Unchecked exceptions
Generally, checked exceptions are subject to the catch or specify a requirement, which means they require catching
or declaration. This requirement is optional for unchecked exceptions. Code that uses a checked exception will not
compile if the catch or specify rule is not followed.
Errors
Runtime exceptions
1. try
2. catch
3. finally
4. throw
5. throws
EXAMPLE:
Java try block is used to enclose the code that might throw an exception. It must be used within the method.
1. try{
2. //code that may throw exception
3. }catch(Exception_class_Name ref){}
1. try{
2. //code that may throw exception
3. }finally{}
2. Java finally block is a block that is used to execute important code such as closing connection, stream etc. Java
finally block is always executed whether exception is handled or not. Java finally block follows try or catch
block.
Ans. A stream can be defined as a sequence of data. There are two kinds of Streams −
Java provides strong but flexible support for I/O related to files and networks but this tutorial
covers very basic functionality related to streams and I/O.
Standard Input − This is used to feed the data to user's program and usually a keyboard is used as
standard input stream and represented as System.in.
Standard Output − This is used to output the data produced by the user's program and usually a computer
screen is used for standard output stream and represented as System.out.
Standard Error − This is used to output the error data produced by the user's program and usually a
computer screen is used for standard error stream and represented as System.err.
Ans. Java Swing is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is
built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java. Unlike AWT, Java Swing
provides platform-independent and lightweight components. The javax.swing package provides classes for java
swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
Q.19. Expalin the difference between java awt and java swing
There are many differences between java awt and swing that are given below.