Java Questions and Answers
Java Questions and Answers
1) Explain OOPs concept ? And where have you used all 4 of them in your framework.
Answer: OOP : Object Oriented Programming : It is to design a program using classes and
objects.
Object : Object has state and behavior. State has some Identification. Behavior means some
action of that state.
Example : A dog is an object because it has states like color, name, breed, etc. as well as
behaviors like wagging the tail, barking, eating, etc.Object is actually derived something from
class.
Class : it is the blueprint on the basis of which the object is created. Object is inheriting the
properties of class. Object is created from class.
Example : JsonPath Jsp = new JsonPath();
Jsonpath() : Class
Jsp : Object
Here , all the method in JsonPath() class are come in Jsp Object
Where have you used all 4 of them in your framework.
Inheritance The mechanism in Java by which one class acquires the properties
(instance variables) and functionalities of another class is known as Inheritance.
Polymorphism Polymorphism allows us to perform a task in multiple ways.
4) Types of polymorphism?
Answer: Polymorphism allows us to perform a task in multiple ways. the word Polymorphism
‘Poly’ means ‘Many’ and ‘Morphos’ means ‘Shapes’.
Types of Polymorphism
Method Overloading
Method Overriding
5) Method overloading and how to implement it ?
Answer: In one class two method name are same name are known as “Method Overloading”.
Why need of Method overloading – when we have two different arguments.
When we want different output from same method (Input).
For eg. public class Soap
{
Public static void display (){}
Public static void display (int a){} // pass argument
}
Need for overloading: Function is same but depending on argument your output result is
change. This is used when different result from argument.
6) Method overriding and how to implement it ?
Answer: Method Overriding
When we want to enhance or change the functionality of inherited object to achieve
desired results.
To implement this method definition should be same.
Using overriding we can only enhance the properties of class. We cannot reduce
properties.
7) What points should be taken care of while overriding a method .
Answer:
To implement this method definition should be same.
Method definition= method name, argument, return type.