Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

The "super" reference

Posted on 2013-12-04
7
Medium Priority
?
367 Views
Last Modified: 2013-12-05
I am trying to utilize inheritance to use variables from a parent class in a child class. So that I don't have to repeat everything, I am simply using the " super(parameters) ; " statement. But when I do this I keep getting an error that says, "error: can not reference variable before supertype constructor has been called."

I don't know what this is telling me to do.

I'm using jgrasp writing java.
0
Comment
Question by:aminkeith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 300 total points
ID: 39697425
Can you post the code that is having this problem?

If I had to guess it sounds like you might have:

public class MyBase {
    protected int value ;

    public MyBase(int theValue) {
        value  =theValue ;
    }
}

public class Child extends MyBase {
    public Child(int theValue) {
       super(value) ;  // Accidentally using base class's variable
    }
}

But if you post the code it'll be easier to figure out :)

Doug
0
 
LVL 12

Accepted Solution

by:
Gibu George earned 300 total points
ID: 39697479
If you want to use the instance variables of the super class in child class, you need to make those instance variables protected instead of private, then in the first line of code in the constructor of the child class should the super() call, with super class constructor parameters
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 600 total points
ID: 39697634
>> can not reference variable before supertype constructor has been called
The first thing to do in a method you override is calling super()
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Assisted Solution

by:Valeri
Valeri earned 300 total points
ID: 39697671
it means that you are trying to use variable (the one that is mentioned in the error message) that is still not created, because the object that this variable is part of is still not created.
When you invoke "super(parameters);" probably one of your parameters is member of the child class or base class, both of them are still not created.
Also keep in mind that "super" should be the first line in the constructor of the child classes.

Shoud be something like that:

public class Base {
    protected int valueBase ;

    public Base(int theValue) {
        valueBase = theValue ;
    }
}

public class Child extends Base {
    private int valueChild;

    public Child(int theValue) {
       super(theValue) ; // creates the base class first
       valueChild = theValue;      
    }
}
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 600 total points
ID: 39697678
>> The first thing to do in a method you override is calling super()
That should have been:
The first thing to do in a constructor you override is calling super()
0
 

Author Comment

by:aminkeith
ID: 39700135
Thanks all of you guys. I actually figured out what my problem was. I think I had them set to private instead of protected. Thanks anyway.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 39700337
You're welcome.
Thanx 4 axxepting
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question