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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:41:45+00:00 2026-06-15T16:41:45+00:00

I am trying to understand interfaces in Java and have this task to do

  • 0

I am trying to understand interfaces in Java and have this task to do which I am a stack with. It must be something easy, but I don’t seem to see the solution. Interface contains a few methods, one of them should return true if all elements of this set are also in the set. I.e.

public interface ISet{
//some methods
   boolean isSubsetOf(ISet x);
}

Then the class:

public class myClass implements ISet{
       ArrayList<Integer> mySet;
       public myClass{
          mySet = new ArrayList<Integer>();
        }
       //some methods
       public boolean isSubsetOf(ISet x){
          //method body
    }
 }

What do I need to write in method body? How do I check that the instance of myClass is a subset of ISet collection? I was trying to cast, but it gives an error:

ArrayList<Integer> param = (ArrayList<Integer>)x;
    return param.containsAll(mySet);
  • 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-15T16:41:47+00:00Added an answer on June 15, 2026 at 4:41 pm

    An interface is like an idea of a class.

    It contains things like method definitions and static and final variables.

    Therefore any class that implements them will contain a body (i.e. a way to handle that method in this class).

    For eg.

    public interface ISet{
        boolean isSubsetOf(ISet x);
    }
    

    Now any class that implements this will “handle” the method isSubsetOf and return a boolean. How they do this is not the interfaces business.
    Now in your class

    public class myClass implements ISet{
       ArrayList<Integer> mySet;
       public myClass(){
          mySet = new ArrayList<Integer>();
        }
    
       public boolean isSubsetOf(ISet x){
          //you now have to get what type of object x is
          // it is obviously implementing ISet therefore it most probably be myClass.
          //So when you typecast the object it should typecast to myClass.
          ArrayList<Integer> param = (ArrayList<Integer>)( ((myClass)x).mySet );
          return param.containsAll(mySet)
        }
    }
    

    This will work as we typecast x.mySet to ArrayList and x itself to myClass.

    However there is a flaw in the logic. If you have another class implementing ISet then they might not typecast to myClass but in this case with on implements you will be fine.

    It also seems like you are confused how to run this method so I’ll add that

    myClass A=new myClass();
    A.add(1);    A.add(2);    A.add(3);
    myClass B=new myClass();
    B.add(3);    B.add(1);    B.add(2);   B.add(4);
    A.isSubsetOf(B);
    //This will answer true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to understand java behaviour. Using this interfaces : public interface IA {}
Trying to understand the math of this code snippet. A token is provided which
so i was trying to understand the interfaces , but i almost only see
I am trying to understand Java interfaces like the millions of others around the
I am trying to understand something about ArrayObject and ArrayIterator classes ArrayObject : This
I am trying understand ViewModels deeper and I have read many articles and blogs
Trying to understand PNG format. Consider this PNG Image: The Image is taken from
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand something. I created a d:\svn\repository on my server. I committed folders

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.