0% found this document useful (0 votes)
100 views

Worksheet: Q.1. What Is An Interface. Explain The Characteristics of The Interface

An interface is a collection of abstract methods that is implemented by a class. It defines behaviors that a class implements. An interface can contain constants, default methods, static methods, and nested types in addition to abstract methods. A class implements an interface using the implements keyword and must define all of the interface's abstract methods unless the class itself is abstract. Interfaces differ from classes in that interfaces cannot be instantiated, do not contain constructors, have only abstract methods, and cannot contain instance fields.

Uploaded by

Asge Asche
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Worksheet: Q.1. What Is An Interface. Explain The Characteristics of The Interface

An interface is a collection of abstract methods that is implemented by a class. It defines behaviors that a class implements. An interface can contain constants, default methods, static methods, and nested types in addition to abstract methods. A class implements an interface using the implements keyword and must define all of the interface's abstract methods unless the class itself is abstract. Interfaces differ from classes in that interfaces cannot be instantiated, do not contain constructors, have only abstract methods, and cannot contain instance fields.

Uploaded by

Asge Asche
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

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.

An interface is similar to a class in the following ways −

 An interface can contain any number of methods.


 An interface is written in a file with a .java extension, with the name of the interface matching the name of
the file.
 The byte code of an interface appears in a .class file.
 Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that
matches the package name.

However, an interface is different from a class in several ways, including −

 You cannot instantiate an interface.


 An interface does not contain any constructors.
 All of the methods in an interface are abstract.
 An interface cannot contain instance fields. The only fields that can appear in an interface must be declared
both static and final.
 An interface is not extended by a class; it is implemented by a class.
 An interface can extend multiple interfaces.

Q.2. How can we declare the interface.

Ans: The interface keyword is used to declare an interface. Here is a simple example to declare an interface −

Example

/* File name : NameOfInterface.java */


importjava.lang.*;
// Any number of import statements

public interface NameOfInterface {


// Any number of final, static fields
// Any number of abstract method declarations\
}
Q.3. What are the properties of an interface.

Ans: Interfaces have the following properties −

 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

/* File name : Animal.java */


interface Animal {
public void eat();
public void travel();
}

Q.4. How can we implement interfaces.

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

/* File name : MammalInt.java */


public class MammalInt implements Animal {

public void eat() {


System.out.println("Mammal eats");
}

public void travel() {


System.out.println("Mammal travels");
}

Public int noOfLegs() {


return 0;
}

public static void main(String args[]) {


MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
This will produce the following result −

Output

Mammal eats
Mammal travels

When implementation interfaces, there are several rules −

 A class can implement more than one interface at a time.


 A class can extend only one class, but implement many interfaces.
 An interface can extend another interface, in a similar way as a class can extend another class.

Q.5. what are the packages. Explain in details.

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.

Some of the existing packages in Java are −

 java.lang − bundles the fundamental classes


 java.io − classes for input , output functions are bundled in this package

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.

Q.6 How can we create packages.

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.

javac -d Destination_folder file_name.java


Then a folder with the given package name is created in the specified destination, and the compiled class files will
be placed in that folder.

Q.7. write a java program of the packages

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.

Following package example contains interface named animals −

/* File name : Animal.java */


package animals;

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 */

public class MammalInt implements Animal {

public void eat() {


System.out.println("Mammal eats");
}

public void travel() {


System.out.println("Mammal travels");
}

publicintnoOfLegs() {
return 0;
}

public static void main(String args[]) {


MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}

Q.8 What are the role of import keyword

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;

Q.9. what do you understand by access modifier. Explain in details.

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.

There are 4 types of java access modifiers:

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.

2) default access modifier

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.

3) protected access modifier


The protected access modifier is accessible within package and outside the package but through inheritance only.

The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the
class.

4) public access modifier

The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.

Q.10. What is an Exception

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

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.

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.

Q.12. Explain the types of Exception handling.

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.

Unchecked exceptions come in two types:

 Errors
 Runtime exceptions

Q.13. Explain the keywords used in java exception handling.

Ans. There are 5 keywords used in java exception handling.

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. Java try block must be followed by either catch or finally block.

Syntax of java try-catch

1. try{  
2. //code that may throw exception  
3. }catch(Exception_class_Name ref){}  

Syntax of try-finally block

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.

Q.14. what do you understand by stream.

Ans. A stream can be defined as a sequence of data. There are two kinds of Streams −

 InPutStream − The InputStream is used to read data from a source.


 OutPutStream − The OutputStream is used for writing data to a destination.

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.

Q.15. What is Byte Stream.


Ans. Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to
byte streams but the most frequently used classes are, FileInputStream and FileOutputStream.

Q.16.What is character stream


Ans. Java Byte streams are used to perform input and output of 8-bit bytes, whereas Java Character streams are used
to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the
most frequently used classes are, FileReader and FileWriter.
Q.17. what is standard stream.
Ans. All the programming languages provide support for standard I/O where the user's program can take input from
a keyboard and then produce an output on the computer screen. If you are aware of C or C++ programming
languages, then you must be aware of three standard devices STDIN, STDOUT and STDERR. Similarly, Java
provides the following three standard streams −

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

Q.18. What do you understand by java swing.

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

Ans. Difference between AWT and Swing

There are many differences between java awt and swing that are given below.

No. Java AWT Java Swing

1) AWT components Java swing components are platform-independent.


are platform-dependent.

2) AWT components Swing components are lightweight.


are heavyweight.

3) AWT doesn't support Swing supports pluggable look and feel.


pluggable look and feel.

4) AWT provides less Swing provides more powerful componentssuch as


components than Swing. tables, lists, scrollpanes, colorchooser, tabbedpane etc.

5) AWT doesn't follows Swing follows MVC.


MVC(Model View Controller)
where model represents data,
view represents presentation
and controller acts as an
interface between model and
view.

Q.20 What are the hierarchy of java swing class

Ans. The hierarchy of java swing API is given below.

You might also like