Overriding User Library Classes in Java

不羁岁月 提交于 2019-12-04 18:13:36

Since the library classes are final, you can't extend them. Another option is to wrap them in your own classes. For example, you can create your own drawing class that has an instance of StdDraw that it delegates to. For many of your methods you can simply call the corresponding method of the StdDraw instance, or you can simulate overriding by implementing methods yourself.

rooftop

Just simply extend the class,

public MyClass extends ClassFromLib

make sure the library jar file is on the classpath. If the author of that class declared it as final indicating that it's not suitable for subclassing. then the best alternative is to use the delegate pattern.

I wrote the code below in this editor so no promises that it complies, but hopefully you get the idea.

public Myclass {
    private ClassFromLib cfl = new ClassFromLib();

    public void methodA(){
        //Do whatever you need here
        cfl.methodA();  //Doesn't have to be the same name. 
        //Do whatever you need here
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!