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 8003561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:40:47+00:00 2026-06-04T16:40:47+00:00

In this C++ program, #include<iostream> using namespace std; class Base { public: Base() {

  • 0

In this C++ program,

#include<iostream>
using namespace std;

class Base
{

    public:

    Base()
    {
        cout<<"\n\nBase ctr";
        fun();
    }  

    virtual void fun()
    {
        cout<<"\n\nBase's fun()";
    }
};

class Derived : public Base
{
    public:

    Derived()
    {
        cout<<"\n\nDerived ctr";
        fun();
    }

    virtual void fun()
    {
        cout<<"\n\nDerived's fun()";
    }
};
    int main()
    {
        Derived obj;
        return 0;
    }

when calling a virtual function from Base class’s constructor using Derived class object, Base’s class fun() gets called,

output::
Base ctr

Base’s fun()

Derived ctr

Derived’s fun()

reason in explained here
but

in this java program,

class Base
{
    Base()
    {
        System.out.println( "\n\nBase ctr" );
        fun();
    }
    void fun()
    {
        System.out.println( "\n\nBase's fun()" );
    }
}

class Derived extends Base
{
    int a=1;

    Derived()
    {
        System.out.println( "\n\nDerived ctr a = "+a );

        a=8;
        fun();
    }

    void fun()
    {
       System.out.println( "\n\nDerived's fun() a = "+a );
    }

    public static void main(String args[])
    {
       Derived obj = new Derived();
    }
}

output is ::
Base ctr

Derived’s fun() a = 0

Derived ctr a = 1

Derived’s fun() a = 8

here, Derived class fun() is being called, so the constraint applied to c++ program, is not applicable to JAVA program. why?

  • 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-04T16:40:49+00:00Added an answer on June 4, 2026 at 4:40 pm

    The two languages take a different approach to dynamic dispatch. In C++ it will only dispatch to a fully constructed object. That is handled by changing the type of the object during construction as the different levels of the hierarchy constructor start executing. Java on the other hand, considers the object to be of the most derived type before even starting the base-most constructor.

    The problem with the Java approach is that it might actually be executing code on an object that has not yet been constructed. Consider that the derived type had a field initialized to X, and that it is accessed/updated in a method. Calling that method on the base object would access the member before the constructor has initialized it.

    At any rate, you should avoid calling a virtual function in the constructor or destructor of your object in either language.


    Illustrative example:

    public class Base {
       public final int x;
       public Base() { 
          x = foo();
       }
       int foo() { return 1; }
    }
    public class Derived extends Base {
       public final int y;
       public Derived() {
          y = 2;
       }
       int foo() { return y; }
    }
    Derived d = new Derived();
    assert( d.x == d.y );                      // Can this ever fail?
    

    In this code, we have 2 final int, and the code seems simple enough to reason about. Can the assert ever fail?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include <iostream> using namespace std; class Base { public: Base() { cout << In
#include <iostream> using namespace std; class Base { public: Base() { cout << In
Having a program like this: #include <iostream> #include <string> using namespace std; class test
Is there any scope problem in this program? #include<iostream> using namespace std; template<class Type>
#include<iostream> using namespace std; class Abs { public: virtual void hi()=0; }; class B:public
Consider the following program #include <iostream> #include<cstdlib> using namespace std; class E { public:
#include <iostream> using namespace std; class X { public: X() { cout<<Cons<<endl; } X(const
#include <iostream> #include <vector> using namespace std; class base { int x; public: base(int
#include iostream using namespace std; class A { public: void mprint() { cout<<\n TESTING

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.