Solved

Override method problem - StackOverflowError

Posted on 2013-05-14
2
214 Views
Last Modified: 2013-05-14
I have classes like so:
public class ParentClass {
    private Object myObject;

    public void setMyObject(final Object object) {
	this.myObject = object
    }
}

public class ChildClass extends ParentClass {
    @Override
    public void setMyObject(final Object object) {
        setMyObject(object);
        // do other stuff
    }
}

Open in new window


I want to override the method so that I can do some things not in the parent class method, but first I have to make sure that myObject gets set. As it is, this code gives a StackOverflowError because ChildClass.setMyObject() is called ad infinitum. I cannot change the access of myObject to protected. How is this done?
0
Comment
Question by:allelopath
  • 2
2 Comments
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 39165042
you have to invoke the method of the parent class in this way:

super.setMyObject(object);
0
 
LVL 16

Expert Comment

by:Valeri
ID: 39165050
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now