Java Interview Questions And Answers: Part-2

Jun 25, 2017 Java, Java Interview Questions, 359 Views
Java, Java Interview Questions And Answers, java interview questions and answers for freshers and experienced

For More Basics Java Interview Questions Follow Link: Click here.

51. Can overloaded methods be synchronized?

Yes. Overloaded methods can be synchronized.

 

52. Can we overload main() method?

Yes, we can overload main() method. A class can have any number of main() methods but execution starts from public static void main(String[] args) only.

 

53. Can we declare overloaded methods as final?

Yes, we can declare overloaded methods as final.

 

54. In the below class, is constructor overloaded or is method overloaded?

public class A

{

public A(){}

void A(){}

}

None of them. It is neither constructor overloaded nor method overloaded. First one is a constructor and second one is a method.

 

55. Overloading is the best example of dynamic binding?

No, Overloading is the best example for static binding

 

56. Can overloaded method be overridden?

Yes, we can override a method which is overloaded in super class.

 

57. What is method overriding?

Modifying a super class method in the sub class is called method overriding. Using method overriding, we can change super class method according to the requirements of sub class.

 

58. What are the rules to be followed while overriding a method?

There are 5 main rules you should kept in mind while overriding a method. They are,

  1. Name of the method must be same as that of super class method.
  2. Return type of overridden method must be compatible with the method being overridden, i.e. if a method has primitive type as its return type then it must be overridden with primitive type only and if a method has derived type as its return type then it must be overridden with same type or its sub class types.
  3. You must not reduce the visibility of a method while overriding.
  4. You must not change parameter list of a method while overriding.
  5. You cannot increase the scope of exceptions while overriding a method with throws clause.

When a class extends its super class, all or some members of super class are inherited to sub class. When an inherited super class method is modified in the sub class, then we call it as method is overridden. Through method overriding, we can modify super class method according to requirements of sub class.

Method Overriding in java is most useful features of java. Through inheritance we can reuse already existed code and through method overriding we can modify that reused code according to our requirements.

Name of the overridden method must be same as in the super class. You can’t change name of the method in subclass.

  • Return Type Of Overridden Method :

The return type of the overridden method must be compatible with super class method. If super class method has primitive data type as its return type, then overridden method must have same return type in sub class also. If super class method has derived or user defined data type as its return type, then return type of sub class method must be of same type or its sub class. 

class SuperClass{

    void firstMethodOfSuperClass()  {

        System.out.println("Super Class");

    }

    double secondMethodOfSuperClass()  {

        return 0.0;

    }

    Object thirdMethodOfSuperClass()  {

        return new Object();

    }

}

 

class SubClass extends SuperClass {

    int firstMethodOfSuperClass() {

        //Compile time error, return type must be void not int

    }

     void secondMethodOfSuperClass() {

        //Complie time error, return type must be double not void

    }

     String thirdMethodOfSuperClass() {

        //No Compile time error,

        //return type is compatible with super class method, because

        //String is sub class of Object class

        return new String();

    }

}

 

  • Visibility Of Overridden method:

You can keep same visibility or increase the visibility of overridden method but you can’t reduce the visibility of overridden methods in the subclass. For example, default method can be overridden as default or protected or public method but not as private.

class SuperClass{

    protected void methodOfSuperClass(){

        System.out.println("Super Class");

    }

}

 class SubClass extends SuperClass{

    private void methodOfSuperClass(){

        //Compile time error, can't reduce visibility of overridden method

        //here, visibility must be protected or public but not private or default

    }

}


Note: Visibility goes on decreasing from public to protected to default to private members.

 

  • Arguments Of Overridden Methods :

For method to be properly overridden, you must not change arguments of method in subclass. If you change the number of arguments or types of arguments of overridden method in the subclass, then method will be overloaded not overridden.

 

class SuperClass{

    void methodOfSuperClass(){

        System.out.println(“Super Class");

    }

}

 class SubClass extends SuperClass{

    //This class will have two methodOfSuperClass() methods.

    //one is from super class which takes no argument

    //and one is below method which takes one argument

    void methodOfSuperClass(int i){

        System.out.println(i);

    }

}

 public class MethodOverloading{

    public static void main(String[] args){

        SuperClass obj  = new SuperClass();

        obj.methodOfSuperClass();         //Output : Super Class

        SubClass obj1 = new SubClass();

        obj1.methodOfSuperClass();          //Output : Super Class

        obj1.methodOfSuperClass(100);       // Output : 100

    }

}

 

59. Can we override static methods?

No, Static methods cannot be overridden. If we try to override them they will be hidden in the sub class.

 

60. What happens if we change the arguments of overriding method?

If we change the arguments of overriding method, then that method will be treated as overloaded not overridden.

 

61. Can we override protected method of super class as public method in the sub class?

Yes. You can increase the visibility of overriding methods but can’t reduce it.

 

62. Can we change the return type of overriding method from Number type to Integer type?

Yes. You can change because Integer is a sub class of Number type.

 

63. Can we override a super class method without throws clause as a method with throws clause in the sub class?

Yes, but only with unchecked type of exceptions.

 

64. Can we change an exception of a method with throws clause from SQLException to NumberFormatException while overriding it?

Yes. Overridden method may throw SQLException or its sub class exception or any unchecked type of exceptions.

 

65. Can we change an exception of a method with throws clause from unchecked to checked while overriding it?

No. We can’t change an exception of a method with throws clause from unchecked to checked.

 

66. How do you refer super class version of overridden method in the sub class?

Using super keyword, we can refer super class version of overridden method in the sub class.

 

67. Can we override private methods?

No private methods cannot be overridden. They are not at all inherited to sub class.

 

68. Can we remove throws clause of a method while overriding it?

Yes. You can remove throws clause of a method while overriding it.

 

69. Is it possible to override non-static methods as static?

No. You can’t override non-static methods as static.

 

70. Can we change an exception of a method with throws clause from checked to unchecked while overriding it?

Yes. We can change an exception from checked to unchecked but reverse is not possible.

 

71. Can we change the number of exceptions thrown by a method with throws clause while overriding it?

Yes, we can change. But, exceptions must be compatible with throws clause in the super class method.

 

72. What is the difference between method overloading and method overriding in java?

 

Method Overloading

Method Overriding

Definition

When a class has more than one method with same name but with different arguments, then we call it as method overloading.

When a super class method is modified in the sub class, then we call this as method overriding.

Method Signature

Overloaded methods must have different method signatures.  That means they should differ at least in any one of these three things – Number of arguments, Types of arguments and order of arguments. But, they must have same name.

Overridden methods must have same method signature. I.e. you must not change the method name, types of arguments, number of arguments and order of arguments while overriding a super class method.

Return Types

Overloaded methods can have same or different return types.

The return type of the overridden method must be compatible with that of super class method. That means if super class method has primitive type as its return type, then it must be overridden with same return type. If super class method has derived type as its return type then it must be overridden with same type or its sub class type.

Visibility(private, public, protected and default)

Overloaded methods can have same visibility or different visibility.

While overriding a super class method either you can keep the same visibility or you can increase the visibility. But you can’t reduce it.

Static Context

Overloaded methods can be static or not static. It does not affect the method overloading.

You can’t override a static method.

Binding

Binding between method call and method definition happens at compile time (Static Binding).

Binding between method call and method definition happens at run time (Dynamic Binding).

Polymorphism

It shows static polymorphism.

It shows dynamic polymorphism.

Private methods

Private methods can be overloaded.

Private methods can’t be overridden.

Final Methods

Final methods can be overloaded.

Final methods can’t be overridden.

Class Requirement

For method overloading, only one class is required. I.e. Method overloading happens within a class.

For method overriding, two classes are required – super class and sub class. That means method overriding happens between two classes.

 

73. Does an interface extend Object class in java?

No, Interfaces don’t inherit from Object class. They don’t have default parent like classes in java.  But, following two cases were there.

Case 1:

An interface does not extends Object class, but we can call methods of Object class on interface variable like below.

interface A{}

class test{

static public void main(String kl[]){

A a=null;

a.equals(null);

a.hashCode();

a.toString();

 }

}

 

Case 2:

An interface does not extend Object class, but methods of Object class are visible in interface.

interface A{

@Override

    public boolean equals(Object obj);

@Override

    public int hashCode();

@Override

    public String toString();

}

 NOTE: -

This is because, for every public method in Object class, there is an implicit abstract and public method declared in every interface which does not have direct super interfaces.

“If an interface has no direct super interfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.”

 

74. Abstract class must have only abstract methods?

No, Abstract class can also have concrete methods.

 

75. Is it compulsory for a class which is declared as abstract to have at least one abstract method?

Not necessarily. Abstract class may or may not have abstract methods.

 

76. Can we use “abstract” keyword with constructor, Instance Initialization Block and Static Initialization Block?

No. Constructor, Static Initialization Block, Instance Initialization Block and variables cannot be abstract.

 

77. Why final and abstract cannot be used at a time?

Because, final and abstract are totally opposite in nature. A final class or method cannot be modified further whereas abstract class or method must be modified further. “final” keyword is used to denote that a class or method does not need further improvements. “abstract” keyword is used to denote that a class or method needs further improvements.

 

78. Can we instantiate a class which does not have even a single abstract methods but declared as abstract?

No, we can’t instantiate a class once it is declared as abstract even though it does not have abstract methods.

 

79. Can we declare abstract methods as private?

No. Abstract methods cannot be private. If abstract methods are allowed to be private, then they will not be inherited to sub class and will not get enhanced.

 

80. We can’t instantiate an abstract class. Then why constructors are allowed in abstract class?

It is because, we can’t create objects to abstract classes but we can create objects to their sub classes. From sub class constructor, there will be an implicit call to super class constructor. That’s why abstract classes should have constructors. Even if you don’t write constructor for your abstract class, compiler will keep default constructor.

 

81. Can we declare abstract methods as static?

No, abstract methods cannot be static.

 

82. Can a class contain an abstract class as a member?

Yes, a class can have abstract class as its member.

 

83. Abstract classes can be nested?

Yes, Abstract classes can be nested i.e. an abstract class can have another abstract class as its member.

 

84. Can we declare abstract methods as synchronized?

No, abstract methods cannot be declared as synchronized. But methods which override abstract methods can be declared as synchronized.

 

85. Can we declare local inner class as abstract?

Yes. Local inner class can be abstract.

 

86. Can abstract method declaration include throws clause?

Yes. Abstract methods can be declared with throws clause.

 

87. Can interfaces have constructors, SIB (Static Initializer Block) and IIB (Instance Initializer Block)?

No. Interfaces can’t have constructors, SIB and IIB. They show 100% abstractness.

 

88. Can we re-assign a value to a field of interfaces?

No. The fields of interfaces are static and final by default. They are just like constants. You can’t change their value once they got.

 

89. Can we declare an Interface with “abstract” keyword?

Yes, we can declare an interface with “abstract” keyword. But, there is no need to write like that. All interfaces in java are abstract by default.

 

90. For every Interface in java, .class file will be generated after compilation. True or false?

True. .class file will be generated for every interface after compilation.

 

91. Can we override an interface method with visibility other than public?

No. While overriding any interface methods, we should use public only. Because, all interface methods are public by default and you should not reduce the visibility while overriding them.

 

92. Can interfaces become local members of the methods?

No. You can’t define interfaces as local members of methods like local inner classes. They can be a part of top level class or interface.

 

93. Can an interface extend a class?

No, a class cannot become super interface to any interface. Super interface must be an interface. That means, interfaces don’t extend classes but can extend other interfaces.

 

94. Like classes, does interfaces also extend Object class by default?

No. Interfaces don’t extend Object class.

 

95. Can interfaces have static methods?

No. Interfaces can’t have static methods.

 

96. Can an interface have a class or another interface as its members?

Yes. Interfaces can have classes or interfaces as their members.

 

97. What are marker interfaces? What is the use of marker interfaces?

Marker interfaces in java are interfaces with no members declared in them. They are just an empty interfaces used to mark or identify a special operation. For example, Cloneable interface is used to mark cloning operation and Serializable interface is used to mark serialization and deserialization of an object. Marker interfaces give instructions to JVM that classes implementing them will have special behavior and must be handled with care.

Marker interfaces don’t provide any functionality. In earlier versions of Java (Before Java 5), marker interfaces are used to provide metadata to the readers. With the introduction of annotations from Java 5, annotations are used more instead of marker interfaces to provide metadata. But, still many use marker interfaces to mark the special behavior of a class.

Java’s built-in Marker Interfaces:

These are some built-in marker interfaces in java which are used to mark some special behavior of a class.

1) java.lang.Cloneable Interface

2) java.io.Serializable Interface

3) java.util.EventListener

4) java.rmi.Remote

 

98. How many types of nested classes are there in java?

Java supports 2 types of nested classes. They are,

  1. Static Nested Classes
  2. Non-static Nested Classes OR Inner Classes

Non-static nested classes can be of 3 type,

  1. Member Inner Classes
  2. Local Inner Classes
  3. Anonymous Inner Classes

 

99. Can we access non-static members of outer class inside a static nested class?

No, we can’t access non-static members of outer class inside a static nested class. We can access only static members of outer class inside a static nested class.

 

100. What are member inner classes in java?

Member inner classes are the classes which are declared as non-static members of another class. Member inner classes can be accessed only by instantiating the outer class.

 

For More Java Interview Questions: Part-3  Click here.