Inheritance,Abstract Class,Interfaces,Polymorphism
Inheritance in Java
1. Purpose of Inheritance: The main goal of inheritance is to enable code reusability.
2. Inheritance Explained: Inheritance refers to the process where a subclass (such as FirefoxDriver) inherits the methods and variables of its superclass (such as RemoteWebDriver). For example:
|
1 2 |
FirefoxDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); |
3. Achieving Inheritance: In Java, inheritance is established between two classes using the extends keyword. From now on, we can associate the extends keyword with the concept of inheritance.
Example:
- “Car is a Vehicle”
Vehicleis the superclass.Caris the subclass.
|
1 |
public class FirefoxDriver extends RemoteWebDriver |
4. Single Inheritance: In Java, single inheritance occurs when a subclass directly inherits from a single superclass using the extends keyword.
5. Multilevel Inheritance: In multilevel inheritance, a subclass can inherit from a superclass, and that superclass can inherit from another class (its own superclass). Essentially, a subclass has access to the members of both its immediate superclass and the class above that.
6. Multiple Inheritance: In Java, a class cannot extend more than one class at a time. Therefore, multiple inheritance (inheriting from more than one class) is not supported in Java.
Example:
|
1 |
ClassC extends ClassB, ClassA // Not valid in Java |
The Diamond Problem in Java: In multiple inheritance scenarios, there can be ambiguities or conflicts in method calls. This is known as the “Diamond Problem”. Java avoids this issue by not supporting multiple inheritance for classes.
Example Code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class Object { public int hashCode() { return 1; } } public class RWD extends Object { public void get(String url) { // Implementation } } public class FirefoxDriver extends RWD { // FirefoxDriver inherits from RWD } public class IndeedTests { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver"); FirefoxDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); } } |
Methods in Java
In Java, methods are categorized into two main types:
a. Concrete Methods
Definition: Methods that have a body (curly braces) are called Concrete Methods.
|
1 2 3 4 5 |
// Concrete Method (Implemented) public int add(int a, int b) { // Method body } |
Any method with a body (i.e., contains curly braces) is considered implemented.
b. Abstract Methods
Definition: Methods that do not have a body and are declared with the abstract keyword are called Abstract Methods.
|
1 2 3 |
// Abstract Method (Incomplete) public abstract int add(int a, int b); |
Abstract Classes in Java
Key Points:
-
An abstract method must be declared using the
abstractkeyword.
123456789101112// Abstract Class Examplepublic abstract class Car {// Concrete Methodpublic void gears() {// Implementation}// Abstract Methodpublic abstract void engine();} - If a class contains at least one abstract method, it must be declared as
abstract. - An abstract class can have both abstract and concrete methods.
Example:engine()is abstract,brakes()could be concrete. - It is not mandatory for an abstract class to contain abstract methods.
- You cannot instantiate (create objects of) an abstract class.
This means you cannot access non-static methods directly from an abstract class. -
Static members of an abstract class can be accessed using the class name.
Example:
By.name(""),By.className("")- Static methods belong to the class (class-level).
- Non-static methods are accessible via subclass objects (object-level).
- A subclass that inherits from an abstract class must override all abstract methods, unless the subclass is also declared abstract.
Summary
- Static methods → Invoked using the class name.
- Concrete (non-static) methods → Inherited and used within the subclass.
- Abstract methods → Must be overridden in the subclass and invoked from there.
Interface:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
An interface is a reference type in Java, it is similar to class, it is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods an interface may also contain constants and static methods. It doesnt contain non-static methods final int i=10; i=15; // Error final variables are constants.. public class Shapes // circle,cylinder,square,rectangle { public abstract void area(); public abstract void noofsides(); } interface Shapes { void area(); public abstract void noofsides(); } Points to be noted for interfaces: 1. An interface is a group of related methods with empty bodies.Ex: Abstract Methods 2. All interface abstract methods are by default/optional abstract and public. Ex: Default means hear it is optional Ex: void area() or public abstract void area() 3. We cannot develop non static concrete methods inside interfaces. 4. So, in interfaces the methods are only public ,abstract,static. No other keywords are allowed. 5. Interface variables has to be initialized at the time of declaration. 6. All the interface variables are by default public static final. 7. Interfaces are going to be implemented by subclass using a keyword called 'implements' 8. A SubClass implmenting an interface should implement all the abstract methods of the interface. 9. A class can implement multiple interfaces. Mulitiple inheritance is achieved using interfaces ClassA implements interfaceA,interfaceB { } ------------------------------------------------------------------------------------------------------ Implementing an interface by a class can be written as follows: Interface refvar = new ClassName(); WebDriver driver = new FirefoxDriver(); WebDriver driver = new ChromeDriver(); WebDriver driver = new InternetExplorerDriver(); final variables are constants final int i =5; i=6; //not possible.. final class cannot be inherited. Example: public final class System{} public class Example extends System{} // not possible final methods cannot be overridden. |
Polymorphism:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
Polymorphism its an most imp oops concept. Polymorphism means existence of Methods and objects in many forms.2 types of polymorphism in java. Note: Compile Time checks for syntax. Run Time memory allocation happens for objects,variables. 1. Compile Time/Static Binding/Method Overloading: ------------------------------------------------------- Existence of two or more methods with in a class which are having same name but having parameters differ in the following way: a. Type of Arguments public void area(int a){} public void area(float a){} b. Position of Arguments public void area(int a,float b){} public void area(float a,int b){} c. No of Arguments public void area(int a){} public void area(int a,int b){} Then we should call these methods are overloaded methods. Method invocation is mapped to method impl during compile Time. Static Binding means the mapping is fixed it will not change during run time. Method Overloading happens with in a class. for priting integer-> int i =5; System.out.println(i); for priting float-> float f =3.5f; System.out.println(f); Constructor Overloading: -------------------------- Constructor is special type of method which is having same name as the classname, but it doesnt have any returntype. public class Student { Student()//Default Constructor { } Student(int i )//Parameterized Constructor { } Student(float f )//Parameterized Constructor { } } 2. Run Time/Dynamic Binding/Method Overriding ------------------------------------------------ If a method which is having same name and same arguments available in superclass and subclass are known are Overriden methods. Method invoication mappedto Method implementation is changed during compile to run time. So, we call this as run time polymorphism and Dynamic Binding. Note: A SuperClass can store its subclass object. Animal a = new Bird(); Bird is-a Animal. 'is-a' an inheritance relationship. public class Animal{} public class Bird extends Animal{ main() { Animal a = new Bird(); } } -------------------------------------------------------------------------------------------------------- Overview of System.out.println() public class System { static PrintStream out =new PrintStream(); } public class PrintStream { void println(int i) { } } |

Hello there, Where do we use the OOPS concepts – Polymorphism, Abstraction and Encapsulation in Selenium ? Please share with examples.