It seems very silly, but I am really confused. Please see below code:  package com.one; public class SuperClass {     protected void fun() {         System.out.println("base fun");     } } ---- package com.two; import com.one.SuperClass; public class SubClass extends SuperClass{     public void foo() {         SuperClass s = new SuperClass();         s.fun(); // Error Msg: Change visibility of fun() to public      } }   I have read from oracle doc and  as well, that protected members are visible in the sub class in another package also. So fun() should be visible in SubClass in package two. Then why the error?  Am I terribly missing something very obvious?
414
In Java, are there clear rules on when to use each of access modifiers, namely the default (package private), public, protected and private, while making class and interface and dealing with inheritance?
47611
I want to use a Java file SaveProp which is written in a package user. The class file has been placed in WEBINF/classes. Below are the two lines which cause the issue.  jsp:useBean id="user" class="user.SaveProp" scope="session"  jsp:setProperty name="user" property="*"   It throws:  The value for the useBean class attribute user.SaveProp is invalid   I'm using Tomcat 6.0.
107235
#include&lt;iostream&gt; using namespace std;  class A  {    int a;    int b;    public:    void eat()    {       cout&lt;&lt;"A::eat()"&lt;&lt;endl;    } };  class B: public A {    public:    void eat()    {        cout&lt;&lt;"B::eat()"&lt;&lt;endl;     }  };  class C: public A {     public:    void eat()     {        cout&lt;&lt;"C::eat()"&lt;&lt;endl;     }  };  class D: public B, C {  };  int foo(A *ptr) {  ptr-&gt;eat();  } main() {  D obj; foo(&amp;(obj.B)); //error. How do i call with D's B part.  }   The above foo call is a compile time error.  I want to call foo with obj's B part without using virtual inheritance. How do i do that.   Also, in case of virtual inheritance, why the offset information need to be stored in the vtable. This can be determined at the compile time itself. In the above case, if we pass foo with D's object, at compile time only we can calculate the offset of D's A part.
75556
Should you ever use protected member variables?  What are the the advantages and what issues can this cause?
33281
In terms of Java, when someone asks:      what is polymorphism?   Would overloading or overriding be an acceptable answer?  I think there is a bit more to it than that.   IF you had a abstract base class that defined a method with no implementation, and you defined that method in the sub class, is that still overridding?  I think overloading is not the right answer for sure.
99846
I need to determine if a Class object representing an interface extends another interface, ie:   package a.b.c.d;     public Interface IMyInterface extends a.b.d.c.ISomeOtherInterface{     }   according to  Class.getSuperClass() will return null for an Interface.       If this Class represents either the   Object class, an interface, a   primitive type, or void, then null is   returned.   Therefore the following won't work.  Class interface = Class.ForName("a.b.c.d.IMyInterface") Class extendedInterface = interface.getSuperClass(); if(extendedInterface.getName().equals("a.b.d.c.ISomeOtherInterface")){     //do whatever here }   any ideas?
78478
Looking through some code I came across the following code  trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto));   and I'd like to know if casting this way has any advantages over   trTuDocPackTypdBd.update((TrTuDocPackTypeDto)packDto);   I've asked the developer responsible and he said he used it because it was new (which doesn't seem like a particularly good reason to me), but I'm intrigued when I would want to use the method.
52524
So I was making a class the other day and used Eclipse's method to create the equals method when I realized that it generated the following working code:  class Test {   private int privateInt;   [...]   public boolean equals(Object obj) {     [...]     Test t = (Test) obj;     if ( t.privateInt == privateInt ) {     [...]   } }   t.privateInt..???? It's suppose to be private! So I guess there is one more field visibility other than private, protected, package protected and public.  So what is happening here? How is this called? Where would somebody use this? Doesn't this break encapsulation? What if the class didn't have a mutator and I changed this? Does this happen to C++ as well? Is this an OO idiom? If not, then why did Java do it?  Generally, where can I find information about this?  Thank you.
91389
I can't seem to access instance members of the surrounding class from inside an enum, as I could from inside an inner class. Does that mean enums are static? Is there any access to the scope of the surrounding class's instance, or do I have to pass the instance into the enum's method where I need it?  public class Universe {     public final int theAnswer;      public enum Planet {         // ...         EARTH(...);         // ...          // ... constructor etc.          public int deepThought() {             // -&gt; "No enclosing instance of type 'Universe' is accessible in this scope"             return Universe.this.theAnswer;         }     }      public Universe(int locallyUniversalAnswer) {         this.theAnswer = locallyUniversalAnswer;     } }
58422
What are good reasons to prohibit inheritance in Java, for example by using final classes or classes using a single, private parameterless constructor? What are good reasons of making a method final?
77567
I've read in the  and the  the mention of inner classes. What is their purpose?  I see in the Apex Workbook that they used private variables and setter/getter methods in the inner class, but why didn't they just do that in the outer class with a constructer, getter, and setter?
105374
Backgrounder:  The  (Pointer to IMPLementation) is a technique for implementation hiding in which a public class wraps a structure or class that cannot be seen outside the library the public class is part of.  This hides internal implementation details and data from the user of the library.  When implementing this idiom why would you place the public methods on the pimpl class and not the public class since the public classes method implementations would be compiled into the library and the user only has the header file?  To illustrate, this code puts the Purr() implementation on the impl class and wraps it as well.  Why not implement Purr directly on the public class?  // header file: class Cat {     private:         class CatImpl;  // Not defined here         CatImpl *cat_;  // Handle      public:         Cat();            // Constructor         ~Cat();           // Destructor         // Other operations...         Purr(); };   // CPP file: #include "cat.h"  class Cat::CatImpl {     Purr(); ...     // The actual implementation can be anything };  Cat::Cat() {     cat_ = new CatImpl; }  Cat::~Cat() {     delete cat_; }  Cat::Purr(){ cat_-&gt;Purr(); } CatImpl::Purr(){    printf("purrrrrr"); }
6413
When programming interfaces, I've found I'm doing a lot of casting or object type conversion.  Is there a difference between these two methods of conversion?  If so, is there a cost difference or how does this affect my program?  public interface IMyInterface {     void AMethod(); }  public class MyClass : IMyInterface {     public void AMethod()     {        //Do work     }      // Other helper methods.... }  public class Implementation {     IMyInterface _MyObj;     MyClass _myCls1;     MyClass _myCls2;      public Implementation()     {         _MyObj = new MyClass();          // What is the difference here:         _myCls1 = (MyClass)_MyObj;         _myCls2 = (_MyObj as MyClass);     } }   Also, what is "in general" the preferred method?
30943
I think there must be something subtle going on here that I don't know about. Consider the following:  public class Foo&lt;T&gt; {   private T[] a = (T[]) new Object[5];    public Foo() {     // Add some elements to a   }    public T[] getA() {     return a;   } }   Suppose that your main method contains the following:  Foo&lt;Double&gt; f = new Foo&lt;Double&gt;(); Double[] d = f.getA();   You will get a CastClassException with the message java.lang.Object cannot be cast to java.lang.Double.  Can anyone tell me why? My understanding of ClassCastException is that it is thrown when you try to cast an object to a type that cannot be casted. That is, to a subclass of which it is not an instance (to quote the documentation). e.g.:  Object o = new Double(3.); Double d = (Double) o; // Working cast String s = (String) o; // ClassCastException   And it seems I can do this. If a was just a T instead of an array T[], we can get a and cast it without a problem. Why do arrays break this?  Thanks.
97448
For example:  public class A : A.B {     public class B { } }   Which generates this error from the compiler:     Circular base class dependency   involving 'A' and 'A.B'   I always figured a nested class behaved just like a regular class except with special rules concerning accessing the outer class's private members, but I guess there's some implicit inheritance occurring between the two classes?
8179
Suppose I have BaseClass with public methods A and B, and I create DerivedClass through inheritance.  e.g.  public DerivedClass : BaseClass {}   Now I want to develop a method C in DerivedClass that uses A and B.  Is there a way I can override methods A and B to be private in DerivedClass so that only method C is exposed to someone who wants to use my DerivedClass?
10304
I know how package level protection in java works. I read a lot of code (including lots of open source stuff) and no-one seem to be using it. The whole protection level seems slightly faulty to me (I'd have c#'s internal any day of the week).   Are there any legit real-world use-cases that are in common use ?  Edit: A little too late after asking this question I realized I'd forgotten to exclude the "standard" pattern of package protected implementation classes, possibly providing implementations of public interfaces. Everyone uses those, as has been noted several times in the replies. I still think there are a large number of nice replies to this question.