Java OOPS

Refer to the below Links for Practise
Collections in Java
Complete List of Selenium Basics
Top 50 Selenium Real Time Interview Questions
Real Time Scenario’s
Java Platform Standard Edition 8 Documentation
Complete List of Java Basics
Java Quiz
Java OOPS Quiz
Selenium Advanced Quiz
Selenium Quiz
Selenium WebDriver Resume

Inheritance,Abstract Class,Interfaces,Polymorphism

java oops,inheritance,interfaces,abstract class,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:

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”
  • Vehicle is the superclass.
  • Car is the subclass.

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:

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:

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.

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.


Abstract Classes in Java

Key Points:

  1. An abstract method must be declared using the abstract keyword.
  2. If a class contains at least one abstract method, it must be declared as abstract.
  3. An abstract class can have both abstract and concrete methods.

    Example: engine() is abstract, brakes() could be concrete.
  4. It is not mandatory for an abstract class to contain abstract methods.
  5. You cannot instantiate (create objects of) an abstract class.

    This means you cannot access non-static methods directly from an abstract class.
  6. 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).
  7. 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:

Polymorphism:


1 comment on “Java OOPS

  1. Sitha

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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *