1. What
is Nested class or Inner class?
Defining
a class within another class is called Nested / Inner class.
Nested
classes are divided into 2 Categories,
- static
- non-static
Eg:
class OuterClass
{
...
static class StaticNestedClass
{
...
}
class InnerClass {
...
}
·
Nested class is a member of it's outer or enclosing class.
·
non-static nested classes have access to other members of the enclosing
class, even they are declared as 'private'.
·
static nested classes do not have access to other members of the
enclosing class.
·
As a member of outer class a nested class can be declared private,public and protected or package-private(if
a class has no modifier it is visible within own package, this is also known as
package-private).
Uses of nested classes:
It is a way of Logically
grouping classes that are only used in one place - If a class is
useful to only one other class, then it is logical to Embed it in that class
and keep the two classes together.Nesting such 'helper classes' makes their
package streamlined.
It Increases
Encapsulation: Consider there are two high-level classes A,B.
Where B needs access to members of A. By hiding
class B within class A , A's
members can be declared private and B can access them. In
addition B itself can be hidden from outside world.
It can lead more
reliable and maintainable code: nesting small classes within Top-Level classes places the
code closer to where it used.
static nested classes:
·
Like static class methods, a static nested class can not refer
directly to instance variables or methods defined in it's enclosing class.It
can use them only through an object reference.
·
In java we can not make Top level class as static, only nested
classes can be static.
·
An instance of inner class can not be created without an instance
of outer class.
·
static nested classes are accessed using the enclosing class name.
OuterClass.StaticNestedClass
To create
an object for static nested class
OuterClass.StaticNestedClass staticnestedclsobj=new
OuterClass.StaticNestedClass();
To Create Object for
non-static inner class
OuterClass.NestedClass
nestedclsobj=new OuterClass().new NestedClass();
2. Is Java pure Object
Oriented language?
Java is not Pure Object
Oriented Language, because it supports Primitive datatypes such as
int, byte, long... etc,
which are not objects.This Contrast with a 'pure OOP language' like Smalltalk,
where there are no primitive types, and boolean, int and methods are all
objects.
3. Difference Between
JDK,JVM and JRE ?
JDK: Java Development
Kit (JDK) is for development purpose and JVM is a part of it. JDK provides all
the tools, executables and binaries required to compile, debug and execute a
Java Program.
JVM: Java Virtual
Machine(JVM) is Executes to Java Programs.It is responsible for Execution of
Java Programs.
JRE: Java Runtime
Environment (JRE) is the implementation of JVM. JRE consists of JVM and java
binaries and other classes to execute any program successfully. JRE doesn't
contain any development tools like java compiler, debugger etc.
4. Difference between HashSet and TreeSet in Java?
1.
First major difference
between HashSet and TreeSet is performance. HashSet is
faster than TreeSet.
2.
In HashSet and TreeSet the HashSet allows
null object but TreeSet doesn't allow null Object and
throw NullPointerException, Why, because TreeSet uses compareTo() method to compare keys
and compareTo() will throw java.lang.NullPointerException.
3.
HashSet is backed by HashMap while TreeSetis
backed by TreeMap.
4.
HashSet uses equals()
method to compare two object in Set and for detecting
duplicates while TreeSet uses compareTo() method
for same purpose.
5.
HashSet doesn't guaranteed any order while TreeSet maintains
objects in natural Sorted order or the Order defined by either Comparable or Comparator method
in Java.
Important*-The TreeSet
allows NULL object while it is Empty. If it is not empty then
only it doesn't allows NULL Object.
HashSet.
|
TreeSet.
|
Faster.
|
Slower(when compared to HashSet).
|
Doesn't Guaranteed Any Order.
|
Natural Sorted Order or Order
Provided by the Comparable/Comparator.
|
Uses equals() method to compare
elements.
|
Uses compareTo() method to compare
elements.
|
backed by HashMap.
|
backed by TreeMap.
|
5. What are the drawbacks of JDBC ?
- JDBC uses the software dependent sql queries,so the JDBC Persistence logic is database software dependent.
- All Exceptions of JDBC are checked exceptions, so the Exception Handling is Mandatory.
- There is no proper support of Transaction Management.
- There is no built-in support of Caching/Buffering to reduce network round trips between Java App & Database.
- We cannot keep objects in relationship while underlying Database tables are in relationship.
0 comments:
Post a Comment