Can a class be declared both abstract and final ? If not, why ?
We can not declare a class both abstract and final.
This is because, the keyword abstract represents an incomplete class which depends on the subclasses for its implementation. Creating a subclass is compulsory for a abstract class .
final keyword presents inheritance. So, we can not create a subclass of a final class.
Therefore both abstract and final can not be used together for the same class.
Explain Dynamic method dispatch in java.
This is a mechanism by which a call to an overridden method is resolved at run time, rather than at compile time.
A superclass reference variable can refer to a subclass object. Java uses this to resolve calls to overridden methods at run time.
If a superclass contains a method that is overridden by a subclass, then when different types of objects are referred through a superclass reference variable, different versions of the method are executed.
The determining factor is the type of the object that is being referred and not the type of the reference variable.
Difference between an abstract class and interface.
|Abstract class is used when there is some common features to be shared across all extending classes.||An interface is written when the features are to be implemented differently by different classes.|
|An abstract class can contain some abstract methods and some concrete methods.||Priot to JDK 8, interface could only contain abstract methods. Starting JDK 8, interface can can also contain default implementation of the methods.|
|Abstract class can contain normal variables as member.||Interface variables can only be constants.|
|Abstract class is declared using the keyword abstract||Interface is declared using the keyword interface.|
How the Garbage collector can be invoked ?
Garbage collector is invoked by the JVM when it needs to collect some memory. We can request garbage collector to be invoked by using gc() method of Runtime or System class as :
Can we skip the argument(String args) for the main method ?
Removing the String args from main() method will result in Compiler treating it as any other normal method. But your program needs a main method with String argument to run.
Why is main method marked static ?
Main() being static, helps the compiler to invoke it in a class independent manner.
What is the difference between print() and println() methods ?
Print() method displays the data and retains the cursor in the same line, where as println() displays the result and moves the cursor to the next line.
In System.out.println(), explain which is a class, which is instance and static or nonstatic methods.
System represents java.lang.System class. It has final static PrintStream field out.
Println() is nonstatic method of PrintStream which is invoked using the instance out.
Which part of JVM allocates memory for a Java Program?
Classloader subsystem of JVM is responsible for memory allocation for a java program.
© 2015 – 2016, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post