Java Basics interview questions
Which class is Superclass of all classes?
java.lang.Object is the root class for all the java classes and we don’t need to extend it.
To start with Class and Object are not Java concepts. They are the key terms that are used to define a very popular and one of the most appreciated ways of programming called the “Object Oriented Programming”.
Or you can imagine Object class in Java programming language as the super cosmic class. It is directly or indirectly inherited by each and every class.
Can we declare constructor as private ?
Yes we can declare the constructor as private. But we cannot create instances of the class from other
classes. So think of a scenario where you want to take the responsibility to create an instance of a class
and then others can use your instance. Let's see an example.
If we look at the singleton design pattern where we can only create one instance of that class there we
need to declare our constructor as private.
Why do we create an object in java?
Consider a class A is like a closed room with many things inside.You are an outsider Class B, wanting to
access something(METHODS) from the class A.You won't be able to directly access anything in a room(A)
as there is no way to go inside . What you need is a way to go inside the room to access the components
(Methods).So we have to create a door (OBJECT) to that room.
The created Door (OBJECT) is especially for you. Only you can use the door (OBJECT). No person from
another Room (CLASS) can access through the Door (OBJECT) created by you.
He has to create his own Door (OBJECT).If you are already inside room A, you don't need a door to
access the things inside the room where you are in. i.e., To access a Method in a class from within the
class itself, you don't have to create an object(door). You can directly access the things (METHODS) by
its name.
What are JDK, JVM, JRE?
What is byte code in Java?
Byte Code is the code generated by the JVM after compiling the program.
It acts as the intermediate code because it has to be interpreted and executed by the JVM installed
on a specific platform. It is the JVM which makes resource necessary calls to the processor in order
to run the byte code. Byte code makes the java platform independent.
No comments: