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?
-
2Can you subclass the existing class? – Paul Tomblin Dec 05 '12 at 19:30
-
you can extend the class that you have and in the new class add new method... – melli-182 Dec 05 '12 at 19:30
-
1Do you control the source? Think about extending or composition. – jlordo Dec 05 '12 at 19:30
-
25I am not sure why people down-voted this... I think what the guy wanted is an equivalent to extension methods from C#, where you don't have to subclass class A, but you can write an extension method foo() that then you can call on anything that is A or inherits from A... (A.foo()). Now I don't know if there is such thing, but it is tremendously useful. – Daniel Gruszczyk Nov 21 '14 at 11:08
-
2I am also looking for the functionality Daniel described above. I can't subclass the existing class (Selenium's Window class) because a lot of framework code instantiates it and I can't update them to instantiate my subclass instead. So I need to inject few methods into this class. The alternative is to create a new class hierarchy, but injecting methods directly into Window class would be cleaner. Does Java have something similar to C# extension methods? – atlantis Apr 17 '15 at 17:15
4 Answers
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!

- 1,627
- 1
- 11
- 11
-
2The OP asked about adding to an *existing* class, so suggesting creating a new class obviously doesn't satisfy the request. – DavidJ Jan 17 '19 at 21:05
You can extend classes in Java. For Example:
public class A {
private String name;
public A(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
public class B extends A {
private String title;
public B(String name, String title){
super(name); //calls the constructor in the parent class to initialize the name
this.title= title;
}
public String getTitle(){
return this.title;
}
public void setTitle(String title) {
this.title= title;
}
}
Now instances of B
can access the public fields in A
:
B b = new B("Test");
String name = b.getName();
String title = b.getTitle();
For more detailed tutorial take a look at Inheritance (The Java Tutorials > Learning the Java Language > Interfaces and Inheritance).
Edit: If class A
has a constructor like:
public A (String name, String name2){
this.name = name;
this.name2 = name2;
}
then in class B
you have:
public B(String name, String name2, String title){
super(name, name2); //calls the constructor in the A
this.title= title;
}

- 14,034
- 15
- 82
- 102
-
1Thanx, rather helpful! now how many super(field) i can use, because if the constructor of ex. the upper paradigms has two fields the second is getting wrong. – Pots Spi Dec 05 '12 at 21:11
-
Ah, you just do one call to super. Basically it is calling the constructor of the parent class. So you only call it once in the constructor and it must be the first line. See edit. – Jacob Schoen Dec 05 '12 at 21:19
The examples only really apply if the class you're extending isn't final. For example, you cannot extend java.lang.String using this method. There are however other ways, such as using byte code injection using CGLIB, ASM or AOP.

- 762
- 7
- 16
Assuming this question is asking about the equivalent of C# extension methods or JavaScript prototypes then technically it is possible as this one thing that Groovy does a lot. Groovy compiles Java and can extend any Java class, even final ones. Groovy has metaClass to add properties and methods (prototypes) such as:
// Define new extension method
String.metaClass.goForIt = { return "hello ${delegate}" }
// Call it on a String
"Paul".goForIt() // returns "hello Paul"
// Create new property
String.metaClass.num = 123
// Use it - clever even on constants
"Paul".num // returns 123
"Paul".num = 999 // sets to 999
"fred".num // returns 123
I could explain how to do the same way as Groovy does, but maybe that would be too much for the poster. If they like, I can research and explain.

- 673
- 1
- 9
- 14
-
3
-
1Correct - I was trying to be helpful adding more information. Obviously extending/sub-classing is the usual way and plenty of folk pointed that out, but if for some reason we take the questioner literally --- he wanted to add fields/methods to an existing Class in Java, then that is not for the light hearted. Groovy is basically an improved duck typed Java. One of the features they added was just that ability. Groovy is pretty awesome and as a Java programmer myself would recommend looking into it. – Paul Bartlett Nov 29 '17 at 18:56
-
2Yes, I understand, and I might even agree with you about Groovy, but when someone comes here to look for Java help, a Groovy advertisement is not the right answer. – PLG Nov 29 '17 at 19:05
-
1This answer does not have anything to do with java.lang.reflections. I don't even understand what is this. – Surajit Biswas Apr 02 '19 at 12:16
-
Actually my answer does have a lot to do with Java :-) Groovy can extend a Java class and you can then use it from Java code. Groovy is a JVM language. Hopefully this explains my rationale. – Paul Bartlett Oct 24 '19 at 06:02