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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:44:11+00:00 2026-06-09T14:44:11+00:00

This is a code sample of inheritance while the code functions fine.I am unable

  • 0

This is a code sample of inheritance while the code functions fine.I am unable to understand the output of this code kindly explain it to me.

class R {
    private void S1() {
        System.out.println("R:S1");
    }

    protected void S2() {
        System.out.println("R:S2");
    }

    protected void S1S2() {
        S1();
        S2();
    }
}

class S extends R {
    private void S1() {
        System.out.println("S:S1");
    }

    protected void S2() {
        System.out.println("S:S2");
    }
}

public class Inheritance {
    public static void main(String srgs[]) {
        new S().S1S2();
    }
}

The output is:

R:S1
S:S2

Why is the first call made to,R class’ S1 while second to S class’ S2.

  • 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-09T14:44:13+00:00Added an answer on June 9, 2026 at 2:44 pm

    R.S1 is private, so it’s not called polymorphically, and S.S1 doesn’t override it.

    R.S2 is protected, so S.S2 overrides it, and when you call S2 on an instance of S2 (even if it’s only statically known to be an instance of R), S.S2 will be called.

    From section 8.4.8.1 of the JLS:

    An instance method m1, declared in class C, overrides another instance method m2, declared in class A iff all of the following are true:

    • C is a subclass of A.

    • The signature of m1 is a subsignature (§8.4.2) of the signature of m2.

    • Either:

      • m2 is public, protected, or declared with default access in the same package as C, or

      • m1 overrides a method m3 (m3 distinct from m1, m3 distinct from m2), such that m3 overrides m2.

    Note how m2 can’t be private.

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

Sidebar

Related Questions

Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
I have this sample code for async operations (copied from the interwebs) public class
I'm using this simple code: http://ejohn.org/blog/simple-javascript-inheritance/ Using this library, I made this simple class:
I'm reading this code sample : And since I don't know C#, I decided
For one or another reason this code sample gives 48 hours instead of 24,
In this particular code sample I want to reference the second overloaded method (int
I am new to scheme. This is code sample from SICP course of MIT.
I'm trying to learn the Enterprise Library. I found this useful code sample to
In this sample code the URL of the app seems to be determined by
This is a code sample taken from the Google Maps API developer's guide. I'm

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.