Top 150+ Java Interview Questions and Answers

    58 Votes

58. What is an abstract class?

  • An Abstract class is a conceptual class.
  • An Abstract class cannot be instantiated – objects cannot be created.
  • Abstract classes act as a base for a group of classes, which are closely related in a package.
  • If a class is declared as “final”, we can't extend that class. In some cases, properties of a class should be extended and used in sub-classes. Such classes are called Abstract Classes.

 

59. What are the properties of abstract class?
  • A class with one or more abstract methods is automatically abstract and it cannot be instantiated.
  • A class declared abstract, even with no abstract methods cannot be instantiated.
  • A subclass of an abstract class can be instantiated if it overrides all abstract methods by implementing them.
  • A subclass that does not implement all of the superclass abstract methods is it'self abstract; and it cannot be instantiated.
  • We cannot declare abstract constructor or abstract static methods.

 

60. What is object class in java?

Every class that we create extends the class Object by default. It means that the Object class is at the top of the hierarchy. Due to this default inheritance of object class, we can pass an object of any class as an argument to any method.

61. What are the methods of object class?

The methods in this class are:

  • equals(Object ref) - returns if both objects are equal
  • finalize( ) - method called when an object’s memory is destroyed
  • getClass( ) - returns class to which the object belongs
  • hashCode( ) - returns the hashcode of the class
  • notify( ) - method to give message to a synchronized methods
  • notifyAll( ) - method to give message to all synchronized methods
  • toString() - return the string equivalent of the object name
  • wait() - suspends a thread for a while
  • wait(...) - suspends a thread for specified time in seconds

62. What does java.util package provides?

The java.util package provides various utility classes and interfaces that support

  • Date & calendar operations
  • String manipulations
  • Collections Operations

63. How do we convert object to string?

For a reference type object, converting from object to string is done by calling "toString()" on the reference. All reference types inherit this method from java.lang.Object and override this method to produce a string that represents the data in a form suitable for printing.

In case of Java's primitive types, to make a conversion to a string, the compiler must wrap the primitive data type in it's wrapper class, and call the "toString()" method of the wrapper class.

String arg1 = new Integer(1 + 2).toString();

For user defined class, the standard Object.toString() produce something like "ClassName@123456" (where the number is the hashcode representation of the object). To have something meaningful, the classes have to provide a method with the signature public String toString().

public class Car() {
String toString()
{
return brand + ” : ” + color + ” : ”+wheels+” : “+ speed;
}
}

64. How can we convert string to object?

Many data type wrapper classes provide the valueOf(String Variable) method which converts the given string into a numeric value. The syntax is straightforward. It requires using the static Integer.valueOf(String s) and intValue() methods from the java.lang.Integer class.

To convert the String "22" into the int 22 you would write

int i = Integer.valueOf("22").intValue(); 
Doubles, floats and longs are converted similarly. To convert a String like "22" into the long value 22 you would write
long l = Long.valueOf("22").longValue();

To convert "22.5" into a float or a double you would write:

double x = Double.valueOf("22.5").doubleValue(); float y = Float.valueOf("22.5").floatValue(); 

If the passed value is non-numeric like “Four", it will throw a NumberFormatException.

65. What is Regular expression?

Regular expressions or Java Regex is an API to search or manipulate string based on a certain pattern. It is supported by the java.util.regex API, which provides one interface and three classes.

Page 6 of 14

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

Java OOPS Interview Questions and Answers
Top Advanced Java Interview Questions and Answers
Java Collections Interview Questions and Answers
Java Thread Interview Questions and Answers