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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:24:09+00:00 2026-06-12T07:24:09+00:00

I am practicing inheritance. I have two similar classes that I’d like to assimilate

  • 0

I am practicing inheritance.

I have two similar classes that I’d like to assimilate into one array, so I thought to use the Object class as a superclass since everything is a sublcass of Object.

So, for example I put T class and CT class into an array called all like so:

 Object all[] = new Object[6];

    all[0] = T1;

    all[1] = CT2;

    all[2] =T3;

    all[3] = CT1;

    all[4] = T2;

    all[5] = CT3;

I skipped the declarations as thats not my problem.

My real issue becomes when I wish to call a function within the array utilizing a loop:

for (int i = 0; i < 6; i++) {

    all[i].beingShot(randomNum, randomNum, AK47.getAccuracy());
}

The classes involved with T and CT respectively both have the beingShot method, which is public.

Eclipse advises casting them as a quick fix. I’m wondering if there is any logical alternative other than creating my own Object class that holds the beingShot method, or adding this to the class of Object, although I feel either of these choices would cause more problems in the long run.

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-12T07:24:11+00:00Added an answer on June 12, 2026 at 7:24 am

    If both classes implement the same method(s), you should consider creating an interface.

    Interfaces are very powerful and easy to use.

    You could call your interface Shootable.

    You can create an array of different objects that implement Shootable and treat them all the same.

    // Define a VERY simple interface with one method.
    
    interface Shootable {
        public void beingShot();
    }
    
    // Any class that implements this interface can be treated interchangeably
    
    class Revolver implements Shootable {
        public void beingShot() {
            System.out.println("Revolver: firing 1 round");
    }
    
    class MachineGun implements Shootable {
        public void beingShot() {
            System.out.println("Machine Gun: firing 50 rounds");
        }
    }
    
    class HockeyPuck implements Shootable {
        public void beingShot() {
            System.out.println("Hockey Puck: 80 MPH slapshot");
        }
    }
    
    class RayBourquePuck implements Shootable {
        public void beingShot() {
            System.out.println("Hockey Puck: 110 MPH slapshot");
        }
    }
    
    class OunceOfWhiskey implements Shootable {
        public void beingShot() {
            System.out.println("Whiskey Shot: 1 oz down the hatch...");
        }
    }
    
    // You can declare an array of objects that implement Shootable
    
    Shootable[] shooters = new Shootable[4];
    
    // You can store any Shootable object in your array:
    
    shooters[0] = new MachineGun();
    shooters[1] = new Revolver();
    shooters[2] = new HockeyPuck();
    shooters[3] = new OunceOfWhiskey();
    
    // A Shootable object can reference any item from the array
    
    Shootable anyShootableItem;
    
    // The same object can to refer to a MachineGun OR a HockeyPuck
    
    anyShootableItem = shooters[0];
    anyShootableItem.beingShot();
    
    anyShootableItem = shooters[2];
    anyShootableItem.beingShot();
    
    // You can call beingShot on any item from the array without casting
    
    shooters[0].beingShot();
    shooters[1].beingShot();
    
    // Let's shoot each object for fun:
    
    for (Shootable s : shooters) {
        s.beingShot();
    }
    

    Here’s a great related question and answer.

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

Sidebar

Related Questions

While practicing a tutorial on GTK+ I have encountered sample code that looks like
i've been trying to study for my finals by practicing classes and inheritance, this
I am practicing using pointers. I have a pointer to an array of 3
I'm practicing my Rails development skills by building an app that will have different
Im practicing with one way hash encryption. I have a MVC model with Entity
I'm practicing Haskell, and writing a summation function that takes in two numbers (upper
So I am practicing using a databases with ASP.Net... I have two datatable columns
Started practicing with XML and C# and I have an error message of There
I have been practicing TDD and (some) XP for a few years now and
Whilst practicing some TDD at work for an ASP.Net MVC project, I ran into

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.