Java field inheritance

Are fields only inherited "one level up"?

By this, I mean that if I have a Superclass for a class that then has a subclass, and Superclass has a field, the class inherits it, and the subclass will not. It's right?

And if so, is there a way to make Subclass automatically inherit a field from a superclass, given that, as I understand it, there is no way to inherit two classes at once?

Thanks to everyone who is in no hurry to answer. I understand that my question may be impractical, and in fact you are probably just redefining the field or something else, but I'm not trying to do something specific, just trying to find out how Java works. Thanks.

Here is my code:

public class SuperClass { protected int entero; protected void method(){ entero=1; } public class SubClass extends Class { public SubClass(){} } public class Class extends SuperClass { public Class(){} } public static void main(String[] args){ Class object= new Class(); SubClass subobject= new SubClass(); /*This is where I get an error, why?*/ subobject.entero=2; /*This one is fine*/ object.entero=2; object.method(); System.out.println(object.entero); } 
+4
source share
3 answers

Any class B than extends class A inherits A's fields. If the class C extends B , C inherits all infrequent field instances and methods from A and B , i.e. Transitivity is in progress.

If the field is private , then it cannot be directly changed from a subclass; however, you can get around this using the setter / getter methods.

If the field is protected , then the subclass has direct access to it.

EDIT 1:

In a comment, you say that the field is protected , but you still cannot access it from a subclass. The only thing I can think of is that you have this situation:

 class A { protected int x; } class B extends A { private int x; } class C extends B { private int z = x; } 

It will NOT , because by declaring x again in B , you are hiding the field x from A So now C sees x as B's private variable x , which you do not have access to.

EDIT 2 : I am not going to delete the above edit because it is informative, but now that you have posted your code, it is because your SubClass does not actually extend anything (this was later fixed in the edit).

+4
source

Inheritance in Java is transitive.

If your classes are Superclass <Class <Subclass, then Subclass inherits all implicit field instances and methods provided by superclasses that are not overridden or not hidden by the class or subclass.

One level of inheritance is specified by the Java Language Specification, Section 8.4.8: Inheritance, Redirection, and Hiding :

Class C inherits from its direct superclass and direct superinterfaces all abstract and non-abstract methods of the superclass and superinterfaces that are public, protected, or declared with default access in the same package as C, and are neither overridden (Β§8.4.8.8.1 ) and is not hidden (Β§8.4.8.2) by a declaration in the class.

+3
source

If you have classes that:

 public class SuperClass { public void methodName() {} //SuperClass stuff } public class MidClass extends SuperClass { //MidClass stuff } public class SubClass extends MidClass { //SubClass Stuff } 

Then this is quite true for your main method:

 SubClass sc = new SubClass(); sc.methodName(); 
0
source

Source: https://habr.com/ru/post/1491931/


All Articles