Table of Contents
Question content
Solution
Home Java Java best practices for calling overloaded methods of parent and child classes

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

Feb 09, 2024 am 11:30 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)