Home > Java > body text

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

王林
Release: 2024-02-09 11:30:09
forward
564 people have browsed it

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;
        }
    }
Copy after login

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);
    }
Copy after login

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);
    }
Copy after login

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");
}
Copy after login

In child class

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

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();
}
Copy after login

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 );
  }
}
Copy after login

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);
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template