194) Explain what is encapsulation?
Encapsulation is the process of wrapping of code and behaviour in a single unit called class and preventing from misuse is called encapsulation. Encapsulation exposes only part of object which are safe to exposed and remaining part of object is kept secured.
Encapsulation is supported through access control in java. There are four types of access control specifiers(public,private, protected, default) in java which supports encapsulation.
For example tv manufacturers exposes only buttons not all the thousands of electronic components which it is made up of.
195) What is inheritance ?
Inheritance is one of the important feature of object oriented language. Inheriting is the process of acquiring features of others. For example a child acquires the features of their parents.
In java inheritance is the process of inheriting member of existing classes by extending their functionality.
The original class is called base class, parent class or super class. The new class derived from parent is called child class, sub class, and derived class.
We use extends keyword in java to extend a class in java. All java classes extend java.lang.Object since object class is the super class for all classes in java.
When we create a new class by using inheritance ‘is-a’ relationship is formed.
196) Explain importance of inheritance in java?
Reusability :The major advantage of inheritance is code reuse. We can avoid duplicating code by using inheritance. We can place all common state and behaviour in that class , by extending that class we can
Extendability : We can add new functionality to our application without touching the existing code.
For example if we take Ms word we came across number of versions of msword such as word 2003,2007. Everytime they won’t write new code they reuse the existing code and some more features.
197) What is polymorphism in java?
Polymorphism is combination of two greek words which mean many forms. In polymorphism actual type of object involved in method call determines which method to call rather type of reference variable.
59) What is covariant return ?
In java 1.4 and earlier one method can override super class method if both methods have same signature and return types.
From Java 1.5 , a method can override other method if argument types match exactly though return types are different.(Return type must be subtype of other method).
Example : Class A
return new A();
Example : Class B
return new B();
From java 1.5 return type for doSomeThing() in Class B is valid . We get compile time error in 1.4 and earlier.