How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask R.A. McConnell Your Own Question

R.A. McConnell
R.A. McConnell, Consultant
Category: Programming
Satisfied Customers: 757
Experience:  A-Plus, MCP, 18+ years experience, LINUX admin
Type Your Programming Question Here...
R.A. McConnell is online now
A new question is answered every 9 seconds

Hello, I have a problem with inheritance in Java classes.I

This answer was rated:

Hello, I have a problem with inheritance in Java classes.


I really need an expert who can communicate in plain english rather than technical language.


The extends keyword doesn't seem to be working.


I am in the process of creating a java program which stores details of songs from music cds. As you can probably guess I am practising using multiple classes and inheritance. The program so far consists of 3 classes; 1 introductory class called YourTracks, 1 Superclass called Tracks, 1 subclass called StoringTracks, I have used the "extends" keyword on the StoringTracks class. The code for these classes can be found via these links: The YourTracks java file compiles successfully into a class file no problem. However, for some reason, whenever I try to compile the Tracks and StoringTracks java files into class files (using Command Prompt) I get error messages, saying "cannot find symbol" as if nothing is being inherited from the superclass into the subclass. The computer does seem to find each file okay, it's only when I try to specify a package that it gives an error, so my computer must be configured to automatically look in the same directory. I cannot emphasise enough that I am still learning and I need an expert who can explain in plain english rather than technical language. I look forward to hearing from anyone who can help, Thank you, XXXXX XXXXX

Hello and welcome to JustAnswer. I'll be happy to help you with this.

It looks like the problem code is in, which creates an object of type StoringTracks as an action listener. This creates an infinite loop, as is set to extend a class that references an object of its type. In plain language, you're trying to stuff one box inside the other, then stuff the outer box inside the inner one. A quick scan of your code and it looks like you'd be better off bagging and just place its code in the definition of your action listener on storecdtrack. When modified, it would look like this:

storecdtrack.addActionListener(new ActionListener(){

public void actionPerformed (ActionEvent event){

int n = Integer.parseInt(cdtrackfield.getText());
String t = cdtracknamefield.getText();
double d = Double.parseDouble(cdtracktime.getText());
String f = cdtracknotes.getText();


} // end actionPerformed()

}); // end storecdtrack.addActionListener()

If you would like clarification on any of these points, more depth in my answer, or if you have additional questions with regard to this matter, please don't hesitate to ask for more information by responding to me here and I'll be happy to continue to lend a hand.
Customer: replied 3 years ago.

Hello, thank you very much for your response - I've been waiting all day for someone to reply like you have!

I've tried your suggested adjustment, however now it's saying I've got to declare the textfield names as final because they're now accessed from within an inner class. That shouldn't be a problem though, I'm about to correct that.


However, I still need help with inheritance. Since I posted my question I tried the simplest way I could think of to practise inheritance (just declaring a text variable inside a superclass then trying to print it from a subclass and STILL it's saying variable not found.

Is there any reason the extends keyword wouldn't be working, or is my computer configured wrong?

I can't find anyone else on the internet having this problem.


I'd appreciate any help you can give,

Thank you



Any variables declared in the superclass and not inside a method of the superclass should be readily available to all methods in the classes that instantiate or extend the superclass. To be 100% correct, you should practice encapsulation in each class, providing public methods for setting, retrieving, and changing the values of variables in any class, although I understand the desire to just use the variable directly in a class that extends the superclass.

If you've got code, I would be glad take a look at it and see what's up.
Customer: replied 3 years ago.

Thank you, XXXXX XXXXX sense. Now please can you tell me how to make the following variable name, cdtrackfield, final


Here is the line of code:

JTextField cdtrackfield = new JTextField("Number",5);


I need to do the same for all 4 text fields.

I know how to normally declare an integer or String variable as a final, but not the likes of JFrame components.


I've tried

"final JTextField cdtrackfield"


"JTextField final cdtrackfield"


but I keep on getting the response "identifier expected" and other frustrating error messages.


Any help would be greatly appreciated!


The final keyword is typically used on classes and methods, but not on objects (like those of type String and JTextField) or primitive variable types. Are you trying to prevent inheritance from subclasses or prevent overriding? These are a couple of the key features of Object Oriented Programming.

You can apply final to a variable (or object), but you never get to modify it after you set an initial value for it (by assignment). If this is what you mean you might try this:

public final JTextField cdtrackfield;

cdtrackfield = new JTextField("Number",5);

I'm not 100% sure on this, as I don't think I've had occasion to use it, but it seems like the thing here. Even so, it's generally not my preference to use it unless there's a deeply compelling reason.
Customer: replied 3 years ago.

I know what you mean about the final keyword, I very rarely use it, only if I have to.

In this case, I was only using it because the names of the textfield objects I created were being used inside an inner class(the action listener's class) therefore when I tried to compile it I got an error message saying those varialbes (the textfield names) needed to be declared as final.

But since I wasn't going to use those textfields for anything else, just that one purpose I went ahead and tried it (I remember reading somewhere that you can make a reference to an object final, you just can't make the object itself final).


This seems to be working fine now, which is good, there's just one more thing I'd like to know which is related to my code.


If I wanted to add more buttons and consequently more actionlisteners to my second frame (the frame with the textfields),

how would I specify which button is for which actionlistener?

I would prefer not to use anonymous classes for each button.


Imagine I had 2 buttons in the Tracks class, just call them button1 and button2 activating method1 and method2 to keep it simple.

What would be the code for adding one actionlistener at a time

(you can just specify one of them, I'll follow that example.)


Sorry to bother you again, but this is the last thing I have a query on for now....


You've been great so far!

It's not a bother. This is what I do all day.

Insofar as I understand it, you'll need to add the action listener to each button independently, like so:

JButton button1 = new JButton("Button1");

JButton button2 = new JButton("Button2");

button1.addActionListener(new ActionListener(){

public void actionPerformed (ActionEvent event){

//method1() code goes here



button2.addActionListener(new ActionListener(){

public void actionPerformed (ActionEvent event){

//method2() code goes here



In both cases, your method is anonymous, although you should be able to call a method from an object created from a class (a handler) you declare elsewhere. If you go this route, I'd declare the handler class within the body of the class that is adding the item listener, then use it like this:

public class HandlerClass implements ActionListner {

private SomeClass localInstance;

public HandlerClass(SomeClass dummyInstance) {

localInstance = dummyInstance;


public void actionPerformed(ActionEvent e) {

// actual code for the handler goes here, using the lButton object, which represents the object instance that this is the handler for


JButton button1 = new JButton("Button1");

button1.addActionListener(new SomeClass(nameOfInstanceToPassIn));

Forgive my formatting. Tight controls on this are hard here. I'll edit it later, if it gets too messy.

Customer: replied 3 years ago.

Thank you for your efforts, I've never used a "handler" before.

Please can you tell me what they are and how they are an advantage?


In simple terms if you can,


Thank you

It's a bit complex, but I'll try to be brief. A handler is a class that you use to create an object that will simply plug into your someObject.addActionListener(); statement. It allows you to define and manage the code that goes into the action listener elsewhere, making it more portable and maintainable as well as generic, and then just pass in the reference to the object instantiated from the class, to register that object as being the handler for someObject. The New Boston (Bucky Roberts) has some excellent tutorials on this (although he uses addItemListner() instead, but the theory is similar) here:

I'd recommend watching numbers 52-54, and any others that interest you. Numbers 62 and 63 should be of particular interest. Also, his style is one that I find very accessible and non-technical, which works better for most people, and he's funny to boot.
R.A. McConnell, Consultant
Category: Programming
Satisfied Customers: 757
Experience: A-Plus, MCP, 18+ years experience, LINUX admin
R.A. McConnell and other Programming Specialists are ready to help you

JustAnswer in the News:

Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.

What Customers are Saying:

  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
< Previous | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland

Meet The Experts:



    Computer Software Engineer

    Satisfied Customers:

    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
< Last | Next >
  • ATLPROG's Avatar


    Computer Software Engineer

    Satisfied Customers:

    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
  • LogicPro's Avatar


    Computer Software Engineer

    Satisfied Customers:

    Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
  • unvadim's Avatar


    Computer Software Engineer

    Satisfied Customers:

    Good knowledge of OOP principles. 3+ years of programming experience with Java and C++. Sun Certified Java Programmer 5.0.
  • lifesaver's Avatar


    Computer Software Engineer

    Satisfied Customers:

    Several years of intensive programming and application development experience in various platforms.
  • ehabtutor's Avatar


    Computer Software Engineer

    Satisfied Customers:

    Bachelor of computer science, 5+ years experience in software development, software company owner
  • TheDoctor's Avatar


    Software Engineer

    Satisfied Customers:

    M.S. in Internet Information Systems
  • The-PC-Guy's Avatar


    Software Engineer

    Satisfied Customers:

    Extensive Knowledge in PHP, MYSQL, CSS & Javascript

Related Programming Questions