I have a class B, that extends from class A, class B overrides a class A’s method:
public class ClassB extends ClassA {
@Overrides
protected void DoSomething() {
super.DoSomething();
// Some ClassB code here, called WorkB
}
}
After that, I create a class B object, and I need do something extra in addition to what A’s version in DoSomething():
ClassB objB = new ClassB() {
@Overrides
protected void DoSomething() {
// I need to do something more here
}
}
I need to know if I can do ALL of ClassA works, ClassB works, then add some other code for objB in this method? I have a solution: create a public method in ClassB, that do WorkB, then in objB, just call the method. But I want to know if there is/are another way (no matter worse or better my solution!).
EDIT: I will summarise the question so you can easily understand it:
Class A‘sdoSomethingmethod does something called WorksA.Class BoverridesdoSomething, call super.doSomething() and some extra code, which mean it doesWorksAand an extra calledWorksB.- Object
objBdoSomething()method has to doWorksA,WorksBand another extra calledWorksBExtra.
That’s all.
Yes just call
super.doSomething()first and it will throw up until Class A and then Class B. After that you can do specific stuff.}
}
Outputs A, B, new B