Professional Documents
Culture Documents
8. What is a Class?
A class is a blue print from which individual objects are created. A class can contain
fields and methods to describe the behavior of an object.
9. What is an Object?
Object is a runtime entity and its state is stored in fields and behavior is shown via
methods. Methods operate on an object's internal state and serve as the primary
mechanism for object-to-object communication.
Member
Visibility
Accessible to: Public protected package private
Defining class Yes Yes Yes Yes
Class in same package Yes Yes Yes No
Subclass in different package Yes Yes No No
Non-subclass different package Yes No No No
12. What are Local Variables, Instance Variables, and Class Variables?
Variables defined inside methods, constructors or blocks are called local variables. The
variable will be declared and initialized within the method and it will be destroyed when
the method has completed.
Instance variables are variables within a class but outside any method. These variables
are instantiated when the class is loaded.
Class Variables are declared with in a class, outside any method, with the static keyword.
In above example the child has inherit its family name from the parent class just by
inheriting the class. When child object is created printMyName() present in child class is
called.
16. Polymorphism
Polymorphism in Java has two types: Compile time polymorphism (static binding) and
Runtime polymorphism (dynamic binding). Method overloading is an example of static
polymorphism, while method overriding is an example of dynamic polymorphism.
An important example of polymorphism is how a parent class refers to a child class
object. In fact, any object that satisfies more than one IS-A relationship is polymorphic
in nature.
For instance, lets consider a class Animal and let Cat be a subclass of Animal. So,
any cat IS animal. Here, Cat satisfies the IS-A relationship for its own type as well as its
super class Animal.
Note: Its also legal to say every object in Java is polymorphic in nature, as each one
passes an IS-A test for itself and also for Object class.
Static Polymorphism:
In Java, static polymorphism is achieved through method overloading. Method
overloading means there are several methods present in a class having the same name but
different types/order/number of parameters.
At compile time, Java knows which method to invoke by checking the method
signatures. So, this is called compile time polymorphism or static binding. The
concept will be clear from the following example:
class DemoOverload{
It should be noted that in the first call to move(), the reference type is Vehicle and
the object being referenced is MotorBike. So, when a call to move() is made, Java
waits until runtime to determine which object is actually being pointed to by the
reference. In this case, the object is of the class MotorBike. So, the move() method
of MotorBike class will be called. In the second call to move(), the object is of the
class Vehicle. So, the move() method of Vehicle will be called.
As the method to call is determined at runtime, this is called dynamic binding or late
binding.
An object in Java that passes more than one IS-A tests is polymorphic in nature
Every object in Java passes a minimum of two IS-A tests: one for itself and one for Object
class
Interface can have only abstract methods. Abstract class can have concerete and
abstract methods.
The abstract class can have public, private, protected or default variables and also
constants. In interface the variable is by default public final. In nutshell the
interface doesnt have any variables it only has constants.
A class can extend only one abstract class but a class can implement multiple
interfaces. Abstract class doesn't support multiple inheritance whereas abstract
class does.
If an interface is implemented its mandatory to implement all of its methods but if
an abstract class is extended its mandatory to implement all abstract methods.
The problem with an interface is, if you want to add a new feature (method) in its
contract, then you MUST implement those method in all of the classes which
implement that interface. However, in the case of an abstract class, the method can
be simply implemented in the abstract class and the same can be called by its
subclass.
22. Explain with example to describe when to use abstract class and interface?
Consider a scenario where all Cars will have 4 tyres and other features can be different.
In this case any subclass of Car has to have 4 tyres. This is a case where abstract class
will be used and a default implementaion for tyres will be provided.
public abstract class Car {
private final static TOTAL_TYRES = 4;
public abstract String getCarName();
public final int getNoOfTyres() {
return TOTAL_TYRES;
}
}
Consider a scenario where Cars can have any number of tyres and other features can also
be different. In this case interface will be created.
public interface Car {
public abstract String getCarName();
public abstract int getNoOfTyres();
}
25. Explain the scenerios to choose between String , StringBuilder and StringBuffer
If the Object value will not change in a scenario use String Class because a String object is
immutable.
If the Object value can change and will only be modified from a single thread, use a
StringBuilder because StringBuilder is unsynchronized(means faster).
If the Object value may change, and can be modified by multiple threads, use a
StringBuffer because StringBuffer is thread safe(synchronized).
The code in which the exception may occur is enclosed in a try block, also called as a
guarded region.
The catch clause matches a specific exception to a block of code which handles that
exception.
And the clean up code which needs to be executed no matter the exception occurs or not
is put inside the finally block
Throwable is a parent class of all Exception classes. There are two types of Exceptions:
Checked exceptions and UncheckedExceptions or RunTimeExceptions. Both type of
exceptions extends Exception class.
Runtime exceptions need not be explicitly caught in try catch block as it can occur
anywhere in a program, and in a typical one they can be very numerous. Having to add
runtime exceptions in every method declaration would reduce a program's clarity. Thus,
the compiler does not require that you catch or specify runtime exceptions (although you
can). The solution to rectify is to correct the programming logic where the exception has
occurred or provide a check.
32. What is checked exception?
Checked exception are the exceptions which forces the programmer to catch them
explicitly in try-catch block. It is a subClass of Exception. Example: IOException.
While exceptions are conditions that occur because of bad input or human error etc. e.g.
FileNotFoundException will be thrown if the specified file does not exist. Or a
NullPointerException will take place if you try using a null reference. In most of the
cases it is possible to recover from an exception (probably by giving user a feedback for
entering proper values etc.)
34. Once the control switches to the catch block does it return back to the try block
to execute the balance code?
No. Once the control jumps to the catch block it never returns to the try block but it goes
to finally block(if present).
35. Where is the clean up code like release of resources is put in try-catch-finally
block and why?
The code is put in a finally block because irrespective of try or catch block execution the
control will flow to finally block. Typically finally block contains release of connections,
closing of result set etc.
37. How do you get the descriptive information about the Exception occurred
during the program execution?
All the exceptions inherit a method printStackTrace() from the Throwable class. This
method prints the stack trace from where the exception occurred. It prints the most
recently entered method first and continues down, printing the name of each method as it
works its way down the call stack from the top.
38. Why is not considered as a good practice to write a single catch all handler to
catch all the exceptions?
You can write a single catch block to handle all the exceptions thrown during the
program.
If you use the Superclass Exception in the catch block then you will not get the valuable
information about each of the exception thrown during the execution, though you can
find out the class of the exception occurred. Also it will reduce the readability of the code
as the programmer will not understand what is the exact reason for putting the try-catch
block.
40. What happens if the handlers for the most specific exceptions is placed above the
more general exceptions handler?
Compilation fails. The catch block for handling the most specific exceptions must always
be placed above the catch block written to handle the more general exceptions.
//The code below will not compile.
try {
// code that can throw IOException or its subtypes
} catch (IOException e) {
// handles IOExceptions and its subtypes
} catch (FileNotFoundException ex) {
// handle FileNotFoundException only
}
41. Does the order of the catch blocks matter if the Exceptions caught by them are
not subtype or supertype of each other?
No. If the exceptions are siblings in the Exception classs hierarchy i.e. If one Exception
class is not a subtype or supertype of the other, then the order in which their
handlers(catch clauses) are placed does not matter.
42. What is a Thread?
In Java, "thread" means two different things:
An instance of Thread is justan object. Like any other object in Java, it has variables
and methods, and lives and dies on the heap. But a thread of execution is an individual
process (a "lightweight" process) that has its own call stack. In Java, there is one thread
per call stackor, to think of it in reverse, one call stack per thread. Even if you don't
create any new threads in your program, threads are back there running.
The main() method, that starts the whole ball rolling, runs in one thread, called
(surprisingly) the main thread. If you looked at the main call stack (and you can, any time
you get a stack trace from something that happens after main begins, but not within
another thread), you'd see that main() is the first method on the stack the method at the
bottom. But as soon as you create a new thread, a new stack materializes and methods
called from that thread run in a call stack that's separate from the main() call stack.