deepakguna deepakguna -3 years ago 126
Java Question

how to call inherited class constructor in the inner class

I need to call inherited class constructor in the inner class. Say i have Outerclass that inherits Baseclass and i have an inner class inside Outerclass. From this inner class's constructor i need to invoke BaseClass constructor?

public class BaseClass {
public BaseClass (WebDriver driver)
this.driver =driver;
PageFactory.initElements(new AjaxElementLocatorFactory(driver, maxTimeOutForElement), this);

public class OuterClass extends BaseClass {
OuterClass (WebDriver driver)

//Does something

public class InnerClass
InnerClass (WebDriver driver)
//is there a way to call Baseclass constructor super(driver)
//I don't want to call directly new AuthorLoginPage(driver) breaking
//my constructor chain.

Answer Source

The answer to:

//is there a way  to call Baseclass constructor super(driver) 

is no. Keep in mind: InnerClass isn't static. This means: objects of InnerClass can't exist "on their own". The only way to create an instance of InnerClass is by first creating an instance of OuterClass.

At the point in time when InnerClass objects can be created (their constructor code being executed) - there is no point in calling the constructor of the super class. Because InnerClass has no superclass (besides Object). And it doesn't make sense to call the constructor of the enclosing OuterClass - because that OuterClass object already exists!

Or, explanation via code:

OuterClass outer = new OuterClass(someDriver);
OuterClass.InnerClass inner =; 

Leading to: step back, and look into your model. It doesn't make much sense on a first deeper glance.

And to address the comment by the OP: InnerClass extends Object! It has no direct relation to BaseClass. Besides the fact that any InnerClass object belongs to an OuterClass object - and that one extends BaseClass!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download