Tuesday, October 1, 2019

Java Interview Question







Image result for java logo




What is meant by Method Overriding?
Ans: Method overriding happens if the sub class method satisfies the below conditions with the Super class method:
  • Method name should be same
  • Argument should be same
  • Return type also should be same
The key benefit of overriding is that the Sub class can provide some specific information about that sub class type than the super class.

What is meant by Overloading?
Ans: Method overloading happens for different classes or within the same class.
For method overloading, subclass method should satisfy the below conditions with the Super class method (or) methods in the same class itself:
  • Same method name
  • Different argument type
  • May have different return types
What is meant by Interface?
Ans: Multiple inheritance cannot be achieved in java. To overcome this problem Interface concept is introduced.
An interface is a template which has only method declarations and not the method implementation.
Example:
1
Public abstract interface IManupulation{ //Interface declaration
2
Public abstract void add();//method declaration

3
public abstract void subtract();
4
}
  • All the methods in the interface are internally public abstract void.
  • All the variables in the interface are internally public static final that is constants.
  • Classes can implement the interface and not extends.
  • The class which implements the interface should provide an implementation for all the methods declared in the interface.
1
public class Manupulation implements IManupulation{ //Manupulation class uses the interface
2
Public void add(){

3
……………
4
}

5
Public void subtract(){
6
…………….

7
}
8
}
What is meant by Abstract class?
Ans: We can create the Abstract class by using “Abstract” keyword before the class name. An abstract class can have both “Abstract” methods and “Non-abstract” methods that are a concrete class.
Abstract method:
The method which has only the declaration and not the implementation is called the abstract method and it has the keyword called “abstract”. Declarations are the ends with a semicolon.
Example:
1
public abstract class Manupulation{
2
public abstract void add();//Abstract method declaration

3
Public void subtract(){
4
}

5
}
  • An abstract class may have a Non- abstract method also.
  • The concrete Subclass which extends the Abstract class should provide the implementation for abstract methods.
Difference between Array and Array List.
Ans: The Difference between Array and Array List can be understood from the below table:
                        Array                                      
   Array List    
Size should be given at the time of array declaration.

String[] name = new String[2]
Size may not be required. It changes the size dynamically.

ArrayList name = new ArrayList
To put an object into array we need to specify the index.

name[1] = “book”
No index required.

name.add(“book”)



Difference between String, String Builder, and String Buffer.
Ans: String: String variables are stored in “constant string pool”. Once the string reference changes the old value that exists in the “constant string pool”, it cannot be erased.

String Buffer:
  • Here string values are stored in a stack. If the values are changed then the new value replaces the older value.
  • The string buffer is synchronized which is thread-safe.
  • Performance is slower than the String Builder.

String Builder:
This is same as String Buffer except for the String Builder which is not threaded safety that is not synchronized. So obviously performance is fast.


No comments:

Post a Comment

Which Python course is best for beginners?

Level Up Your Python Prowess: Newbie Ninjas: Don't fret, little grasshoppers! Courses like "Learn Python 3" on Codecade...