Engineering Full Stack Apps with Java and JavaScript
This page will contain some of the frequently asked questions.
1. Is Java platform independent? Explain.
Yes. Refer to the article features-of-java.
2. Is Java a compiled language or interpreted Language?
Java is a compiled interpreted language. Refer to the article java-is-a-compiled-and-interpreted-language
3. What is the difference between path and classpath?
Please refer to the article path-vs-classpath.
4. Explain the object oriented principles with examples?
Object oriented principles are Encapsulation, Inheritance, Polymorphism and Abstraction. Refer to the article object-oriented-programming-oop-concepts-with-examples.
5. Which all access modifiers can be applied to a top level class in java?
A regular top level class can be public or default (without any modifiers). A top level class is one that is directly under the package (including default package) and does not come inside any class. A class that comes inside another class is called an inner class and can have any access modifier a class member can have.
6. Why regular top level class cannot be private or protected?
A regular top level class cannot be private or protected mainly because there is no need to do so.
7. Can we have more than one main method in a class?
Yes, we have more than one main method in our class through overloading just like any other method. The main method is just like any other method, but the signature public static void main(String args[]) is the only one known to the JVM and hence it looks for a method with that signature only. We should call the other main methods ourselves just like any other method.
8. What is the significance of public, static, void and String args[] in main method signature?
The signature of main method, which is the entry point to a java application, is public static void main(String args[])
9. What are Command Line Arguments?
Command Line Arguments are Arguments passed through command line. Command line is the line in which you type a command in DOS or a shell prompt and is not specific to java. In Java we can pass command line arguments to a program as:
java MyClass arg1 arg2 arg3
The String array args[] (name doesn’t matter) in the main method will have these populated.
10. Are static methods inherited?
Yes, static methods are inherited, but they are NOT polymorphic, or in simple terms, they are NOT overriden, unlike instance methods which are polymorphic and overriden.
Through polymorphism, we mean overriding. In java, only instance methods are overridden. Static methods, static variables and instance variables are just re-declared (hiding parent method) in the child class, but not overridden.
11. Are default variables and methods inherited?
Default variables and methods are inherited only if both parent and child are in same package.
12. How can you use annotations to make sure you are doing a valid override?
In java 5 and above, we can confirm whether we are doing valid override by using the @Override annotation above the subclass's method. If not valid override, compiler will throw error.
For example, compiler will always throw error when @override is given over static methods in subclasses even though the name and signature matches exactly with the parent method. This is because static methods are just re-declared (hiding parent method) in the child class, but not overridden.
13. What is RTTI in Java?
RTTI stands for runtime type identification. RTTI lets you find the exact type of an object at runtime, for example when you have a base type reference referring to a subclass object.
Two common forms of RTTI are:
However these two requires that you have all the types available at compile-time and run-time.
Reflection is another form of runtime type identification (RTTI) which allows you to discover class information solely at run-time.
14. Differentiate between JDK and JRE?
JDK stands for Java Development Kit, contains javac.exe and is used for compiling java source. JRE stands for Java Runtime Environment, contains java.exe and is used for executing java programs. Refer to jdk-jre-jvm-and-your-first-java-program
15. Explain initialization blocks in java?
Initialization blocks are blocks called automatically by java for initializing a class similar to constructors. Refer to blocks-and-methods-in-java.
16. How can you set and get system properties?
We can use java -D to set a system property. System properties consist of name=value pairs and must be appended directly behind the -D, for example,
java -Dmyproperty=myvalue.
Now you can get it from within the code as
System.getProperty("myproperty"); or
System.getProperties().getProperty("myproperty");
Comments
Reflection in java
Heartin,
Could you please explain reflection with one example? When it will be used and its advantages
Thanks in advance
Chinnu