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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:09:31+00:00 2026-05-29T08:09:31+00:00

We’re having a discussion with a friend concerning java code design and efficiency. He

  • 0

We’re having a discussion with a friend concerning java code design and efficiency.

He argues methods should preferably be private for performance reasons as well as class consistency protection when overriding.

I argue methods should preferably be protected for complete customizability, and to avoid loosing time modifying and releasing an API as soon as its user want to change part of its behaviour.

We know the preference for composition over inheritance so here I mainly focus on performance comparison.

A simple test (see below) proves extended classes with parent having protected methods aren’t slower than those with parent having private methods. It’s even sometimes (I don’t really understand performance change) faster.

elapsed:8051733.063 microseconds for A (private)
elapsed:8036953.805 microseconds for B (protected)

Do you think the below mentionned test is robust enough for making the comparison?

public class VerifPerfProtected {
public static void main(String[] args) {
    int ncalls = 1000000000; //10^9
    ChildrenClassA a = new ChildrenClassA();
    ChildrenClassB b = new ChildrenClassB();

    long start = System.nanoTime();
    a.manyCalls(ncalls);
    long stop = System.nanoTime();
    System.out.println("elapsed:" + (stop - start)/1000.0 + " microseconds for A (private)");

    start = System.nanoTime();
    b.manyCalls(ncalls);
    stop = System.nanoTime();
    System.out.println("elapsed:" + (stop - start)/1000.0 + " microseconds for B (protected)");
}

public static class ParentClassA{
    public void manyCalls(int n){
        for (int i = 0; i < n; i++) {
            callAmethod();
        }
    }
    public void callAmethod(){
        aPrivateMethod();
    }
    private void aPrivateMethod(){
        int a=0;
        for (int i = 0; i < 5; i++) {
            a++;
        }
    }
}

public static class ParentClassB{
    public void manyCalls(int n){
        for (int i = 0; i < n; i++) {
            callAmethod();
        }
    }
    public void callAmethod(){
        aProtectedMethod();
    }
    protected void aProtectedMethod(){
        int a=0;
        for (int i = 0; i < 5; i++) {
            a++;
        }
    }
}

public static class ChildrenClassA extends ParentClassA{        
}

public static class ChildrenClassB extends ParentClassB{        
}
}
  • 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-29T08:09:32+00:00Added an answer on May 29, 2026 at 8:09 am

    It is best not to confuse good design with performance requirements.

    You should start with what is good design, and where you can’t agree, you have to both accept it probably doesn’t matter. Trying to use a performance comparison to find some measured way of choosing the difference is almost always a mistake because you don’t know if the performance difference will matter anyway.

    I suggest you allow for each developer to follow their own style unless there is a very good reason which can be agreed, not to do something.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have thousands of HTML files to process using Groovy/Java and I need to
I have this code to decode numeric html entities to the UTF8 equivalent character.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.