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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:54:01+00:00 2026-05-18T01:54:01+00:00

This is a very simple question, and I apologize for being so noobish :/

  • 0

This is a very simple question, and I apologize for being so noobish :/

I’m new(ish) to Java. I’ve got a game on the Market that works quite well, but the code is all rubbish. Basically, I wrote it like I was writing JavaScript. I’m trying to learn more about Object Oriented Programming. So, in my new game I’ve been creating Objects that extend from a custom class. For example (this isn’t actually what I’m doing):

public class Color{
    public final int STROKE_WIDTH = 3;

    public Color(){}
}

public class Orange extends Color{
    public int alpha = 30;

    public Orange(){
        super();
    }
}

Now, say in my game’s main thread I instantiate many Colors (Orange, Red, Purple) and I store them in an ArrayList<Color>. I iterate through this list at another point in the game, and I want to access the Color‘s alpha. My problem is, since they’re all wrapped into the superclass, I can’t just say colorObj.alpha, but I do have access to colorObj.STROKE_WIDTH. Similarly, I wouldn’t be able to access any methods in the Orange class.

There must be a way to do this, I’m just new. I don’t expect anyone to sit here and type out an explanation.. I wouldn’t want to waste your time. But if someone could just paste a link to some beginner’s tutorial or something that would help me out with this, I’d appreciate 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-05-18T01:54:01+00:00Added an answer on May 18, 2026 at 1:54 am

    I iterate through this list at another point in the game, and I want to access the Color’s alpha. My problem is, since they’re all wrapped into the superclass, I can’t just say colorObj.alpha

    If all your actual colors have alpha, then put that in the Color class. It should contain the attributes and behaviors that are common to all colors.

    Side note: one of the tentants of object oriented programming is enscapsulation. Public data members expose the internal representation of your class. Typically you hide that representation behind accessor methods, such as:

    class Color {
       private int alpha;
       public int getAlpha() { return alpha; }
    }
    

    Now nobody knows your representation; nobody can change alpha to a value that doesn’t make sense for a Color, you could make it read-only or write-only, you could even change it to a computed value and none of the clients who use it are affected.

    In fact, a “best practice” in OOP is taking this to extreme and programming to interfaces, not classes. You would define an interface for Colors which has no data members and in fact cannot be instantiated. It represents the contract that Colors will obey and nothing more. For instance:

    interface Color {
       int getR();
       int getG();
       int getB();
       int getA();
    }
    
    class Red implements Color {
       ...
    

    Of course, this is overkill for something like a Color. I know it’s a contrived example, but Colors don’t have different behavior, just different values, so rather than having class Red, Orange, etc. you would have instances of a Color class which represent each.

    Similarly, I wouldn’t be able to access any methods in the Orange class.

    True. But the idea is that the code which is dealing with Colors doesn’t need to know what actual colors they are. That’s the power of polymorphism. If there is some special-case processing required, you can “down cast” a Color to a specific subtype, but the need to do that is often a symptom of sloppy design.

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

Sidebar

Related Questions

This is probably a very simple question, I apologise - I'm new to rails.
I am very new to programming, so I apologize if this question seems absurdly
I'm very new to node.js so I apologize if this is a poor question.
I apologies for this being a very simple question but as a first time
I'm new to SSRS, so I apologize if this question is too simple: I
This is a very simple question for compiler guys, but I always get confused
This is a very simple question, but I can't seem to find something about
This is a very simple question and I feel stupid for asking it, but
This may be very simple question,But please help me. i wanted to know what
This should be very simple question. There are many programming languages out there, compiled

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.