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

  • Home
  • SEARCH
  • 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 3307336
In Process

The Archive Base Latest Questions

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

Continuing from this question: Why can't you reduce the visibility of a method in

  • 0

Continuing from this question: Why can't you reduce the visibility of a method in a Java subclass?

I need to create class B that is almost identical to class A, except that B cannot do certain things that A can.

Being a lazy programmer as I am, I tried to inherit A, only to greet with error that B cannot reduce the visibility of A methods. Duh!..

Now A is an API from a vendor, my intention is to encapsulate this API so that it is easier to use.

I wonder what is the best practice to work around this?

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

    Two options:

    If you need B to keep the same interface as A (so that client code can use any of the two without changes), you can override “forbidden” methods in B and have them throw an UnsupportedOperationException. For example:

    public class A
    {
        public int allowedMethod() { ... }
        public int forbiddenMethod() { ... }
    }
    
    public class B extends A
    {
        public int forbiddenMethod()
        {
            throw new UnsupportedOperationException("Sorry, not allowed.");
        }
    }
    

    Or, if you really want the API of B to be a subset of the API of A, then just have B contain an instance of A, and delegate method calls appropriately.

        public class A
        {
            public int allowedMethod() { ... }
            public int forbiddenMethod() { ... }
        }
    
        public class B
        {
            private A a;
    
            public int allowedMethod()
            {
                return a.allowedMethod();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Continuing from my previous question , is there a comprehensive document that lists all
Continuing from this question , i am confused whether DISPID_VALUE on IDispatch::Invoke() for script
Continuing from this question With each system call, the function constructs a set of
Following on from this question I asked yesterday: Can I shorten this regular expression?
Continuing from my last question , I'd like to know how can I bind
from this question , I drilled down the problem to a listbox, that doesn't
Continuing my problem from yesterday, the Silverlight datagrid I have from this issue is
This is continuing questions from Selecting the highest salary Assuming a table ' wagetable
I have a solution containing several projects that have migrated from VS 2003, 2005,
I have seen this question asked in a couple of different ways on SO

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.