2016年10月23日星期日

Java Interview

Q) What all memory areas are allocated by JVM?
Heap, Stack, Program Counter Register and Native Method Stack


Types of polymorphism in java- Runtime and Compile time polymorphism

Q) Can we override a static method?
No, we cannot override a static method.
Overriding depends on having an instance of a class. The point of polymorphism is that you can subclass a class and the objects implementing those subclasses will have different behaviors for the same methods defined in the superclass (and overridden in the subclasses). A static method is not associated with any instance of a class so the concept is not applicable.

Q) Is it possible to overload main() method of a class?
Yes, we can overload main() method as well.

private and final methods can be overloaded but they cannot be overridden.

An overridden method may have a more specific return type. That is, as long as the new return type is assignable to the return type of the method you are overriding, it's allowed.
For example:
class ShapeBuilder {
    ...
    public Shape build() {
    ....
}

class CircleBuilder extends ShapeBuilder{
    ...
    @Override
    public Circle build() {
    ....
}

没有评论:

发表评论