Classes and Objects in Java

Classes and Objects in Java

1. Which two method you need to implement for key Object in HashMap ?
A:In order to use any object as Key in HashMap, it must implements equals and hashcode method in Java. Read How HashMap works in Java  for detailed explanation on how equals and hashcode method is used to put and get object from HashMap.

2. What is immutable object? Can you write immutable object?Immutable classes are Java
A:classes whose objects can not be modified once created. Any modification in Immutable object result in new object. For example is String is immutable in Java. Mostly Immutable are also final in Java, in order to prevent sub class from overriding methods in Java which can compromise Immutability. You can achieve same functionality by making member as non final but private and not modifying them except in constructor.

3.What is object?

A:An object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation. An object encapsulates state and behavior.

4.What is hash code?

A:hash code is a unique identification number allotted to the objects by the JVM.This hash code nu,ber is also called reference number which is sreated based on the location of the object in memory,and is unique for all objects,except for string objects.

5.How can you find the hash code of an object?

A:The hashcode() method of 'object'class in java.lang package is useful to find the hash code of an object.

6.What is the access specifiers?how many types are available in java?

A:An access specifiers is a key word that specifies how to access the members of a class or a class itself.We can access specifiers before a class and its members.THere are 4 access specifiers Available in java.

7.main difference between private and public?

 A:  'private' members of a class are not accessible any where outside the class.They are accessible only with in the class by the methods of that class.
    'public' members of a class are accessible every where outside the class.So anyy other program can read them and use them.

8.Why we are using 'default' method in java?

A:If no access specifier is written by the programmer,then the java compiler uses a 'default'access specifier.'default'members are accesssible outside the class,but with in the same directory.

9.What is protected?

A:'protected' members of a class are accessible outside the class,but generally ,within the same directory.

10.can you declare a class as 'private' ?

A:No,if we declare a class as private ,then it is not available to java compiler and hence a compile time error occurs.But ,inner classes can be declared as private.

11.What is a constructor?

A:a constructor is a similar to a method that is used to initialize the instance variables.The sole purpose of a constructor is to initialise the instance variables.a constructor has the following characters.
i.The constructor name and class name should be same.And the constructor's name should end with a pair of simple braces.

12.What is default constructor?

A:No argument constructor of a class is known as default constructor. When we don’t define any constructor for the class, java compiler automatically creates the default no-args constructor for the class. If there are other constructors defined, then compiler won’t create default constructor for us.

13.How can you call a constructor from another constructor ?

A:By using this() reference.
14 .How can you call the constructor of super class ?

A :By using super() syntax.
15 .What's the difference between normal methods and constructors?

A :Constructors must have the same name of the class and can not have a return type. They are called only once,  while regular methods can be called whenever required. We cannot explicitly call a constructor.

16.What is constructor chaining ?
A:When a constructor of a class is executed it will automatically call the default constructor of the super class (if no explicit call to any of the super class constructor) till the root of the hierarchy.


Post a Comment