1. What are OOPS concepts?
Answer: Object Oriented Programming (OOP) is a programming concept that works on the principle that objects are the most important part of your program. It allows users create the objects that they want and then create methods to handle those objects. Manipulating these objects to get results is the goal of Object Oriented Programming.
Core OOPS concepts are: Class, Object, Inheritance, and Polymorphism
Abstraction, Encapsulation, Association, Aggregation, Composition
Advantages of OOPS:
OOP offers easy to understand and a clear modular structure for programs.
Objects created for Object-Oriented Programs can be reused in other programs. Thus it saves significant development cost.
Large programs are difficult to write, but if the development and designing team follow OOPS concept then they can better design with minimum flaws.
It also enhances program modularity because every object exists independently.
2. Provide real time examples for each and every OOPS concepts?
Answer: Core OOPS concepts are:
1. Object: Any entity that has state and behaviour is known as an object. For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical. An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other's data or code.
Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviours like wagging the tail, barking, eating, etc.
2. Class: Collection of objects is called class. It is a logical entity.
A class can also be defined as a blueprint from which you can create an individual object. Class doesn't consume any space.
3. Inheritance: When one object acquires all the properties and behaviours of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
4. Polymorphism: If one task is performed in different ways, it is known as polymorphism. For example: to convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.
In Java, we use method overloading and method overriding to achieve polymorphism.
Another example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.
5. Abstraction: Hiding internal details and showing functionality is known as abstraction. For example phone call, we don't know the internal processing. In Java, we use abstract class and interface to achieve abstraction.
6. Encapsulation: Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.
7. Association: Association represents the relationship between the objects. Here, one object can be associated with one object or many objects. There can be four types of association between the objects:
One to One
One to Many
Many to One, and
Many to Many
8. Aggregation: Aggregation is a way to achieve Association. Aggregation represents the relationship where one object contains other objects as a part of its state. It represents the weak relationship between objects. It is also termed as a has-a relationship in Java. Like, inheritance represents the is-a relationship. It is another way to reuse objects.
9. Composition: The composition is also a way to achieve Association. The composition represents the relationship where one object contains other objects as a part of its state. There is a strong relationship between the containing object and the dependent object. It is the state where containing objects do not have an independent existence. If you delete the parent object, all the child objects will be deleted automatically.
3. What is inheritance? Types of inheritance? Does multiple inheritance allowed in java. If not, why?
Answer: Inheritance Concept: Please refer below link
https://www.automationtestinginsider.com/2019/07/inheritance-in-java.html
Multiple inheritance is not supported by Java using classes, handling the complexity that causes due to multiple inheritance is very complex. It creates problem during various operations like casting, constructor chaining etc and the above all reason is that there are very few scenarios on which we actually need multiple inheritance, so better to omit it for keeping the things simple and straightforward.
How are above problems handled for Default Methods and Interfaces?
Java 8 supports default methods where interfaces can provide default implementation of methods. And a class can implement two or more interfaces. In case both the implemented interfaces contain default methods with same method signature, the implementing class should explicitly specify which default method is to be used or it should override the default method.
4. Does an interface implements or extends another interface?
Answer: Unlike classes, interfaces are always completely abstract. ... An interface can extend any number of interfaces but one interface cannot implement another interface, because if any interface is implemented then its methods must be defined and interface never has the definition of any method.
5. Can multiple inheritances support in Interface?
Answer: Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance.
6. Examples of Abstract and Interface used in selenium project?
Answer: WebDriver driver = new ChromeDriver();
In above line “WebDriver” is an interface which lists all the methods required to be present for Browser Interactions; irrespective of Browser type.
No how all the Browser’s Driver would namely, firefox, IE, Safari etc could ensure that they have all the required methods for Browser Interaction.
This is achieved by OOPs abstraction concept.(Interfaces and Abstract classes in Java is a tool to implement Abstraction).
Lets assume we do not have Interfaces and Abstract classes implementation for Web Driver.
In that case, for a multi-Browser tests, code would like as below:
//Sample Code
Now let’s just say ChromeDriver team got a new method which they added in ChromeDriver class and user of selenium test thought of using that method.
Since, FirefoxDriver team did not add it, above test will fail for mozilla browser.
Only way to ensure that all the drivers (ChromeDriver, FireFox Driver) have same set of method and properties is by pointing them to a single source. A source which contains all the required methods for Browser Interaction.
This in realty is achieved by extending ChromeDriver, FirefoxDriver classes etc to “WebDriver” interface which contains signature of all the methods required to be present in a driver. So when there is a need for any new Method, it will be added in “WebDriver” interface and all the extended classes will have to implement it.
This is also a good example of Polymorphism, as driver alone can take multiple forms, it can be used for ChromeDirver Class as well as Fire fox class.
WebDriver driver = new ChromeDriver();
7. Can we extend multiple interfaces?
Answer: interface can extend more than one parent interface. The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.
8. What are abstract classes and methods in Java?
Answer: Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.
9. What is an Abstract Class? Write an example code?
Answer: A class that is declared using “abstract” keyword is known as abstract class. It can have abstract methods (methods without body) as well as concrete methods (regular methods with body). A normal class (non-abstract class) cannot have abstract methods.
An abstract class cannot be instantiated, which means you are not allowed to create an object of it.Because these classes are incomplete, they have abstract methods that have no body so if java allows you to create object of this class then if someone calls the abstract method using that object then What would happen?There would be no actual implementation of the method to invoke.
Why do we need abstract class?
Below example given:
//abstract parent class
Output:
10. Diff between abstract and interface.
Answer: 1. Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
2. Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
3. Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc.
4. Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
5. An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
6. A Java class can implement multiple interfaces but it can extend only one abstract class.
7. Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main () exists.
8. In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
11. Can we use private and protect access modifier inside a Interface?
Answer: Java interfaces are meant to specify fields and methods that are publicly available in classes that implement the interfaces. Therefore you cannot use the private and protected access modifiers in interfaces. Fields and methods in interfaces are implicitly declared public if you leave out an access modifier, so you cannot use the default access modifier either (no access modifier).
12. What is polymorphism? How we can achieve it?
Answer: Please refer below link
https://www.automationtestinginsider.com/2019/07/polymorphism-in-java.html
13. Explain run time polymorphism and compile time polymorphism with examples?
Answer: Compile time Polymorphism
1. In Compile time Polymorphism, call is resolved by the compiler.
2. It is also known as Static binding, early binding and overloading as well.
3. Overloading is compile time polymorphism where more than one methods share the same name with different parameters or signature and different return type.
4. It is achieved by function overloading and operator overloading.
5. It provides fast execution because known early at compile time.
6. Compile time polymorphism is less flexible as all things execute at compile time.
Run time Polymorphism
1. In Run time Polymorphism, call is not resolved by the compiler.
2. It is also known as Dynamic binding, late binding and overriding as well.
3. Overriding is run time polymorphism having same method with same parameters or signature, but associated in a class & its subclass.
4. It is achieved by virtual functions and pointers.
5. It provides slow execution as compare to early binding because it is known at runtime.
6. Run time polymorphism is more flexible as all things execute at run time.
14. Difference between method overloading and method over riding?
Answer:
1) Method overloading is used to increase the readability of the program.
Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
2) Method overloading is performed within class. Method overriding occurs in two classes that have IS-A (inheritance) relationship.
3) In case of method overloading, parameter must be different. In case of method overriding, parameter must be same.
4) Method overloading is the example of compile time polymorphism.
Method overriding is the example of run time polymorphism.
5) In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.
Return type must be same or covariant in method overriding.
15. Can we achieve method overloading when two methods have only difference in return type.
Answer: In java, method overloading is not possible by changing the return type of the method only because of ambiguity. Let's see how ambiguity may occur:
16. Method overloading and overriding examples in Selenium project?
Answer: We use implicit wait in Selenium. Implicit wait is an example of overloading. In Implicit wait we use different time stamps such as SECONDS, MINUTES, HOURS etc.,
A class having multiple methods with same name but different parameters is called Method Overloading.
We use a method which was already implemented in another class by changing its parameters. To understand this you need to understand Overriding in Java.
Declaring a method in child class which is already present in the parent class is called Method Overriding. Examples are get and navigate methods of different drivers in Selenium.
17. What is encapsulation?
Answer: Please refer below link:
https://www.automationtestinginsider.com/2019/07/encapsulation-in-java.html
18. What is IS-A and HAS-A relation in java with examples?
Answer: Is-A Relationship in Java:
In Java, an Is-A relationship depends on inheritance. Further inheritance is of two types, class inheritance and interface inheritance. It is used for code reusability in Java.When there is an extends or implement keyword in the class declaration in Java, then the specific class is said to be following the Is-A relationship.
Has-A Relationship in Java:
In Java, a Has-A relationship is also known as composition. It is also used for code reusability in Java. In Java, a Has-A relationship simply means that an instance of one class has a reference to an instance of another class or another instance of the same class. For example, a car has an engine, a dog has a tail and so on. In Java, there is no such keyword that implements a Has-A relationship. But we mostly use new keywords to implement a Has-A relationship in Java.
19. Explain runtime polymorphism and compile time with examples?
Answer: Please refer below link
https://www.automationtestinginsider.com/2019/07/polymorphism-in-java.html
20. Can we overload main method?
Answer: Yes, main method can be overloaded. Overloaded main method has to be called from inside the "public static void main(String args[])" as this is the entry point when the class is launched by the JVM. Also overloaded main method can have any qualifier as a normal method have.
21. Can final methods be overloaded?
Answer: Yes. It is possible to have overloaded final methods, and it is quite similar to having overloaded non-final methods in Java. However, overriding final methods is not allowed.
Output:
22. Can static methods be overloaded?
Answer: Static methods cannot be overridden because they are not dispatched on the object instance at runtime. The compiler decides which method gets called. Static methods can be overloaded (meaning that you can have the same method name for several methods as long as they have different parameter types).
23. Can static methods be overridden?
Answer: Static methods cannot be overridden because they are not dispatched on the object instance at runtime. The compiler decides which method gets called. Static methods can be overloaded (meaning that you can have the same method name for several methods as long as they have different parameter types).
24. ClassA { } ClassB extends ClassA { } ClassA a= new ClassA(); ClassB b=new ClassB(); ClassA c= new ClassB(); ClassB d=new ClassA(); Which is valid and invalid?
Answer: We have Parent class as ClassA and child class as ClassB. Let's have two methods methodOfClassA() and methodOfClassB() in ClassA and ClassB respectively.
Below are the possible scenarios:
25. Suppose i have 100 methods in my interface i want to implement only 2 methods in my class.. How to do?
Answer: You can do this by creating an abstract class that implements the interface.
26. Class A has 100 functions and class B wants to use 20 functions, how to use without inheritance?
Answer: Yes, create the object of Class A in Class B and call 20 required functions from Class B.
ClassA obj= new ClassA();
obj.methodOfClassA();
27. Create a custom class which contains an interface in it and custom class should be responsible for performing operations. Which type of interface will you use to create such a custom class?
28. What is a Constructor? Types of Constructor?
Answer: Please refer below link
https://www.automationtestinginsider.com/2019/07/constructors-in-java.html
29. When will you use this and super in a constructor?
Answer: super() as well as this() both are used to make constructor calls. super() is used to call Base class's constructor(i.e, Parent's class) while this() is used to call current class's constructor. super() is use to call Base class's(Parent class's) constructor.
30. What is the purpose of using ‘this’ keyword in Java?
Answer: The this keyword is used when you need to use class global variable in the constructors. this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
this keyword usage in java - https://www.automationtestinginsider.com/2019/07/this-keyword-in-java.html
31. Can a constructor be private?
Answer: Yes it can. A private constructor would exist to prevent the class from being instantiated, or because construction happens only internally, e.g. a Factory pattern.
32. Can you make the constructor of a class static?
Answer: The method declared as static requires no object creation .As we don't create object for the main method it is declared as static. Constructor is implicitly called to initialize an object, so there is no purpose in having a static constructor. Java does not permit to declare a constructor as static.
33. If you want to call a constructor from parent class, what will you do?
Answer: Using super keyword
1. The call to super() must be the first line of the derived class constructor.
2. If explicit call to parent constructor not made, the subclass' constructor will automatically invoke super(). ( ...
3. Can also use super to invoke a method from the parent class (from inside the derived class).
34. What is the purpose of using Constructors in Java?
Answer: The purpose of constructor is to initialize the object of a class while the purpose of a method is to perform a task by executing java code. Constructors cannot be abstract, final, static and synchronised while methods can be. Constructors do not have return types while methods do.
35. How Constructors are different from Methods in Java?
Answer: The purpose of constructor is to initialize the object of a class while the purpose of a method is to perform a task by executing java code. Constructors cannot be abstract, final, static and synchronised while methods can be. Constructors do not have return types while methods do.
36. Is overriding applicable for Constructors in Java?
Answer: Constructor overriding is never possible in Java. This is because, Constructor looks like a method but name should be as class name and no return value. Overriding means what we have declared in Super class, that exactly we have to declare in Sub class it is called Overriding.
37. What is static block in java
Answer: Java provides the user with a block known as static block, which is mainly used for the static initializations of a class. The block consists of a set of statements which are executed before the execution of the main method
38. What is instance block at class level and method level?
39. What are static and non-static methods and why will you create static and non-static methods?
Answer: The keyword static lets a method run without any instance of the class. A static method belongs to the class, so there’s no need to create an instance of the class to access it. To create a static method in Java, you prefix the static keyword before the name of the method. A non-static method does not have the keyword static before the name of the method. A non-static method belongs to an object of the class and you have to create an instance of the class to access it. Non-static methods can access any static method and any static variable without creating an instance of the class.
Static methods are the methods in Java that can be called without creating an object of class. It is belong to the class. We use static method when we no need to be invoked method using instance. ... A common use for static methods is to access static fields.
40. What are static variables and methods?
Answer: Please refer below link:
https://www.automationtestinginsider.com/2019/08/java-variable-questions-and-answers.html
41. Diff between static and non- static?
Answer: Find the below difference.
static variable and non-static method
One of the key difference between a static and a non-static method is that static method belongs to a class while non-static method belongs to the instance. This means you can call a static method without creating any instance of the class by just using the name of the class
static variable and non-static variables.
static variable and non-static variable
Static variables are shared among all instances of a class. Non static variables are specific to that instance of a class. Static variable is like a global variable and is available to all methods. Non static variable is like a local variable and they can be accessed through only instance of a class.
42. What is final and super keyword? Difference between them?
Answer: super and final keyword in Java.
super keyword:
Super is a keyword of Java which refers to the immediate parent of a class and is used inside the subclass method definition for calling a method defined in the superclass. A superclass having methods as private cannot be called. Only the methods which are public and protected can be called by the keyword super. It is also used by class constructors to invoke constructors of its parent class.
Usage:
Super variables refer to the variable of a variable of the parent class.
Super() invokes the constructor of immediate parent class.
Super refers to the method of the parent class.
Sample Program:
Output:
Instance refers an instance variable of the current class by default, but when you have to refer parent class instance variable, you have to use super keyword to distinguish between parent class (here employee) instance variable and current class (here, clerk) instance variable.
final keyword:
final is a keyword in Java that is used to restrict the user and can be used in many respects. Final can be used with:
Class
Methods
Variables
Class as final:
When a class is declared as final, it cannot be extended further. Here is an example what happens within a program.
Compilation Error will be shown because This is because classes declared as final cannot be inherited.
Method as final:
A method declared as final cannot be overridden; this means even when a child class can call the final method of parent class without any issues, but the overriding will not be possible. Here is a sample program showing what is not valid within a Java program when a method is declared as final.
Variable as final:
Once a variable is assigned with the keyword final, it always contains the same exact value. Again things may happen like this; if a final variable holds a reference to an object then the state of the object can be altered if programmers perform certain operations on those objects, but the variable will always refer to the same object. A final variable that is not initialized at the time of declaration is known as a blank final variable. If you are declaring a final variable in a constructor, then you must initialize the blank final variable within the constructor of the class. Otherwise, the program might show a compilation error.
43. Can we execute a class without a main method?
Answer: Yes You can compile and execute without main method By using static block. But after static block executed (printed) you will get an error saying no main method found. And Latest INFO --> YOU can’t Do this with JAVA 7 and above version.
44. If static is for memory management then why to use non static?
Answer: Non-static method can be overridden because of run time binding. In static method, less memory is use for execution because memory allocation happens only once, because the static keyword fixed a particular memory for that method in ram
45. What is the difference between static and instance variable in Java?
Answer: Please refer below link:
https://www.automationtestinginsider.com/2019/08/java-variable-questions-and-answers.html
46. Explain the difference between static and instance methods in Java?
Answer: Instance method are methods which require an object of its class to be created before it can be called. Static methods are the methods in Java that can be called without creating an object of class. ... Instance method is not with static keyword. Static method means which will exist as a single copy for a class.
47. Can final method be overloaded?
Answer: Yes. It is possible to have overloaded final methods, and it is quite similar to having overloaded non-final methods in Java. However, overriding final methods is not allowed.
48. Can final/Static methods be overridden?
Answer: Yes. It is possible to have overloaded final methods, and it is quite similar to having overloaded non-final methods in Java. However, overriding final methods is not allowed.
Answer: Object Oriented Programming (OOP) is a programming concept that works on the principle that objects are the most important part of your program. It allows users create the objects that they want and then create methods to handle those objects. Manipulating these objects to get results is the goal of Object Oriented Programming.
Core OOPS concepts are: Class, Object, Inheritance, and Polymorphism
Abstraction, Encapsulation, Association, Aggregation, Composition
Advantages of OOPS:
OOP offers easy to understand and a clear modular structure for programs.
Objects created for Object-Oriented Programs can be reused in other programs. Thus it saves significant development cost.
Large programs are difficult to write, but if the development and designing team follow OOPS concept then they can better design with minimum flaws.
It also enhances program modularity because every object exists independently.
2. Provide real time examples for each and every OOPS concepts?
Answer: Core OOPS concepts are:
1. Object: Any entity that has state and behaviour is known as an object. For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical. An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other's data or code.
Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviours like wagging the tail, barking, eating, etc.
2. Class: Collection of objects is called class. It is a logical entity.
A class can also be defined as a blueprint from which you can create an individual object. Class doesn't consume any space.
3. Inheritance: When one object acquires all the properties and behaviours of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
4. Polymorphism: If one task is performed in different ways, it is known as polymorphism. For example: to convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.
In Java, we use method overloading and method overriding to achieve polymorphism.
Another example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.
5. Abstraction: Hiding internal details and showing functionality is known as abstraction. For example phone call, we don't know the internal processing. In Java, we use abstract class and interface to achieve abstraction.
6. Encapsulation: Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.
7. Association: Association represents the relationship between the objects. Here, one object can be associated with one object or many objects. There can be four types of association between the objects:
One to One
One to Many
Many to One, and
Many to Many
8. Aggregation: Aggregation is a way to achieve Association. Aggregation represents the relationship where one object contains other objects as a part of its state. It represents the weak relationship between objects. It is also termed as a has-a relationship in Java. Like, inheritance represents the is-a relationship. It is another way to reuse objects.
9. Composition: The composition is also a way to achieve Association. The composition represents the relationship where one object contains other objects as a part of its state. There is a strong relationship between the containing object and the dependent object. It is the state where containing objects do not have an independent existence. If you delete the parent object, all the child objects will be deleted automatically.
3. What is inheritance? Types of inheritance? Does multiple inheritance allowed in java. If not, why?
Answer: Inheritance Concept: Please refer below link
https://www.automationtestinginsider.com/2019/07/inheritance-in-java.html
Multiple inheritance is not supported by Java using classes, handling the complexity that causes due to multiple inheritance is very complex. It creates problem during various operations like casting, constructor chaining etc and the above all reason is that there are very few scenarios on which we actually need multiple inheritance, so better to omit it for keeping the things simple and straightforward.
How are above problems handled for Default Methods and Interfaces?
Java 8 supports default methods where interfaces can provide default implementation of methods. And a class can implement two or more interfaces. In case both the implemented interfaces contain default methods with same method signature, the implementing class should explicitly specify which default method is to be used or it should override the default method.
4. Does an interface implements or extends another interface?
Answer: Unlike classes, interfaces are always completely abstract. ... An interface can extend any number of interfaces but one interface cannot implement another interface, because if any interface is implemented then its methods must be defined and interface never has the definition of any method.
5. Can multiple inheritances support in Interface?
Answer: Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance.
6. Examples of Abstract and Interface used in selenium project?
Answer: WebDriver driver = new ChromeDriver();
In above line “WebDriver” is an interface which lists all the methods required to be present for Browser Interactions; irrespective of Browser type.
No how all the Browser’s Driver would namely, firefox, IE, Safari etc could ensure that they have all the required methods for Browser Interaction.
This is achieved by OOPs abstraction concept.(Interfaces and Abstract classes in Java is a tool to implement Abstraction).
Lets assume we do not have Interfaces and Abstract classes implementation for Web Driver.
In that case, for a multi-Browser tests, code would like as below:
//Sample Code
if ( BrowserType=='Chrome'){
ChromeDriver driver = new ChromeDriver();
}elseif(BrowserType=='Firefox'){
FireFox driver = new FirefoxDriver();
}
Since, FirefoxDriver team did not add it, above test will fail for mozilla browser.
Only way to ensure that all the drivers (ChromeDriver, FireFox Driver) have same set of method and properties is by pointing them to a single source. A source which contains all the required methods for Browser Interaction.
This in realty is achieved by extending ChromeDriver, FirefoxDriver classes etc to “WebDriver” interface which contains signature of all the methods required to be present in a driver. So when there is a need for any new Method, it will be added in “WebDriver” interface and all the extended classes will have to implement it.
This is also a good example of Polymorphism, as driver alone can take multiple forms, it can be used for ChromeDirver Class as well as Fire fox class.
WebDriver driver = new ChromeDriver();
7. Can we extend multiple interfaces?
Answer: interface can extend more than one parent interface. The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.
8. What are abstract classes and methods in Java?
Answer: Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.
9. What is an Abstract Class? Write an example code?
Answer: A class that is declared using “abstract” keyword is known as abstract class. It can have abstract methods (methods without body) as well as concrete methods (regular methods with body). A normal class (non-abstract class) cannot have abstract methods.
An abstract class cannot be instantiated, which means you are not allowed to create an object of it.Because these classes are incomplete, they have abstract methods that have no body so if java allows you to create object of this class then if someone calls the abstract method using that object then What would happen?There would be no actual implementation of the method to invoke.
Why do we need abstract class?
Below example given:
//abstract parent class
//abstract parent class
abstract class Animal{
//abstract method
public abstract void sound();
}
//Dog class extends Animal class
public class Dog extends Animal{
public void sound(){
System.out.println("Woof");
}
public static void main(String args[]){
Animal obj = new Dog();
obj.sound();
}
}
Output:
Woof
10. Diff between abstract and interface.
Answer: 1. Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
2. Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
3. Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc.
4. Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
5. An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
6. A Java class can implement multiple interfaces but it can extend only one abstract class.
7. Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main () exists.
8. In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
11. Can we use private and protect access modifier inside a Interface?
Answer: Java interfaces are meant to specify fields and methods that are publicly available in classes that implement the interfaces. Therefore you cannot use the private and protected access modifiers in interfaces. Fields and methods in interfaces are implicitly declared public if you leave out an access modifier, so you cannot use the default access modifier either (no access modifier).
12. What is polymorphism? How we can achieve it?
Answer: Please refer below link
https://www.automationtestinginsider.com/2019/07/polymorphism-in-java.html
13. Explain run time polymorphism and compile time polymorphism with examples?
Answer: Compile time Polymorphism
1. In Compile time Polymorphism, call is resolved by the compiler.
2. It is also known as Static binding, early binding and overloading as well.
3. Overloading is compile time polymorphism where more than one methods share the same name with different parameters or signature and different return type.
4. It is achieved by function overloading and operator overloading.
5. It provides fast execution because known early at compile time.
6. Compile time polymorphism is less flexible as all things execute at compile time.
Run time Polymorphism
1. In Run time Polymorphism, call is not resolved by the compiler.
2. It is also known as Dynamic binding, late binding and overriding as well.
3. Overriding is run time polymorphism having same method with same parameters or signature, but associated in a class & its subclass.
4. It is achieved by virtual functions and pointers.
5. It provides slow execution as compare to early binding because it is known at runtime.
6. Run time polymorphism is more flexible as all things execute at run time.
14. Difference between method overloading and method over riding?
Answer:
1) Method overloading is used to increase the readability of the program.
Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
2) Method overloading is performed within class. Method overriding occurs in two classes that have IS-A (inheritance) relationship.
3) In case of method overloading, parameter must be different. In case of method overriding, parameter must be same.
4) Method overloading is the example of compile time polymorphism.
Method overriding is the example of run time polymorphism.
5) In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.
Return type must be same or covariant in method overriding.
15. Can we achieve method overloading when two methods have only difference in return type.
Answer: In java, method overloading is not possible by changing the return type of the method only because of ambiguity. Let's see how ambiguity may occur:
class Test{
static int add(int a,int b)
{return a+b;}
static double add
(int a,int b){return a+b;}
}
class TestOverloading3{
public static void main(String[] args){
System.out.println(Test.add(11,11));//ambiguity
}}
16. Method overloading and overriding examples in Selenium project?
Answer: We use implicit wait in Selenium. Implicit wait is an example of overloading. In Implicit wait we use different time stamps such as SECONDS, MINUTES, HOURS etc.,
A class having multiple methods with same name but different parameters is called Method Overloading.
We use a method which was already implemented in another class by changing its parameters. To understand this you need to understand Overriding in Java.
Declaring a method in child class which is already present in the parent class is called Method Overriding. Examples are get and navigate methods of different drivers in Selenium.
17. What is encapsulation?
Answer: Please refer below link:
https://www.automationtestinginsider.com/2019/07/encapsulation-in-java.html
18. What is IS-A and HAS-A relation in java with examples?
Answer: Is-A Relationship in Java:
In Java, an Is-A relationship depends on inheritance. Further inheritance is of two types, class inheritance and interface inheritance. It is used for code reusability in Java.When there is an extends or implement keyword in the class declaration in Java, then the specific class is said to be following the Is-A relationship.
Has-A Relationship in Java:
In Java, a Has-A relationship is also known as composition. It is also used for code reusability in Java. In Java, a Has-A relationship simply means that an instance of one class has a reference to an instance of another class or another instance of the same class. For example, a car has an engine, a dog has a tail and so on. In Java, there is no such keyword that implements a Has-A relationship. But we mostly use new keywords to implement a Has-A relationship in Java.
19. Explain runtime polymorphism and compile time with examples?
Answer: Please refer below link
https://www.automationtestinginsider.com/2019/07/polymorphism-in-java.html
20. Can we overload main method?
Answer: Yes, main method can be overloaded. Overloaded main method has to be called from inside the "public static void main(String args[])" as this is the entry point when the class is launched by the JVM. Also overloaded main method can have any qualifier as a normal method have.
21. Can final methods be overloaded?
Answer: Yes. It is possible to have overloaded final methods, and it is quite similar to having overloaded non-final methods in Java. However, overriding final methods is not allowed.
import java.util.Scanner;
public class final_method_overload {
final void calc(int a, int b){
int c=a+b;
System.out.println("Addition--"+c);
}
final void calc(double c, double d) // this is final method //overloading
{
System.out.println("Just printing numbers.."+c +"..."+d);
}
void calc(String name) // this method is not final..only overloading
{
Scanner sc=new Scanner(System.in);
name=sc.next();
System.out.println("my name is "+name);
}
public static void main(String[] args) {
final_method_overload fmo=new final_method_overload();
fmo.calc(23,34);
fmo.calc(3.9,6.6);
System.out.println("What is your name??");
fmo.calc("Robert");
}
Output:
Addition--57
Just Printing numbers..3.9...6.6
What is your name??
Robert
my name is Robert
22. Can static methods be overloaded?
Answer: Static methods cannot be overridden because they are not dispatched on the object instance at runtime. The compiler decides which method gets called. Static methods can be overloaded (meaning that you can have the same method name for several methods as long as they have different parameter types).
23. Can static methods be overridden?
Answer: Static methods cannot be overridden because they are not dispatched on the object instance at runtime. The compiler decides which method gets called. Static methods can be overloaded (meaning that you can have the same method name for several methods as long as they have different parameter types).
24. ClassA { } ClassB extends ClassA { } ClassA a= new ClassA(); ClassB b=new ClassB(); ClassA c= new ClassB(); ClassB d=new ClassA(); Which is valid and invalid?
Answer: We have Parent class as ClassA and child class as ClassB. Let's have two methods methodOfClassA() and methodOfClassB() in ClassA and ClassB respectively.
Below are the possible scenarios:
ClassA a= new ClassA();
//Valid - object of classA with reference to classA
a.methodOfClassA();//access successfully method of ClassA
a.methodOfClassB();//Not possible, Cannot access method of ClassB
ClassB b=new ClassB();
//Valid - object of classB with reference to classB
b.methodOfClassA(); //access successfully
b.methodOfClassB(); //access successfully
ClassA c= new ClassB();
//Valid - object of classB with reference to classA
c.methodOfClassA(); //access successfully method of ClassA
c.methodOfClassB(); //Not Possible, Cannot access method of ClassB
ClassB d=new ClassA();
//invalid - object of classA with reference to classB
//Child's reference cannot hold parent's object.
25. Suppose i have 100 methods in my interface i want to implement only 2 methods in my class.. How to do?
Answer: You can do this by creating an abstract class that implements the interface.
26. Class A has 100 functions and class B wants to use 20 functions, how to use without inheritance?
Answer: Yes, create the object of Class A in Class B and call 20 required functions from Class B.
ClassA obj= new ClassA();
obj.methodOfClassA();
27. Create a custom class which contains an interface in it and custom class should be responsible for performing operations. Which type of interface will you use to create such a custom class?
28. What is a Constructor? Types of Constructor?
Answer: Please refer below link
https://www.automationtestinginsider.com/2019/07/constructors-in-java.html
29. When will you use this and super in a constructor?
Answer: super() as well as this() both are used to make constructor calls. super() is used to call Base class's constructor(i.e, Parent's class) while this() is used to call current class's constructor. super() is use to call Base class's(Parent class's) constructor.
30. What is the purpose of using ‘this’ keyword in Java?
Answer: The this keyword is used when you need to use class global variable in the constructors. this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
this keyword usage in java - https://www.automationtestinginsider.com/2019/07/this-keyword-in-java.html
31. Can a constructor be private?
Answer: Yes it can. A private constructor would exist to prevent the class from being instantiated, or because construction happens only internally, e.g. a Factory pattern.
32. Can you make the constructor of a class static?
Answer: The method declared as static requires no object creation .As we don't create object for the main method it is declared as static. Constructor is implicitly called to initialize an object, so there is no purpose in having a static constructor. Java does not permit to declare a constructor as static.
33. If you want to call a constructor from parent class, what will you do?
Answer: Using super keyword
1. The call to super() must be the first line of the derived class constructor.
2. If explicit call to parent constructor not made, the subclass' constructor will automatically invoke super(). ( ...
3. Can also use super to invoke a method from the parent class (from inside the derived class).
34. What is the purpose of using Constructors in Java?
Answer: The purpose of constructor is to initialize the object of a class while the purpose of a method is to perform a task by executing java code. Constructors cannot be abstract, final, static and synchronised while methods can be. Constructors do not have return types while methods do.
35. How Constructors are different from Methods in Java?
Answer: The purpose of constructor is to initialize the object of a class while the purpose of a method is to perform a task by executing java code. Constructors cannot be abstract, final, static and synchronised while methods can be. Constructors do not have return types while methods do.
36. Is overriding applicable for Constructors in Java?
Answer: Constructor overriding is never possible in Java. This is because, Constructor looks like a method but name should be as class name and no return value. Overriding means what we have declared in Super class, that exactly we have to declare in Sub class it is called Overriding.
37. What is static block in java
Answer: Java provides the user with a block known as static block, which is mainly used for the static initializations of a class. The block consists of a set of statements which are executed before the execution of the main method
38. What is instance block at class level and method level?
39. What are static and non-static methods and why will you create static and non-static methods?
Answer: The keyword static lets a method run without any instance of the class. A static method belongs to the class, so there’s no need to create an instance of the class to access it. To create a static method in Java, you prefix the static keyword before the name of the method. A non-static method does not have the keyword static before the name of the method. A non-static method belongs to an object of the class and you have to create an instance of the class to access it. Non-static methods can access any static method and any static variable without creating an instance of the class.
Static methods are the methods in Java that can be called without creating an object of class. It is belong to the class. We use static method when we no need to be invoked method using instance. ... A common use for static methods is to access static fields.
40. What are static variables and methods?
Answer: Please refer below link:
https://www.automationtestinginsider.com/2019/08/java-variable-questions-and-answers.html
41. Diff between static and non- static?
Answer: Find the below difference.
static variable and non-static method
One of the key difference between a static and a non-static method is that static method belongs to a class while non-static method belongs to the instance. This means you can call a static method without creating any instance of the class by just using the name of the class
static variable and non-static variables.
static variable and non-static variable
Static variables are shared among all instances of a class. Non static variables are specific to that instance of a class. Static variable is like a global variable and is available to all methods. Non static variable is like a local variable and they can be accessed through only instance of a class.
42. What is final and super keyword? Difference between them?
Answer: super and final keyword in Java.
super keyword:
Super is a keyword of Java which refers to the immediate parent of a class and is used inside the subclass method definition for calling a method defined in the superclass. A superclass having methods as private cannot be called. Only the methods which are public and protected can be called by the keyword super. It is also used by class constructors to invoke constructors of its parent class.
Usage:
Super variables refer to the variable of a variable of the parent class.
Super() invokes the constructor of immediate parent class.
Super refers to the method of the parent class.
Sample Program:
class employee {
int wt = 8;
}
class clerk extends employee {
int wt = 10; //work time
void display() {
System.out.println(super.wt); //will print work time of clerk
}
public static void main(String args[]) {
clerk c = new clerk();
c.display();
}
}
Output:
8
Instance refers an instance variable of the current class by default, but when you have to refer parent class instance variable, you have to use super keyword to distinguish between parent class (here employee) instance variable and current class (here, clerk) instance variable.
final keyword:
final is a keyword in Java that is used to restrict the user and can be used in many respects. Final can be used with:
Class
Methods
Variables
Class as final:
When a class is declared as final, it cannot be extended further. Here is an example what happens within a program.
final class stud {
// Methods cannot be extended to its sub class
}
class books extends stud {
void show() {
System.out.println("Book-Class method");
}
public static void main(String args[]) {
books B1 = new books();
B1.show();
}
}
Compilation Error will be shown because This is because classes declared as final cannot be inherited.
Method as final:
A method declared as final cannot be overridden; this means even when a child class can call the final method of parent class without any issues, but the overriding will not be possible. Here is a sample program showing what is not valid within a Java program when a method is declared as final.
class stud {
final void show() {
System.out.println("Class - stud : method defined");
}
}
class books extends stud {
void show() {
System.out.println("Class - books : method defined");
}
public static void main(String args[]) {
books B2 = new books();
B2.show();
}
}
Variable as final:
Once a variable is assigned with the keyword final, it always contains the same exact value. Again things may happen like this; if a final variable holds a reference to an object then the state of the object can be altered if programmers perform certain operations on those objects, but the variable will always refer to the same object. A final variable that is not initialized at the time of declaration is known as a blank final variable. If you are declaring a final variable in a constructor, then you must initialize the blank final variable within the constructor of the class. Otherwise, the program might show a compilation error.
import java.util.*;
import java.lang.*;
import java.io.*;
class stud {
final int val;
stud() {
val = 60;
}
void method() {
System.out.println(val);
}
public static void main(String args[]) {
stud S1 = new stud();
S1.method();
}
}
43. Can we execute a class without a main method?
Answer: Yes You can compile and execute without main method By using static block. But after static block executed (printed) you will get an error saying no main method found. And Latest INFO --> YOU can’t Do this with JAVA 7 and above version.
44. If static is for memory management then why to use non static?
Answer: Non-static method can be overridden because of run time binding. In static method, less memory is use for execution because memory allocation happens only once, because the static keyword fixed a particular memory for that method in ram
45. What is the difference between static and instance variable in Java?
Answer: Please refer below link:
https://www.automationtestinginsider.com/2019/08/java-variable-questions-and-answers.html
46. Explain the difference between static and instance methods in Java?
Answer: Instance method are methods which require an object of its class to be created before it can be called. Static methods are the methods in Java that can be called without creating an object of class. ... Instance method is not with static keyword. Static method means which will exist as a single copy for a class.
47. Can final method be overloaded?
Answer: Yes. It is possible to have overloaded final methods, and it is quite similar to having overloaded non-final methods in Java. However, overriding final methods is not allowed.
48. Can final/Static methods be overridden?
Answer: Yes. It is possible to have overloaded final methods, and it is quite similar to having overloaded non-final methods in Java. However, overriding final methods is not allowed.
In Question 47 & 48 : Answer is same and question is different how is it possible
ReplyDeleteFinal method can be overloaded but final/static methods cannot be overridden.