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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:43:23+00:00 2026-05-24T11:43:23+00:00

I have the following example: package cage; import java.util.HashSet; import java.util.Set; import animals.Animal; public

  • 0

I have the following example:

package cage;

import java.util.HashSet;
import java.util.Set;

import animals.Animal;

public class Cage<T extends Animal> {  //A cage for some types of animals
    private Set<T> set = new HashSet<T>();

    public void add(T animal)  {
        set.add(animal);
    }

    public void showAnimals()  {
        for (T animal : set) {
            System.out.println(animal.getName());
        }
    }

    public void transferTo(Cage<? super T> cage) {
        cage.set.addAll(this.set);
    }
}

Main class:

package exe;

import cage.Cage;
import animals.Animal;
import animals.Ape;
import animals.Lion;
import animals.Rat;

public class Main {

    public static void main(String[] args) {
        System.out.println("Test with super........");
        Cage<Animal> animals = new Cage<Animal>();
        Cage<Lion> lions = new Cage<Lion>();
        animals.add(new Rat(true, 4, "Rat", true)); // OK to put a Rat into a Cage<Animal> 
        lions.add(new Lion(true, 4, "King", 9));
        lions.transferTo(animals); // invoke the super generic method -> animals is SUPER of lion.
        animals.showAnimals();
    }
}

In the class cage there is an invocation

cage.set.addAll(this.set);

Why does it work to invoke “cage.set…” with the dot notation althought I have neither a getSet-method nor is “set” static? What’s the technical background?

  • 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-24T11:43:24+00:00Added an answer on May 24, 2026 at 11:43 am

    You’re just using field access. Having a getSet() method would make no difference, as the Java compiler won’t use an accessor method automatically for you.

    I suspect what you’re missing is that private access isn’t determined by the object whose member you’re trying to access – the code in Cage has access to the private members of any other Cage, including the set field.

    Section 6.6 of the Java Language Specification describes access control, including:

    Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

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

Sidebar

Related Questions

I have an interface like the following: package example; import java.awt.Point; public interface Thing
package com.example.helloandroid; import java.io.File; import android.os.AsyncTask; import android.os.Environment; import android.widget.Toast; public class CheckTask extends
Let's have a following example: public class X { } public class Y {
I have the following example class: Test.h: @interface Test : UIButton { NSString *value;
I have the following example code: class A(object): def __init__(self, id): self.myid = id
I have a scala object defined like the following: package com.example object Foo {
I have the following example, compiled in VS2005, warning level 4: int main(int argc,
I have the following example of reading from a buffered reader: while ((inputLine =
I have the following example in a SQL table Cust Group Sales A 1
If I have the following example file where each number is represents a byte

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.