Home  >  Article  >  Java best practices for calling overloaded methods of parent and child classes

Java best practices for calling overloaded methods of parent and child classes

王林
王林forward
2024-02-09 11:30:09565browse

php editor Zimo brings you the best practice in Java programming-calling parent class and subclass overloaded methods. In Java, inheritance is an important object-oriented programming concept that allows subclasses to inherit the properties and methods of parent classes. Method overloading occurs when both the parent class and the subclass define methods with the same name. In this case, we need to pay attention to how to correctly call the overloaded methods of the parent class and subclass to ensure the correctness and maintainability of the program. This article will give you a detailed introduction to the best practices for calling overloaded methods of parent classes and subclasses to help you become more comfortable in Java programming.

Question content

What is the best way to call a method that is overloaded by both parent and subclasses? For example. Say I have a parent and child class

private class parent{
        public parent() {
        }
    }
    
    private class child extends parent{
        string name;

        public child(string name) {
            this.name = name;
        }
    }

I want to create a method that overloads both

private void methodcall(parent parent){
        system.out.println("parent class");
    }

    private void methodcall(child child){
        system.out.println(child.name);
    }

I created a method to create a parent or child and call the methodcall method

private void callMethod(String name){
        Parent parent;
        if(name != null) {
            parent = new Child(name);
        } else {
            parent = new Parent();
        }
        methodCall(parent);
    }

This seems to always call the parent method, how can I call the child method without explicitly casting the object to the child object?

Solution

In my opinion, you should create a method called methodcall() in the superclass parent and respectively in child Rewrite it as follows:

protected void methodcall(){
    system.out.println("parent class");
}

In child class

@override
protected void methodcall(){
    system.out.println(this.name);
}

Then you call like thismethodcall

The signature of a
private void callmethod(string name){
    parent parent;
    if(name != null) {
        parent = new child(name);
    } else {
        parent = new parent();
    }
    parent.methodcall();
}

method is determined by its name and parameter list. This means methodcall(parent) and methodcall(client) are different.

What you want is not overload, but overwhelming. You know what I mean when you add the annotation @override to a method in child in your code - the compiler will complain...

If you leave the definition of the class unchanged, you will have to cast to child in order to call the corresponding method - although this won't work at all, since methodcall() is non-static ( Or you have callmethod() as a member of child.

Or you change your code like this:

class parent
{
  public parent() {}

  public <t extends parent> void methodcall( final t arg )
  {
    out.println( getclass().getname() );
  } 
}
    
class child extends parent
{
  string name;

  public child( string name ) 
  {
    this.name = name;
  }

  @override
  public <t extends parent> void methodcall( final t arg )
  {
    out.println( name );
  }
}

Use it like this:

public static void callMethod( final String name )
{
  Parent parent;
  if( name != null ) 
  {
    parent = new Child( name );
  } 
  else 
  {
    parent = new Parent();
  }
  parent.methodCall(parent);
}

The above is the detailed content of Java best practices for calling overloaded methods of parent and child classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete