Java Interview Questions : Series 4



 51. What is meant by covariant return

type in Java?

A covariant return type of a method is one that can be replaced by a

"narrower" type when the method is overridden in a subclass.

Let say class B is child of class A. There is a get() method in class

A as well as class B. get() method of class A can return an instance

of A, and get() method of class B return an instance of B. Here

class B overrides get() method, but the return type is different.

Before Java 5, any method that overrides the method of parent class

would have same return type.

From Java 5 onwards, a child class can override a method of parent

class and the child class method can return an object that is child of

object return by parent class method.

Polymorphism


52. What is Runtime Polymorphism?

Runtime Polymorphism or Dynamic Polymorphism is the

polymorphism that exists at runtime. In case of method overriding it

is not known which method will be called at runtime. Based on the

type of object, JVM decides the exact method that should be called.

So at compile time it is not known which method will be called at

run time.


53. Is it possible to achieve Runtime

Polymorphism by data members in

Java?

No. We need to create Runtime Polymorphism by implementing

methods at two levels of inheritance in Java.

54. Explain the difference between

static and dynamic binding?

In Static binding references are resolved at compile time. In

Dynamic binding references are resolved at Run time.

E.g.

Person p = new Person();

p.walk(); // Java compiler resolves this binding at compile time.

public void walk(Object o){

((Person) o).walk(); // this is dynamic binding.

}


55. What is Abstraction in Object

Oriented programming?

Abstraction is the process of hiding certain implementation details

of an object and showing only essential features of the object to

outside world.

It is different from Abstract class in Java.

Abstraction process identifies commonalities and hides the

complexity of implementation. It helps us in focusing on the

interface that we share with the outside world.


56. How is Abstraction different from

Encapsulation?

Abstraction happens at class level design. It results in hiding the

implementation details. Encapsulation is also known as

“Information Hiding”. An example of encapsulation is marking the

member variables private and providing getter and setter for these

member variables.


57. What is an abstract class in Java?

An abstract class in Java has one or more abstract methods. An

abstract method is just declared in the abstract class, but it is not

implemented.

An abstract class has to be extended in Java and its abstract

methods have to be implemented by a child class. Also Java does

not allow new instance of Abstract class.


58. Is it allowed to mark a method

abstract method without marking the

class abstract?

No. Java specification says that if there is at least one abstract

method in a class, the class has to be marked abstract.


59. Is it allowed to mark a method

abstract as well as final?

No. It will be contradictory statement to mark a method abstract as

well as final.

An abstract method has to be overridden by a child class. And a

final method cannot be overridden. Therefore a method can be

either abstract or final in Java.


60. Can we instantiate an abstract

class in Java?

No. We cannot create an instance of an abstract class in Java.


61. What is an interface in Java?

An Interface in Java is an abstract type blueprint of a class. It

contains the methods that a class must implement. It is like a

protocol.

It has method signatures and constant declarations.


62. Is it allowed to mark an interface

method as static?

Yes, from Java 8 onwards, we can define static and default methods

in an interface. Prior to Java 8, it was not allowed.


63. Why an Interface cannot be

marked as final in Java?

A final method cannot be overridden. But an interface method has to

be implemented by another class. So the interface method cannot be

marked as final.


64. What is a marker interface?

There are interfaces that do not have any data member or methods.

These interfaces are called Marker interface.

E.g. Serializable, Cloneable, Remote etc.


65. What can we use instead of

Marker interface?

We can use annotations instead of Marker interface.


66. How Annotations are better than

Marker Interfaces?

Annotations serve the purpose of conveying metadata about the

class to its consumers without creating a separate type for it.

Annotations are more powerful than a Marker interface. They allow

programmers to pass more sophisticated information to classes that

"consume" it.


67. What is the difference between

abstract class and interface in Java?

Differences between Abstract class and Interface are as follows:

1. An abstract class can have implemented methods with

body (non-abstract methods). Interface has only abstract

methods. From Java 8 onwards, interface can have

static/default methods in implemented form.

2. An abstract class can have instance member variables. An

interface cannot have instance variables. It can only have

constants.

3. An abstract class can have a constructor. Interface cannot

have constructor. It has to be implemented by another

class.

4. A class can extend only one abstract class. A class can

implement more than one interface.


68. Does Java allow us to use private

and protected modifiers for variables

in interfaces?

No. All the variables in an interface are implicitly public.


69. How can we cast to an object

reference to an interface reference?

An Object that implements an Interface can be cast to the same

Interface. Since An Object implementing an Interface already

provides implementation for the methods of that Interface, it is

allowed to do so as per the rules of Inheritance.


70. How can you change the value of a

final variable in Java?

Java does not allow changing the value of a final variable. Once the

value is set, it cannot be changed.


71. Can a class be marked final in

Java?

Yes a class can be marked final in Java. Once a class is marked

final, it cannot be extended.


72. How can we create a final method

in Java?

To mark a method, add modifier final to that method. A final method

can not be overridden by a child class.


73. How can we prohibit inheritance

in Java?

If you mark a class final, it cannot be extended. This will prohibit

the inheritance of that class in Java.


74. Why Integer class in final in Java?

Integer class is a wrapper for int. If it is not marked final, then any

other class can extend it and modify the behavior of Integer

operations. To avoid this Integer wrapper class is marked as final.


75. What is a blank final variable in

Java?

When we declare a final variable without giving any initial value,

then it is called blank final variable.


76. How can we initialize a blank final

variable?

A blank final instance variable can be initialized in a constructor.

A blank final static variable can be initialized in the static block of

class.


77. Is it allowed to declare main

method as final?

Yes, we can mark the main method as final.


Comments

Popular posts from this blog

3 Lines of Code And You Can Download Your Favourite Youtube Video.

Leetcode Problem(Easy) Roman to Integer

Leetcode : (Find Minimum in Rotated Sorted Array)