Why can’t my subclass access a protected variable of its superclass, when it’s in a different package?

It works, but only you the children tries to access it own variable, not variable of other instance ( even if it belongs to the same inheritance tree ). See this sample code to understand it better: //in Parent.java package parentpackage; public class Parent { protected String parentVariable = “whatever”;// define protected variable } // … Read more

protected vs public constructor for abstract class? Is there a difference?

They are the same for all practical purposes. But since you asked for differences, one difference I can think of is if you are searching for the class’s constructor using reflection, then the BindingFlags that match will be different. BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; var constructor = typeof(MyClass).GetConstructor(flags, null, new Type[0], null); This will … Read more

accessing a protected member of a base class in another subclass

When foo receives a FooBase reference, the compiler doesn’t know whether the argument is a descendant of Foo, so it has to assume it’s not. Foo has access to inherited protected members of other Foo objects, not all other sibling classes. Consider this code: class FooSibling: public FooBase { }; FooSibling sib; Foo f; f.foo(sib); … Read more

Why can’t I use protected constructors outside the package? [duplicate]

Usually protected means only accessible to subclasses or classes in the same package. However here are the rules for constructors from the JLS: 6.6.2.2. Qualified Access to a protected Constructor Let C be the class in which a protected constructor is declared and let S be the innermost class in whose declaration the use of … Read more

Reasons to use private instead of protected for fields and methods

There is some consensus that one should prefer composition over inheritance in OOP. There are several reasons for this (google if you’re interested), but the main part is that: inheritance is seldom the best tool and is not as flexible as other solutions the protected members/fields form an interface towards your subclasses interfaces (and assumptions … Read more

difference between protected and package-private access modifiers in Java? [duplicate]

The first answer is basically correct – protected members can be accessed by classes from the same package subclasses of the declaring class from other packages However, there is a little trick: 6.6.2 Details on protected Access A protected member or constructor of an object may be accessed from outside the package in which it … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)