Coder Social home page Coder Social logo

Comments (1)

yongchein-o avatar yongchein-o commented on September 15, 2024 2

Static Binding

Static binding means the compiler chooses the method based on the compile-time type.

class A {
  static void print() {
    System.out.println("This is superclass.");
  }
  static void print(String s) {
    System.out.println("This is superclass." + s);
  }
}

class B extends A {
  static void print() {
    System.out.print("This is subclass.");
  }
  static void print(int x) {
    System.out.println("This is subclass." + x);
  }
}

Let's say you run the above in jshell, this is the output:

jshell> A b = new B();
b ==> B@7cd62f43

jshell> b.print()
This is superclass.

jshell> b.print(1);
|  Error:
|  incompatible types: int cannot be converted to java.lang.String
|  b.print(1);
|          ^

jshell> b.print("Hello")
This is superclass.Hello

jshell> B b = new B()
b ==> B@53b32d7

jshell> b.print()
This is subclass.

jshell> b.print(1)
This is subclass.1

jshell> b.print("Hello");
This is superclass.Hello

As you can see when we have the variable b of type A, b.print() invokes the method in the superclass cos b is determined by the compiler at compile time to be of type A. It will "start" looking for the method in superclass A. I guess this is static binding.

Dynamic binding

Dynamic is just the compiler choosing which method to invoke based on the run time type of the target. I guess it will start looking for a matching method signature in the subclass first then work its way up.

I removed the static modifier:

jshell> b.print()
This is subclass.

Sorry for the various update. I think I was quite confused too. Maybe this might not be the best example. Hopefully prof can clarify.

from 2122-s2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.