-3
Object x = itr.getInstance();
AbstractGuiTest currentCase = (AbstractGuiTest)x;
WebDriver driver  = currentCase.getDriver();

AbstractGuiTest is a class.

Can someone explain how the syntax of having the class within brackets beside an object like so work?

Need to know some more examples where a class can be used in that manner.

Update:

itr is of the type ITestResult

SteroidKing666
  • 553
  • 5
  • 13
  • It’s a cast. Is `itr` a `Class` instance? If so, you don’t need to cast if the class object is typed. – Bohemian Apr 05 '18 at 02:40

1 Answers1

1

That is to cast object x of type Object which is superclass of all the classes in java to AbstractGuiTest

This happens quite frequently in Java because of inheritance. So, in your case AbstractGuiTestis subclass of Object class or in other words inherits from Object. So, by using this AbstractGuiTest currentCase = (AbstractGuiTest)x; you are making x of type AbstractGuiTest.

If you use this typecasting on classes which are not in the same class hierarchy, you will get ClassCastException

Another example could be: Animal is superClass and Dog is its subclass

public class Animal{ //some code goes in here }

public class Dog extends Animal{ //some code goes in here } Now, you can use this syntax:

Animal a = new Dog(); Dog d = (Dog) a;

You should read more on inheritance in java to make this concept clear.

humblefoolish
  • 409
  • 3
  • 15