31) Why String is immutable in Java ?String is a special type of immutable class. Immutable class is a class which once created, it’s contents can not be changed. Immutable objects are the objects whose state can't be changed once constructed.
32) Can you explain about information hiding in Java ?
Information hiding helps objects to hide critical information from other other objects accessing it. It effectively decouples the method being invoked from the internal workings of a function. By doing so, object can change the hidden portions of the function without changing the calling code. Encapsulation is a common technique programmers use to implement information hiding.
33) Can you explain about encapsulation in Java ?
Encapsulation helps java to bind code and data it manipulates, restrict outside interference and misuse of data. It also hides irrelevant details of an object.
34) Can you explain about the access modifier in Java ?
Access modifiers specifies the access levels of a variable or method. Java access modifiers are public, private, protected, default modifier (Default access modifier).
|Access Modifiers||Same Class||Same Package||Subclass||Other packages|
35) What is the difference between super() and this() in Java ?
If you want to access methods of the base class from derived class "super" keyword is used. To access methods of the same class "this" keyword is used.
36) Can you explain about the constructor in Java ?
- Java constructors are special methods that are called when an object is instantiated.
- When objects are instantiated, arguments passed to the constructor will initialized the variables in an object.
- Name of the constructor should be same as the name of the Class. It can't have any return type.
- A class can have multiple constructors. Calling a constructor from another constructor in the same class is called Constructor chaining.
- All classes have a default empty constructor.
37) Can constructor take parameters ?
Yes. Constructor can take arguments.
38) Can you explain about the default constructor in Java ?
When a constructor is not specified explicitly, java compiler automatically creates a "Default Constructor". When we creates and object instance, default constructor initialize variables with it's default values.
39) What are the common reasons to define a default constructor ?
- To construct an object with default values.
- To initialize an object that doesn't need parameters in that initialization process.
- To redefine the scope of the constructor. By making the default constructor private, Java prevents everyone other than the class from constructing an object.
40) Can you explain about native method in Java ?
- Native is non access modifier. It can be applied only to method.
- It indicates the Platform-Dependent implementation of method or code.