1. What is an inetface?
An interface is a list of methods that must be defined by any class which implements that interface. It may also define constants (public static final). Interfaces are declared using the interface keyword, and may only contain method signatures and constant declarations.
2. What are the features of interfaces?
An interface may never contain method definitions.
· As interfaces are implicitly abstract, they cannot be directly instantiated.
· In an interface, all methods are implicitly public.
· In an interface, all variables are static and final by default
· A class can implement many different interfaces.
· One interface can extend another interface.
· A class can simultaneously extend a class and implement multiple interfaces
· One benefit of using interfaces is that they simulate multiple inheritances.
3. How multiple inheritances is achieved in Java?
Java does not provide an explicit way to implement multiple-inheritance i.e. a class can never extend multiple classes. But, this can be achieved (simulated) using interfaces. This is possible because of two reasons:
a. A class can implement multiple interfaces
b. A class can simultaneously extend a class and implement one or more interfaces
4. What are packages in Java?
In simple terms, package is a group of classes created in a unique directory. Upon importing a package in a program, we will be able to access the classes defined in that package.
5. What are the advantages of packages?
• The classes contained in the packages of other programs can be easily reused.
• In packages, classes can be unique compared with classes in other packages. That is, two classes in two different packages can have the same name.
• Packages provide a way to "hide" classes thus preventing other programs or packages from accessing classes.
• A package may have another sub package.
6. How can you compile a package?
Package is a group of classes created in a unique directory. To compile a package i.e. all its classes, we should move to the directory and issue “javac” command as shown in the following syntax. Assume that the package name (i.e. directory name is ‘p1’)
C:/>jdk1.5/bin/p1>Javac *.java
7. Write short notes on Java API packages.
Java API provides a large number of classes grouped into different packages according to functionality. Most of the time, we use the packages available with the java API. The following diagram shows different packages and the table displays the contents of the packages.
java.lang | This package has primary classes and interfaces essential for java program. It consists of wrapper classes, strings, threads, etc. The classes defined in this package are imported by default. |
java.util | (utility) in this package utility classes like data structures are available. This package contains useful classes and interfaces like stack, linked list, hash table, arrays. etc. |
java.io | This package has file handling classes which can be used to perform input output related tasks |
java.awt | Abstract window tool kit. This package helps to develop GUI. It consists of classes for windows, buttons, lists and etc. |
java.net | This has classes for networking. Client – server programming can be done using this package. Net stands for network. |
java.applet | Classes for creating and implementing Applets. |
java.sql | Structured query language. This package has classes that help to connect to database like oracle. |
util |
io |
awt |
net |
applet |
API Packages |
8. How can a package with multiple classes?
If there are more non-public classes in a same program, they will not become the members of a package. But, to have multiple classes in a single package, the directory is going to be same; while the classes are to be written in different programs.
0 comments:
Post a Comment