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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:54:23+00:00 2026-06-11T10:54:23+00:00

package P1; public class Base { private void pri( ) { System.out.println(Base.pri()); } void

  • 0
package P1;

public class Base {

   private void pri( ) { System.out.println("Base.pri()");  }

   void pac( ) {  System.out.println("Base.pac()");  }

   protected void pro( ) { System.out.println("Base.pro()"); }

   public void pub( ) { System.out.println("Base.pub()"); }

   public final void show( ) {
       pri();  
       pac();  
       pro();  
       pub(); 
   }    
} 

and

package P2;

import P1.Base;

public class Concrete1 extends Base {
   public void pri( ) { System.out.println("Concrete1.pri()"); }
   public void pac( ) { System.out.println("Concrete1.pac()"); }
   public void pro( ) { System.out.println("Concrete1.pro()"); }
   public void pub( ) { System.out.println("Concrete1.pub()"); }
}

And I’m executing

Concrete1 c1 = new Concrete1();
c1.show( );

Now, the output is shown to be

Base.pri()
Base.pac()
Concrete1.pro()
Concrete1.pub()

Can someone explain why this is so? From what I understood about inheritance, this should have happened:

1) P2.concrete1 inherits P1.Base
2) c1 object of concrete1 is created
3) c1.show() is called. Since P1.Base.show() is public, it can be called.
4) Now, in P2.concrete1 after inheritance, only it’s own methods (pri, pac, pro, pub) and P1.Base’s inheritable methods (pro, pub, show) are accessible.

Now WHY does it show Base.pri() and Base.pac() in the output when they’re not even accessible?

It’s clear that I’ve not got a very clear fundamental understanding of inheritance. Can someone explain this situation and how inheritance is actually ‘structured’. I used to think that inheritable methods and fields of the superclass would just superimpose onto the subclass. But that line of reasoning is obviously wrong.

Thanks!

  • 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-11T10:54:24+00:00Added an answer on June 11, 2026 at 10:54 am

    The short answer is that you can only override methods that are visible. The first two methods, pri and pac are private and package protected respectively. Because nothing outside the class can see a private method, it can’t be overridden. Similarly because Concrete1 is in a different package from Base it can’t see Base.pac so cannot override it.

    What this means is that while you define a pri and pac method in Concrete1, they’re just methods that happen to have the same name as the methods in Base, not overrides. The other two methods pro and pub are protected and public respectively, so are visible to Concrete1. As a result the pro and pub methods in Concrete1 are overrides of the methods of the same name in Base.

    Because show is defined in Base, it’s compiled calling the 4 methods as defined in Base. When executed the JVM looks to see if any are overridden and if they are executes the overridden methods. As explained above, pri and pac are not overridden so the Base versions are executed where as pro and pub are so the Concrete1 versions are executed.

    If you were to move the show method into Concrete1 instead of Base then it would execute the 4 methods as defined in Concrete1 as those would be the methods visible to show.

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

Sidebar

Related Questions

MyClass.java: package test; public class MyClass { public void myMethod(){ System.out.println(My Method Called); }
Say there's the following base class: package bg.svetlin.ui.controls; public abstract class Control { protected
package pack; public class sample{ public static void main(String input[]) { NumberFormat numberFormat =
package org.study.algos; public class Study { public static void main(String[] args) { A a
package GC; import java.util.Scanner; public class main { public static void main(String args[]) {
package { import mx.controls.LinkButton; import flash.text.TextLineMetrics; public class multiLineLinkButton extends LinkButton { override protected
I have a generic base class: public class BaseLabel<T> extends JXLabel { private static
I have the following SwingMenu class. package base; import javax.swing.*; public class SwingMenu {
A.as package { public class A { public static var someObject:Object = { (B.SOME_CONST):
Suppose I have following code package memoryleak; public class MemoryLeak { public static int

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.