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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:12:23+00:00 2026-06-08T09:12:23+00:00

Java – I have a abstract base class and I want to set few

  • 0

Java – I have a abstract base class and I want to set few properties of base class from sub class. What is the best way to do it ? Make the properties protected ? Create setters in the base class ?

public abstract class A {
protected String tag;
protected String apiUrl;
    // Setter
    public void setApiUri(String url) {
        this.apiUrl = url;
    }
}

public class B extends A {
    tag = "Class B";
    apiUrl = "Class B Prefs";
}

public class C extends A {
    tag = "Class C";
    apiUrl = "Class C Prefs";
}

Please let me know what is the best approach / any better way of doing it.

  • 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-08T09:12:25+00:00Added an answer on June 8, 2026 at 9:12 am

    If you want maximum encapsulation, make the base class fields private and provide protected accessors. This is usually the best practice.

    If you’re not too bothered about maximum encapsulation, you can make the fields protected, but it couples the classes together even more tightly than inheritance does (er) inherently.

    In general, your best bet is the first option above. The JVM’s JIT can optimize out the calls, and it helps keep the clases somewhat loosely-coupled.

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

Sidebar

Related Questions

Java. I have a very small int array (4 elements). What's the fastest way
java.lang.NoClassDefFoundError: javax/servlet/jsp/el/ELException from using <c:set var=testVar value=testVal /> All other JSTL tags work. jsp
Java DataInputStream class method readFully is used for reading the bytes from the stream
Java doesn't support multiple inheritance for abstract classes, but I'm trying to do the
java.math.BigDecimal.toString() can return something like 0.888200000. What can be the best I can do
Java design question. I have an object that needs to maintain sets of say
Java Concurrency in Practice gives the following example of an unsafe class which due
Java Code: import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExpTest { public static void main(String[]
java.util.Calendar.clone() returns ...a new Calendar with the same properties and returns a shallow copy
Java is properly installed in my Ubuntu 12.04 and the PATH variable is set

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.