Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8814621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:10:26+00:00 2026-06-14T04:10:26+00:00

Let’s say Java has these hierarchical classes: class A { } class B extends

  • 0

Let’s say Java has these hierarchical classes:

class A 
{
}
class B extends A
{
    public void m()
    {
        System.out.println("B\n");
    }
}
class C extends B
{
    public void m()
    {
        System.out.println("C\n");
    }
}
class D extends C
{
    public static void main(String[] args)
    {
        A a = new D();
        // a.m(); // doesn't work
        B b = new D();
        b.m();
        C c = new D();
        c.m();
        D d = new D();
        d.m();
    }
}

This is the (blind) duplication of the same code in C#:

using System;
class A 
{   
}
class B : A
{
    public void M()
    {
        Console.WriteLine("B");
    }
}
class C : B
{
    public void M() // I need to use public new void M() to avoid the warning
    {
        Console.WriteLine("C");
    }
}
class D : C
{
    public static void Main(String[] args)
    {
        A a = new D();
        // a.M(); // doesn't work       
        B b = new D();
        b.M();
        C c = new D();
        c.M();
        D d = new D();
        d.M();
    }
}

When I execute the Java code, I got C-C-C whereas C# returns B-C-C.

To me C#’s result makes more sense, as reference B invokes its own method.

  • What’s the logic behind the Java designer’s decision to print C-C-C instead of B-C-C? I mean, why reference B uses the overriding method in C? What’s the advantage of this approach?
  • How can I change Java code to print out B-C-C just like C# does? I mean, how can I teach java to invoke the method of the exact reference it uses?
  • How can I change C# code to print out C-C-C? I mean, how can I teach C# to invoke the overriding method?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:10:28+00:00Added an answer on June 14, 2026 at 4:10 am

    It’s for the virtual function definition:

    a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature. This concept is a very important part of the polymorphism portion of object-oriented programming (OOP).

    In C#,you should declare the method as virtual in order to be overriden, as shown in MSDN:

    • C# Lang Spec. Virtual keyword
    • C# Virtual

    Since the M method is not virtual, it will execute b.M() even if b variable is actually a D instance.

    In Java, every non-static method is virtual by default, so you when you override a method (even without the @Override annotation) the behavior of the b.M() will be the d.M() that inherits the c.M() method behavior.

    How can I change Java code to print out B-C-C just like C# does? I mean, how can I teach java to invoke the method of the exact reference it uses?

    You simply can’t do this in Java. The M method in C class would override the M method in B. Adding the final modifier to B#M will just make that C or other B children can’t override the M() method.

    How can I change C# code to print out C-C-C? I mean, how can I teach C# to invoke the overriding method?

    Change the M method in B class to virtual and override it in C class:

    class B : A
    {
        public virtual void M()
        {
            Console.WriteLine("B");
        }
    }
    class C : B
    {
        public override void M() // I need to use public new void M() to avoid the warning
        {
            Console.WriteLine("C");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let's say I have the following structure: abstract class Hand {} class Rock extends
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let's say I have two text files that I need to extract data out
Let's say I have a custom class CustomClass, and I have a collection deriving
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say I have 2 functions: void function1(int *ptr) { printf(%d, *ptr); } and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.