I’m not sure what it’s called but you can override methods easily with:
Apple foo = new Apple(){
public void devour(){
//Devour apple
}
};
And you can get the Class of an object with getClass.
Is it possible to do something like this:
Apple a = new Apple();
Class<? extends Apple> B = a.getClass();
Apple c = new B(){
public void polish(){
//Polish apple
}
};
Side Note: I’m asking this question because I specifically want to override a single method in the current swing UI class for a component returned by UIManager.getUI(component).getClass() in this code.
In the first code snippet you are creating an anonymous subclass of
Applewhich overrides thedevour()method, and then instantiatingfooas an instance of this anonymous subclass.In the second example, and in your goal, you cannot change the class/type of an object reference after that object already exists.