You are on page 1of 2

Question : When you declare a method as abstract method ?

Answer : When i want child class to implement the behavior of the method.

Question : Can I call a abstract method from a non abstract method ?

Answer : Yes, We can call a abstract method from a Non abstract method in a Java abstract
class

Question : What is the difference between an Abstract class and Interface in Java ? or
can you explain when you use Abstract classes ?

Answer : Abstract classes let you define some behaviors; they force your subclasses to
provide others. These abstract classes will provide the basic funcationality of your
applicatoin, child class which inherited this class will provide the funtionality of the
abstract methods in abstract class. When base class calls this method, Java calls the
method defined by the child class.

• An Interface can only declare constants and instance methods, but cannot
implement default behavior.
• Interfaces provide a form of multiple inheritance. A class can extend only one
other class.
• Interfaces are limited to public methods and constants with no
implementation. Abstract classes can have a partial implementation,
protected parts, static methods, etc.
• A Class may implement several interfaces. But in case of abstract class, a
class may extend only one abstract class.
• Interfaces are slow as it requires extra indirection to find corresponding
method in the actual class. Abstract classes are fast.

Neither Abstract classes or Interface can be instantiated.

Question : What is user-defined exception in java ?

Answer : User-defined expections are the exceptions defined by the application developer
which are errors related to specific application. Application Developer can define the
user defined exception by inherite the Exception class as shown below. Using this
class we can throw new exceptions.

Java Example :

public class noFundException extends Exception {


}

Throw an exception using a throw statement:

public class Fund {

...
public Object getFunds() throws noFundException {

if (Empty()) throw new noFundException();


...
Question Describe
Explain garbage collection ?
: the
wrapper
classes inpart of Java's security
Answer : Garbage collection is an important
Java
strategy. Garbage collection is ?
also called automatic
memory management as JVM automatically removes the
unused variables/objects Answer
from the: memory.
WrapperTheclass
nameis wrapper around a primitive data type.
"garbage collection" implies that objects
An instance
that are ofnoa wrapper class contains, or wraps, a
longer needed by the program are "garbage"
primitive and
valuecanof be
the corresponding type.
thrown away. A more accurate and up-to-date metaphor
might be "memory recycling." When Following
an object table
is no lists the primitive types and the
longer referenced by the program, the corresponding
heap space itwrapper classes:
occupies must be recycled so that the space is available
for subsequent new objects. The garbage collector
Primitive must
Wrapper
somehow determine which objects are no longer
referenced by the program and makeboolean java.lang.Boolean
available the heap
space occupied by such unreferencedbyteobjects. In the
java.lang.Byte
process of freeing unreferenced objects, the garbage
char beingjava.lang.Character
collector must run any finalizers of objects freed.
double java.lang.Double
In Java, it is good idea to explicitly
float assign null into
java.lang.Float
a variable when no more in use.
int java.lang.Integer
long java.lang.Long
Question
short java.lang.Short
How you can force the garbage collection ?
: void java.lang.Void

Answer : Garbage collection automatic process and can't be forced.


We can call garbage collector in Java by calling
Question
System.gc() and Runtime.gc(), JVM: tries
What toare different
recycle the types of inner classes ?
unused objects, but there is no guarantee when all the
objects will garbage collected.
Answer : Inner classes nest within other classes. A normal class
is a direct member of a package. Inner classes, which
became available with Java 1.1, are four types

Question What are the field/method access levels


• Static member classes
: (specifiers) and class access levels ?
• Member classes
• Local classes
Answer : Each field and method has an access level:
• Anonymous classes

• private: accessible only in this class


Static member classes - a static member class is a
• (package): accessible only in static
this package
member of a class. Like any other static method,
• protected: accessible only in this package
a static and class
member in has access to all static methods
all subclasses of this class of the parent, or top-level, class.
• public: accessible everywhere this class is
available Member Classes - a member class is also defined as a
member of a class. Unlike the static variety, the
memberaccess
Similarly, each class has one of two possible class is instance specific and has access to any
levels: and all methods and members, even the parent's this
reference.
• (package): class objects can only be declared and
Local Classes - Local Classes declared within a block
manipulated by code in this package
of code and
• public: class objects can be declared and these classes are visible only within the
block.
manipulated by code in any package

Anonymous Classes - These type of classes does not


For both fields and classes, package
have access is theand its like a local class
any name
default, and is used when no access is specified
Java Anonymous Class Example

Question public class SomeGUI extends JFrame

You might also like