Developer community
0 votes
91 views
asked by Sr Member (4.8k points) 10 23 31 | 91 views

1 Answer

0 votes
class A {
  void callme() {
    System.out.println("Inside A's callme method");
  }
}

class B extends A {
  void callme() {
    System.out.println("Inside B's callme method");
  }
}

class C extends A {
  void callme() {
    System.out.println("Inside C's callme method");
  }
}

class Dispatch {
  public static void main(String args[]) {
    A a = new A(); // object of type A
    B b = new B(); // object of type B
    C c = new C(); // object of type C
    A r; // obtain a reference of type A

    r = a; // r refers to an A object
    r.callme(); // calls A's version of callme

    r = b; // r refers to a B object
    r.callme(); // calls B's version of callme

    r = c; // r refers to a C object
    r.callme(); // calls C's version of callme
  }
}
answered by Geek (15.3k points) 15 40 60

Related questions

0 votes
1 answer
asked by john Guru (27.1k points) 31 48 96 | 36 views
0 votes
1 answer
asked by john Guru (27.1k points) 31 48 96 | 21 views
0 votes
1 answer
asked by smith Geek (15.3k points) 15 40 60 | 86 views
0 votes
1 answer
asked by smith Geek (15.3k points) 15 40 60 | 86 views
0 votes
1 answer
asked by augustaina Sr Member (4.8k points) 10 23 31 | 88 views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users