Solved

Override method problem - StackOverflowError

Posted on 2013-05-14
2
216 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

920 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

14 Experts available now in Live!

Get 1:1 Help Now