QTP Introduction to object oriented programming

QTP Introduction to object oriented programming

Object-oriented programming (OOP) is a programming paradigm that uses “objects” – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, polymorphism, and inheritance. Many modern programming languages now support OOP. An object is a discrete bundle of functions and procedures, often relating to a particular real-world concept such as a bank account holder, a hockey player, or a bulldozer

Fundamental concepts

Inheritance

Inheritance is a process in which a class inherits all the state and behavior of another class. This type of relationship is called child-Parent or is-a relationship. “Subclasses” are more specialized versions of a class, which inherit attributes and behaviors from their parent classes, and can introduce their own.

For example, the class Dog might have sub-classes called Collie, Chihuahua, and GoldenRetriever. In this case, Lassie would be an instance of the Collie subclass. Suppose the Dog class defines a method called bark() and a property called furColor. Each of its sub-classes (Collie, Chihuahua, and GoldenRetriever) will inherit these members, meaning that the programmer only needs to write the code for them once.

Each subclass can alter its inherited traits. For example, the Collie subclass might specify that the default furColor for a collie is brown-and-white. The Chihuahua subclass might specify that the bark() method produces a high pitch by default. Subclasses can also add new members. The Chihuahua subclass could add a method called tremble(). So an individual chihuahua instance would use a high-pitched bark() from the Chihuahua subclass, which in turn inherited the usual bark() from Dog. The chihuahua object would also have the tremble() method, but Lassie would not, because she is a Collie, not a Chihuahua. In fact, inheritance is an “a… is a” relationship between classes, while instantiation is an “is a” relationship between an object and a class: a Collie is a Dog (“a… is a”), but Lassie is a Collie (“is a”). Thus, the object named Lassie has the methods from both classes Collie and Dog.

What Member Variables Does a Subclass Inherit?


Rule: A subclass inherits all of the member variables within its superclass that are accessible to that subclass (unless the member variable is hidden by the subclass).


The following list itemizes the member variables that are inherited by a subclass:

  • Subclasses inherit those member variables declared as public or protected.
  • Subclasses inherit those member variables declared with no access specifier as long as the subclass is in the same package as the superclass.
  • Subclasses don’t inherit a superclass’s member variable if the subclass declares a member variable using the same name. The subclass’s member variable is said to hide the member variable in the superclass.
  • Subclasses don’t inherit the superclass’s private member variables

 

Encapsulation

Encapsulation conceals the functional details of a class from objects that send messages to it.

For example, the Dog class has a bark() method variable, data. The code for the bark() method defines exactly how a bark happens (e.g., by inhale() and then exhale(), at a particular pitch and volume). Timmy, Lassie‘s friend, however, does not need to know exactly how she barks. Encapsulation is achieved by specifying which classes may use the members of an object. The result is that each object exposes to any class a certain interface — those members accessible to that class. The reason for encapsulation is to prevent clients of an interface from depending on those parts of the implementation that are likely to change in the future, thereby allowing those changes to be made more easily, that is, without changes to clients. For example, an interface can ensure that puppies can only be added to an object of the class Dog by code in that class. Members are often specified as public, protected or private, determining whether they are available to all classes, sub-classes or only the defining class.

 

Polymorphism

Polymorphism refers to a programming language’s ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results

Abstraction

Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes.

 

Advantages of OOC in VB Scripting:

Advantages Are:

  • the reuse of code
  • the encapsulation of complex functionality
  • the ability to change and add functionality without breaking the applications using the classes

In fact VBScript is a programming language that lends itself very nicely to object oriented programming and, in just a few minutes, the programmer can produce classes with properties that they can then go on and use in all of their applications.

Only Encapsulation is supported in VB Script. whereas Polymorphism & Inheritance are not supported.

Features of Class:

Class

A class is a template for an object, a user-defined datatype that contains variables, properties, and methods. A class defines the abstract characteristics of a thing (object), including its characteristics (its attributes, fields or properties) and the things it can do (behaviors, methods, operations or features). One might say that a class is a blueprint or factory that describes the nature of something. For example, the class Dog would consist of traits shared by all dogs, such as breed and fur color (characteristics), and the ability to bark and sit (behaviors). Classes provide modularity and structure in an object-oriented computer program.. Also, the code for a class should be relatively self-contained (generally using encapsulation). Collectively, the properties and methods defined by a class are called members.

Instance

One can have an instance of a class; the instance is the actual object created at run-time. In programmer vernacular, the Lassie object is an instance of the Dog class. The set of values of the attributes of a particular object is called its state. The object consists of state and the behavior that’s defined in the object’s classes.

Method

Method is a set of procedural statements for achieving the desired result. It performs different kinds of operations on different data types. In a programming language, methods (sometimes referred to as “functions”) are verbs.

Member:

Member refers to the variables that are used for storing values in the class. It can be Private or Public.

Object

An object is basically a container for a set of variables and functions, and an object is normally used to represent some kind of entity. Ex: a Car is an entity; Sale of a Product is an entity etc

Property:

Class properties in VBScript are used to assign values to private variable and handle the process of data validation.

Property Let: Which is used by the outside code to store a value in the private property variable. It is similar to a procedure in the sense that it does not return a value. A Property Let procedure must accept at least one argument. If the private variable you are using is an object then the process of assignment and data validation is handled by Property Set.

 Property Set: Similar to Property Let but used for object based properties. By default, the Property Set procedure is Public.

Property Get: To retrieve the value of a private variable we will retrieve the value of a property. This is used by code outside of your class to read the value of a private property variable. It is similar to a function in the sense that it returns a value to the calling code — this value is the private variable value.

The Property Get procedure does not accept any arguments. You can add an argument to it, but then you have to add an additional argument to the property’s corresponding Property Let or Property Set procedure, because Property Let/Set procedure must always have exactly one more argument than its corresponding Property Get procedure.

If the property get procedure returns an object then we can use the set statement (but it works well without set also) to return the value

Get – Used to get the value stored in a member variable

Let – Used to assign a value to a member variable

Set – Used to assign a object to a variable

 

Comments