Java: Adding fields and methods to existing Class?

Pots Spi picture Pots Spi · Dec 5, 2012 · Viewed 43.3k times · Source

Is there, in Java, a way to add some fields and methods to an existing class? What I want is that I have a class imported to my code, and I need to add some fields, derived from the existing fields, and their returning methods. Is there any way to do this?

Answer

awolfe91 picture awolfe91 · Dec 5, 2012

You can create a class that extends the one you wish to add functionality to:

public class sub extends Original{
 ...
}

To access any of the private variables in the superclass, if there aren't getter methods, you can change them from "private" to "protected" and be able to reference them normally.

Hope that helps!