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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:43:23+00:00 2026-05-23T21:43:23+00:00

The output of this simple program is This is base . public class mainApp{

  • 0

The output of this simple program is This is base.

public class mainApp{
    private void func(){
      System.out.println("This is base"); 
    }

    public static void main(String[] args){
        mainApp newObj = new derived();
        newObj.func();
    }
}

class derived extends mainApp{
    public void func(){ 
      System.out.println("This is derived"); 
    }
}
  • My question is when we are using this line mainApp newObj = new derived(); are we not actually creating an object of derived class using a reference of base class mainApp. So, when I am using the object to call it’s method why don’t I get the method from the derived class? Why I get the method from the base class.

  • using this line, mainApp newObj = new derived();, are we working with a reference of mainApp OR we are working with an object of derived class. Which one is correct?

  • 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-05-23T21:43:24+00:00Added an answer on May 23, 2026 at 9:43 pm

    The reason that you’re getting the base class method is that the base class version of func is declared private, and Java does not allow private methods to be overridden by subclasses. This means that if you extend a base class and by pure coincidence decide to name a private member function the same name as a private member function in the base class, you don’t accidentally change the behavior of the base class’s member functions. You are indeed working with an object of type derived, but because the reference is statically typed as a mainApp, calling func is interpreted as calling the private method func in mainApp rather than the public method func in derived. Changing the code to read

    derived d = new derived();
    d.func();
    

    fixes this, because the static type of d is now derived and the call to func has a different meaning.

    At the JVM bytecode level, the instruction used to call private member functions is invokespecial whereas the instruction used to call normal, override-able member functions is invokevirtual. The two have totally different semantics; invokespecial begins looking in the current class (or some base class for things like constructors), whereas invokevirtual looks in the class corresponding to the object’s type at runtime.

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

Sidebar

Related Questions

This is a simple program i wrote using for loop public class Test {
I wrote this simple program: void sig_ha(int signum) { cout<<received SIGINT\n; } int main()
I'm trying to write a simple high-low program that will output like this: $
I wrote this simple class that provides buffering for general output operation: template <typename
Consider the following sample code: class Base { public: void f(); virtual void vf();
I have this output when trying to debug Program received signal SIGSEGV, Segmentation fault
Can anyone explain the output of this program and how I can fix it?
I wrote this simple C program: int main() { int i; int count =
Inspired by this topic , I decided to write a simple program that does
I have written this simple C program that changes the rotation file name. Eg:

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.