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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:39:06+00:00 2026-05-11T07:39:06+00:00

I am trying to wrap my mind around something in java. When I pass

  • 0

I am trying to wrap my mind around something in java. When I pass an object to another class’ method, can I not just call any methods inherent to that object class?

What is the reason code such as the example below does not compile?

Thank you,

class a {   public static void myMethod(Object myObj) {     myObj.testing();   } }   class b {   public void testing() {     System.out.println ('TESTING!!!');   } }   class c {     public static void main (String[] args) {     b myB = new b();         a.myMethod(myB);     } } 

Edit: The reason I have left the parameter in myMethod as type Object, is because I would like to be able to pass in a variety of object types, each having a testing() method.

  • 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. 2026-05-11T07:39:07+00:00Added an answer on May 11, 2026 at 7:39 am

    If you would like to pass in a variety of objects with testing() methods, have each object implement a Testable interface:

    public interface Testable {    public void testing() } 

    Then have myMethod() take a Testable.

    public static void myMethod(Testable testable) {   testable.testing(); } 

    Edit: To clarify, implementing an interface means that the class is guaranteed to have the method, but the method can do whatever it wants. So I could have two classes whose testing() methods do different things.

    public class AClass implements Testable {    public void testing()    {       System.out.println('Hello world');    } }  public class BClass implements Testable {    public void testing()    {       System.out.println('Hello underworld');    } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 153k
  • Answers 153k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think the line "Input string was not in a… May 12, 2026 at 10:21 am
  • Editorial Team
    Editorial Team added an answer GAC both versions of SQLite and ELMAH will work just… May 12, 2026 at 10:21 am
  • Editorial Team
    Editorial Team added an answer Your call to $.ajax() sets off the request and then… May 12, 2026 at 10:21 am

Related Questions

I am writing a client windows app which will allow files and respective metadata
I am trying to wrap my head around doing validation in a MVC scenario.
I am trying to wrap my head around the control infrastructure to understand which
I am trying to wrap my head around the idea of classes, data visibility

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.