0

Is this right?

So I read this today and couldent really make sense of it. 3)third difference is that inner class can modify data members of the outer class whereas subclasses cannot.. ______________________________________________ Wouldent this example be a subclass modifying its parents data ? //let's say parent as a field String x = "blabla" void changeSuper(){ super.x="Hello"; };

4th Feb 2021, 1:03 PM
D_Stark
D_Stark - avatar
3 Answers
+ 1
You're correct and they're wrong unless they're referring to private properties and methods specifically. They should have said "inner class can modify PRIVATE data members..." This compiles but a subclass won't have the same direct access to the private x property defined below: import java.awt.event.*; public class Test { private int x; Test() { Object o = new ActionListener() { public void actionPerformed(ActionEvent ae) { Test.this.x = 3; } }; } }
4th Feb 2021, 1:48 PM
Josh Greig
Josh Greig - avatar
+ 1
There is a lot of correct information on Java but definitely Oracle's official documentation is more reliable. Anyone can make mistakes but the owner of Java will make fewer mistakes on Java. Here is one of Oracle's tutorials on inner classes: https://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html
4th Feb 2021, 3:48 PM
Josh Greig
Josh Greig - avatar
0
Josh Greig that's what thought, I understand that creating a variable in subclass that as the same name as parent wont modify the parent variable but also wont be polymophic, I'll try this with a nested class to see what the diffrenece is because I think it has somthing to do with that, I really should say away from google it always trying to confuse me 😁
4th Feb 2021, 2:18 PM
D_Stark
D_Stark - avatar