Home> Java> body text

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

王林
Release: 2024-02-09 11:30:09
forward
497 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 superclassparentand respectively inchildRewrite it as follows:

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

Inchildclass

@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 meansmethodcall(parent)andmethodcall(client)are different.

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

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

Or you change your code like this:

class parent { public parent() {} public  void methodcall( final t arg ) { out.println( getclass().getname() ); } } class child extends parent { string name; public child( string name ) { this.name = name; } @override public  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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!