search
HomeJavaConfused about polymorphism and static binding in Java

php editor Xigua often receives questions about polymorphism and static binding in Java. These two concepts are often mentioned in Java but can be confusing for beginners. In this article, we will briefly introduce the concepts of polymorphism and static binding and answer some frequently asked questions to help readers better understand and apply these concepts.

Question content

I am still very new to java. So I was playing around with java while reading about polymorphism and static binding. I'm here to clarify if my thought process is correct.

class a {
    void foo(a a) {
        system.out.println("aaaaaa");
    }

}

class b extends a {
    void foo(b a) {
        system.out.println("bbbbb");
    }

}

class c extends b{
    void foo (a a){
        system.out.println("cccccbbbb");
    }

}

I created the following object named c and called foo with c as argument.

C c = new C();
c.foo(c); // the output is BBBBB

From this question about java overloading and dynamic binding, I understand that if the parameter sent is not found in the class, it will up-cast the parameter (c in this case) to the one in the class The argument can be found (in this case a, because void foo (a a)). But if that's the case, shouldn't it print "cccccbbbb"? Via static binding?

Solution

Class c has 2 overloaded methods named foo

// defined in the class C
void foo (A a){
    System.out.println("CCCCCBBBB");
}

// inherited from the class B
void foo(B a) {
    System.out.println("BBBBB");
}

When we call a method foo with arguments of class c, the most specific one will be chosen - class b which is hierarchically smaller than class a is closer, so foo(b) is called.

The above is the detailed content of Confused about polymorphism and static binding in Java. For more information, please follow other related articles on the PHP Chinese website!

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

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool