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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:39:07+00:00 2026-06-09T10:39:07+00:00

If I have: public interface One{ public void method1(); } public interface Two{ public

  • 0

If I have:

public interface One{
    public void method1();
}

public interface Two{
    public void method2();
}

public class AClass implements One, Two{

}

public static void aDiffMethod1(One o){
    o.method1();
}

public static void aDiffMethod2(Two t){
    t.method2();
}

public static void main(String[] args){
     AClass a = new AClass();
     aDiffMethod1(a);
     aDiffMethod2(b);
}

Sometimes in my code I will pass an instance of AClass using the fact it is a subtype of the interface One (in other words, the parameter type of the method will be One and I will pass the type AClass) and at other times I may pass the AClass object knowing its a subtype of the interface Two (the parameter type of the method im passing to is Two, so I pass AClass as a subtype of Two).

Is it ok to pass an instance of AClass into different methods under the guise of being a subtype of different interfaces?

Is this what they mean by programming to the interface- so it is fine?

  • 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-09T10:39:08+00:00Added an answer on June 9, 2026 at 10:39 am

    Yes, that is the whole point of interfaces.

    Methods that only accept interface One will only ever call method1, so this is safe. methods that only accept interface Two will only ever call method2, so again, this is safe.

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

Sidebar

Related Questions

I have two files: public interface PrintService { void print(PrintDetails details); class PrintDetails {
I have interface Foo public interface Foo { public void test(); } Class FooChild
So I have one main file, and two classes: A display Class, and a
I have one main interface and an abstract class implementing all derivable methods (that
I have this interface: public interface IValidationCRUD { public ICRUDValidation IsValid(object obj); private void
I have an interface: public interface IMyInterface { } I have a class which
I have a public facing interface that I'm trying to map two different enumerations
I have a interface as follows public interface IX { void MethodA(); void MethodB();
I'm having a brain cramp: I have a public interface SomeInterface and a static
I have an abstract base class which acts as an interface. I have two

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.