Click here to Skip to main content
15,887,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello.

Im a newbie and i really hope you guys can teach me a thing or two.

I just want to access, values of certain variables, from the superclass. But my constructor is giving me issue:

Java
public class Calculator extends SwingFrame {
 
    SwingFrame testingobj = null;
 
    public Calculator(SwingFrame frame) {
        this.testingobj = frame;
    }
     
    public int test = 0;
     
    void someMethod(){
        //method
    }
 
}


The SwingFrame Class:
Java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    Calculator callthemethod = new Calculator(this); 
    callthemethod.someMethod();


This works perfectly. It will call the method, every time i press the JButton.

Here is the issue:


Java
public class Mastering extends Calculator {
 
    public Mastering(SwingFrame frame) {
        super(frame);
    }
        
}


So now my Mastering class is subclass of Calculator and will inherit the constructor.

But what if i want to access, variables from the Calculator class?

I tryed:

Java
Calculator test = new Calculator();


It tells me, that: Calculator in class Calculator, cannot be applied to given types:

required: SwingFrame.

And now, i dont know how to call variables, from the Calculator class(the Calculator test = new Calculator(); is the only way, i know how to access variables from other the class).

Please help me, i just kept looking for solutions for hours and days. But im stuck.

Thank you very much.
Posted
Updated 9-Dec-13 6:18am
v2

1 solution

Since you have inherited from the Calculator class, you have also inherited all its public and protected variables. So, you can access them directly by name, anywhere inside your class. See this tutorial[^] for a fuller description of the rules on i nheritance.
 
Share this answer
 

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