Click here to Skip to main content
14,740,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following example code
Public class A extends JFrame{

public A()
{
setSIze(500,500);
}
}

Public class B extends A{

public B()
{

setSIze(200,200);
}
}

If i instantiate B it will first call A
So it will setSize(500,500)
Then it will setSize(200,200)

Is there a way (without parameters)
To call setSize of B only?
Thank you

What I have tried:

I didnt try anything yet because i dont know how to do it
Posted
Updated 13-Jan-21 9:36am
Comments
Richard MacCutchan 13-Jan-21 15:25pm
   
Take the setSIze call out of class A. It does mean that you will need to add something else for applications that use class A directly, but you do have to follow the rules.

1 solution

No. A class that extends a base class includes everything from the base as well as adding the new features - so the constructor of the base class is always automatically called before the constructor for the derived class, or the derived class would be unable to use anything from it's base and that would make the whole thing unusable.
   
Comments
Member 11296776 13-Jan-21 14:41pm
   
Thank you for replying
Is there a way to call setSize
Maybe declare height and width variables in A and remove the setSize from B just set the required values for each class?
OriginalGriff 13-Jan-21 15:16pm
   
The A constructor is called before the B constructor - always - so you can't do anything that would affect it from the B instance creation code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900