Java: Adding fields and methods to existing Class?

后端 未结 4 733
终归单人心
终归单人心 2021-01-01 09:08

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

4条回答
  •  悲&欢浪女
    2021-01-01 09:57

    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!

提交回复
热议问题