Abstract Class and Interfaces in Java
1.What is abstract method ?
A. An abstract method is a method without method body. An abstract method is written when the
same method has to perform difference tasks depending on the object calling it.
2. What is abstract class?
A. An abstract class is a class that contains 0 or more abstract methods.
3. How can you force your programmers to implement only the features of your
A. By writing an abstract class or an interface.
4. Can you declare a class as abstract and final also ?
A. No, abstract class needs sub classes. final key word represents sub classes which can not be
created. So, both are quite contradictory and cannot be used for the same class.
5.What is an interface ?
A. An interface is a specification of method prototypes, All the methods of the interface are public
6.Why the methods of interface are public and abstract by default ?
A. Interface methods are public since they should be available to third party vendors to provide
implementation. They are abstract because their implementation is left for third party vendors.
7. Can you implement one interface from another ?
A. No, we can’t implementing an interface means writing body for the methods. This can not be
done again in an interface, since none of the methods of the interface can have body.
8. Can you write a class within an interfae ?
A. Yes, it is possible to write a class within an interface.
9.Explain about interfaces ?
A. * An interface is a specification of method prototypes, before we proceed furthur, written in the
interface without mehtod bodies.
*An interface will have 0 or more abstract methods which are all public and abstract by default.
* An interface can have variables which are public static and final by default. This means all the
variables of the interface are constants.
10.What is the difference between an abstract class and an interface ?
Abstract class Interface
An abstract class is written when there are some common features shared by all the
objects. An interface is written when all the features are implemented differently in different objects.
When an abstract class is written, it is the duty of the programmer to provide sub classes to it.
An interface is written when the programmer wants to leave the implementation to the third
party vendors. An abstract class contains some abstract methods and also some concrete methods.
An interface contains only abstract methods. An abstract class contain instance variables also. An
interface cannot contain instance variables. It contains only constants. All the abstract methods of the
abstract class should be implemented in its sub classes. All the (abstract) methods of the interface
should be implemented in its implementation classes. Abstract class is declared by using the
keyword abstract. Interface is declared using the keyword interface.
11.A programmer is writing the following statements in a program:
1. import java.awt.*;
2. import java.awt.event.*;
Should he write both the statements in his program or the first onw is enough ?
A. event is a sub package of java.awt package. But, when a package is imported, its sub packages
are not automatically imported into a program. So, for every package or sub package, a separate
import statement should be written. Hence if the programmer wants the classes and interfaces of both
the java.awt and java.awt.event packages, then he should both the preceding statements in his
12. How can you call the garbage collector ?
A. We can call garbage collector of JVM to delete any unused variables and unreferenced objects
from memory using gc( ) method. This gc( ) method appears in both Runtime and System classes of
java.lang package. For example, we can call it as:
Runtime.getRuntime( ).gc( );
13.What is the difference between the following two statements.
1. import pack.Addition;
2. import pack.*;
A. In statement 1, only the Addition class of the package pack is imported into the program and in
statement 2, all the classes and interfaces of the package pack are available to the program.
If a programmer wants to import only one class of a package say BufferedReader of java.io package,
we can write import java.io.BufferedReader;
14. What is the differentiate between .ear, .jar and .war files.?
A. These files are simply zipped file using java jar tool. These files are created for different
purposes. Here is the description of these files: .jar files: These files are with the .jar extenstion. The
.jar files contains the libraries, resources and accessories files like property files. .war files: These files
are with the .war extension. The war file contains the web application that can be deployed on the any
development of web applications. .ear files: The .ear file contains the EJB modules of the application.
15. What is a JAR file ?
A. A Java Archive file (JAR) is a file that contains compressed version of several .class files, audio
files, image files or directories. JAR file is useful to bundle up several files related to a project and use
16.What is the scope of default acess specifier ?
A. Default members are available within the same package, but not outside of the package. So their
scope is package scope.
17.What happens if main( ) method is written without String args[ ] ?
A. The code compiles but JVM cannot run it, as it cannot see the main( ) meth