HomeSelenium

Methods, Variables, Classes, Objects, Constructors, this, Overloading, Inheritance and Overriding (Selenium 4 – Session 7)


Methods

In Java programming, programming logic needs to be written inside methods:
  • main() method is a method where the program execution starts and we can write programming logic inside the main() method
  • Demonstrate creating multiple methods along with the below:
  • Creating multiple methods along with main() method
  • All the method should reside inside the Class
  • main() method calling other method
  • non-main() method calling other method
  • method() calling other method multiple times
  • Demonstrate single, multiple parameterized methods and passing arguments to those methods:
  • Create a single parameterized method
  • Create a multiple parameterized method
  • Call the single and multiple parameterized methods by passing the arguments while calling
  • Demonstrate returning the values back to the calling methods:
  • Return nothing from a method
  • Return int value from a method
  • Return String value from a method

Variables

Variable is a name provided to a reserved memory location.
  • There are two types of Variables:
  • Local Variables
  • A variable which is declared inside the method is called local variable
  • Global Variables
  • A variable which is declared inside the class but outside the method is called Global variable
  • Scope of the variables

Classes and Objects

  • Class encloses variables and methods
  • Class is a template used for creating Objects
  • Demonstrate creating a Class and use the Class as a template for creating Objects
  • Create a Class having a main method say Demo
  • Inside the same Java file create another Class named Car
  • Create any variables inside the Car class say model, cost, color
  • Create any methods inside the Car class say startCar(), stopCar(), carDetails()
  • Create any objects using Car class
  • Initialize and Access the variables & methods of Objects
  • Object Creation Statement
  • Car benz = new Car();

Constructors

Constructors are similar to methods, but have the below differences:
  • Demonstrate the constructors having the below qualities:
  • Constructors have the same name as Class name
  • Constructors are automatically called when an object is created for the Class
  • Constructors won’t have any return type – Return types like void, int etc won’t be available for constructors
  • Empty hidden Constructor will be called, when an object is created for the Class not specified with explicit constructors
  • Constructors simplify the initialization of variables
  • Demonstrate initialization of variables without using constructors
  • Demonstrate initialization of variables with constructors

this keyword

 

The purpose of the this keyword is to differentiate the instance variable with the parameterized variables of methods/constructors.
  • Using this keyword with Methods
  • Demonstrate the program which don’t use this keyword
  • Demonstrate the advantage of using this keyword with methods
  • Using this keyword with Constructors
  • Generally required for constructors, as constructors are automatically called when objects are created and there by all the required variables will be initialized automatically
  • Similar to methods.

Overloading

 

Duplicate methods/constructor names are allowed inside the same class, as long as their parameters count or declaration or order of parameters are different.
  • method overloading
  • Two or more methods having the same name can be created inside a single class as long as their parameters count or declaration are different.
  • In this case, the methods are said to be overloaded and the concept is knows as Method overloading
  • Compiler error will be displayed when more than one method has the same name
  • Demonstrate how method overloading concept can avoid compiler error
  • constructor overloading
  • The same concept of method overloading when applied to constructors is known as constructor overloading
  • In this case, the constructors are said to be overloaded and the concept is known as Constructor overloading

Inheritance

Inheritance is a mechanism in which one class acquires the properties (i.e. variables and methods) of another class

  • The purpose of this Inheritance is to use the properties (i.e. methods and variables) inside a class instead of recreating the same properties again in new class.
  • Child class acquires the properties (i.e. variables and methods) of Parent Class.
  • Child class uses extends keyword to inherit the properties from parent class
  • Demonstrate a child class which inherits the properties from Parent Class
  • Child class can have specific properties (i.e. variables and methods) which are not available in the parent class
  • Object created for parent class can access the variables and methods that are created in parent class only. It cannot access the child class properties.
  • Object created for child class which is inheriting the parent class can access the variables and methods of both parent class and child class.

Overriding

When a method in the Child class (i.e. sub-class) is duplicate of a method in Parent class (i.e. super-class) , then the method in the sub-class is said to override the method in super-class.

  • When we create an Object for Sub-class and call the overridden method, the method in the sub-class will be called
  • Even though the name of the method in the sub-class has the same name as a method in super-class, if the type of parameters or number of parameters, then the method in the sub-class will overload the method in super-class instead of overriding
  • Constructors cannot be overridden as the name of the constructor needs to be same as the name of the Class.

By,
Arun Motoori

Comments (0)

Leave a Reply

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

For FREE Testing Tutorials & Videos

X
Open chat
Contact Us on Whatsapp