Call of super.method automatically [duplicate] - java

Consider the following class
class A{
public void init(){
//do this first;
}
public void atEnd(){
//do this after init of base class ends
}
}
class B1 extends A{
#Override
public void init()
{
super.init();
//do new stuff.
//I do not want to call atEnd() method here...
}
}
I have several B1, B2,... Bn child classes which are already developed. All of them extend class A. If I want to add a new functionality in all of them, the best place to do so is define that in a method within class A. But the condition is that the method should always get called automatically just before the init() method of child class ends.
One basic way to do so is to again add atEnd() method call at end of init() method of child classes. But is there any other way to do this smartly ??

One way to do this is by making init() final and delegating its operation to a second, overridable, method:
abstract class A {
public final void init() {
// insert prologue here
initImpl();
// insert epilogue here
}
protected abstract void initImpl();
}
class B extends A {
protected void initImpl() {
// ...
}
}
Whenever anyone calls init(), the prologue and epilogue are executed automatically, and the derived classes don't have to do a thing.

Another thought would be to weave in an aspect. Add before and after advice to a pointcut.

Make init() final, and provide a separate method for people to override that init() calls in the middle:
class A{
public final void init(){
//do this first;
}
protected void initCore() { }
public void atEnd(){
//do this after init of base class ends
}
}
class B1 extends A{
#Override
protected void initCore()
{
//do new stuff.
}
}

The other answers are reasonable workarounds but to address the exact question: no, there is no way to do this automatically. You must explicitly call super.method().

Related

calling subclass method after initialising to parent java

This might (most certainly will) sound stupid, but I am stuck and I cant find a proper solution to my problem.
I have a superclass and two sub classes extend it. On the parent class based on a condition I want to call the method from either of the two classes. This is inside a loop, so instead of doing the same check I decided to do the check once, create an object from the super class and then change the object to either one of the two sub classes. i.e.
public class Parent{
public void method() {
Parent object=new Parent();
if(a==b) {
object=new Child_A();
}else {
object=new Child_B();
}
for() {
object.method();
}
}
public void method() {
//empty method. need it just to compile
}
}
public class Child_A extends Parent{
public void method() {
//do something useful
}
}
public class Child_A extends Parent{
public void method() {
//do something useful
}
}
I had to write the Parent.method(), cos otherwise the compile would complain that there is no method() method on class Parent.
So with this, the method called is not one of the children,but the parents method.
I have read that objects need to be assigned directly to the class, like Childen_A object=new Childen_A. The thing is that I would like to use the same command for both cases (object.method()) no matter which class it refers to. Strange thing is that during debug, i see that object is of type Child_A, nevertheless the super method is called.
I know that the solution would be to create two different objects, one for each sub class, but that would make my code a bit more ugly and i would have to use the if statement inside the loop.So the correct way of doing it must be
public void method() {
for() {
if(a=b) {
Child_A object=new Child_A();
object.method();
}else {
Child_B() object=new Child_B();
object.method();
}
}
}
Is there a way to avoid the if statement inside the loop? Thanks
Your code should be
public class Parent {
public void method() {
}
// OR
public abstract void method(); // and make the class abstract as well
}
public class Child_A extends Parent {
#Override
public void method() {
//do something useful
}
}
// same with Child_B

Java method that can't be callable but can be overridden

If I don't want that a method on my class can be called, I just make it private.
But if I want to allow that method to be overridden, I have to make it protected
Is it possible to have a method on an abstract class that can't be called but can be overridden? (I guess not, but is there any workaround?)
Use case:
abstract class Super {
protected void finalize() {
}
public final void doThings() {
// do stuff
finalize();
}
}
and whoever wanted to extend the class:
class Sub extends Super {
#Override
protected void finalize() {
closeSockets();
alertSomeone();
}
}
But I don't want other classes calling mySub.finalize();
Instead of overwriting a method, the sub-class may provide the super-class with a Runnable which contains the code to be executed. You could do something like this:
public class Super {
private final Runnable subClassCode;
public Super(Runnable finalizeCode) {
subClassCode = finalizeCode;
}
public final void doThings() {
// do stuff
subClassCode.run();
}
}
public class Sub extends Super {
public Sub() {
super(() -> {
// code to be executed in doThings()
});
}
}
You dont need to set the Runnable instance in the constructor. You may also give access to a protected setFinalizeCode(Runnable) method but that method could also be called by other classes within the same package as Super.

How do I selectively override functions of a class

I have an abstract class A which contains two functions, both implemented
public void entry(){}
public void exit(){}
These are internally making system calls which I want to avoid during certain circumstances. I'm a writing a test (separate class) from where I will be making my own calls but I don't want to interfere the normal functioning of the code. What are the ways in which I can achieve this?
One way I was thinking of was to create an interface B with these two functions and creating a class C inside the abstract class implementing this interface and containing the actual code. Inside the abstract class, I would have a function to set the interface instantiation that I want to use. example:
public static void setMockProvider(B provider) {
if (provider != null) {
mockProvider = provider;
} else {
mockProvider = new C();
}
}
And then wherever the entry and exit functions are called, I would do a mockProvider.entry() and mockProvider.exit()
I would suggest just like you said. Make an interface with these two methods. and override them where you want different implementations of them.
interface
public interface Interface
{
void methodA();
}
class A (abstract class)
public class A : Interface
{
void methodDescision(T parameter)
{
if(parameter != null)
{
methodA();
}else{
var YourInnerClass YIC = new YourInnerClass();
YIC.methodA();
}
}
#Override
methodA(){
// do default stuff
}
public class YourInnerClass
{
#Override
methodA(){
//do user defined stuff
}
}
}
class B (other class)
public class B : Interface
{
#Override
void methodA()
{
//your new implementation of that method
}
}
Make sure you implement your interfaces in both classes.
Hope this helps.
cheers!
Not quite sure if I fully understand the question but you want to make your own calls without interfering with how the base of the methods work (I'm assuming they are being inherited)?
If so you can do a super() call, the super will call the method of the class you inherited from.
#Override
public void entry() {
super();
//Your code
}

Is there a difference between calling a supermethod and not implementing?

In the following example, the call FirstChildclass first; first.someMethod(); will do the same as SecondChildclass second; second.someMethod();. Or am I wrong?
class Superclass {
public void someMethod() {
//do something
}
}
class FirstChildclass extends Superclass {
#Override
public void someMethod() {
super.someMethod();
}
public void someOtherMethod() {
//do something else
}
}
class SecondChildclass extends Superclass {
public void someOtherMethod() {
//do something else
}
}
Is there a reason why one will implement it like in FirstChildclass? Because I have seen many implementations like in FistChildclass and am wondering why anyone would do it.
If you ask about the difference between:
#Override
public returnType someName() {
return super.someName();
}
and not overriding it. There is no difference as well as there is no sense in doing it.
You should call super.someName() just in case you want to extend the original method.
Watch out! If you have a constructor you want to use in a child class, but you don't need any additional behavior, you'll call super(arguments).
This is about your design. Override means you wanna change the already existing implementation which is inherited from the parent.
If you are not doing anything different then you don;t want to do the
#Override
public void someMethod() {
super.someMethod();
}
That is useless.
#Override
public void someMethod() {
// do some other logic
super.someMethod();
// do some other logic
}
This is perfectly ok :) Because you are doing some tasks other than the parent's implementation.

Can't cope with inheritance

I have (some pseudocode):
public class Thrd extends Thread{
protected void letUsFinalize(){
int a = 0; // Just for debugging.
}
}
public class FreeThread extends Thrd{
#Override
protected void letUsFinalize() {
FreeThread.this.interrupt();
}
}
Please, have a look at the picture. Our object now is of class FreeThread (visible in the Variables subsection). So, I come to the upper break point in the picture, press Step into and I occur at the lower break point. I mean that I occur in the method of the class Thrd (superclass).
What should I do so that the method of subclass would execute in this case?
If the object that you are using is an instance of FreeThread, then calling object.letUsFinalise() will call the method from FreeThread.
It looks like you are calling letUsFinalise() from the super class, so it's not possible to call the subclass' method unless you are using a static object to it (demonstrated below).
public class SuperClass {
public void method(){
Objects.object.method();
}
}
class SubClass extends SuperClass{
#Override
public void method(){
System.out.println("I'm the sub class!");
}
}
class Objects{
public static SubClass object = new SubClass();
}
I suggest that you create a static object of FreeThread and use that to call the method, as shown above.

Categories