+ 1

Access variables from super super classes

Class two Class one extends two Class three extends one In class three, is it possible to access class one’s protected variable? I know that I can access class one’s variable from three via super.

1st Nov 2017, 2:29 AM
Morgan Lee
Morgan Lee - avatar
11 Respuestas
+ 2
@Ipang ya, I know. lol just don't like to keep a bunch of public codes for others questions in my profile. It did fit, but I added to it and then deleted the added part without re-pasting the whole code before I closed it out and deleted it. Oh well. @Morgan Basically when you extend a class, everything within the super class that is not private becomes a part of the definition of that subclass, including everything that the super class has inherited itself. It's kind of as if it is copied into the class just like you wrote it. In order to access private members of a superclass, you would need to create a method with the appropriate access modifier (public/protected/default(none)). See the classes in my example. If you create a variable within the new subclass that has the exact same name (case sensitive) as one from a super class, it will shadow the variable from the super class. If you create a method with the exact same signature as a non-static method from a super class it will override that method. If static you will shadow it. You can use the super keyword to access a method of the super class that has one of the same name in the sub class. Likewise, you can use super keyword to access the constructor of the super class. EDIT: @Ipang @Morgan There now the code is all there added in 3 parts. lol
1st Nov 2017, 5:35 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
@ChaoticDawg link your code instead to escape character limit for answers :)
1st Nov 2017, 5:10 AM
Ipang
+ 4
@ChaoticDawg, okay, satisfactory answer, guess both @Morgan and me can take that for a lesson for the day... Thanks ^_^
1st Nov 2017, 7:12 AM
Ipang
+ 3
@ChaoticDawg, thanks for noticing, I suppose the "getup" method was meant getpv? also in class three, if I do getpv(int newpv) to overload it instead of override, would it be right and works?
1st Nov 2017, 5:08 AM
Ipang
+ 2
Inheritance test, correct me if I'm wrong :) https://code.sololearn.com/czEG05Q9sZey/?ref=app
1st Nov 2017, 4:49 AM
Ipang
+ 2
This may help: // Part 1 class Two { int defTwo = 2; private int privTwo = 2; public int pubTwo = 2; protected int proTwo = 2; int getPrivTwo() { return privTwo; } void setPrivTwo(int privTwo) { this.privTwo = privTwo; } } class One extends Two { int defOne = 1; private int privOne = 1; public int pubOne = 1; protected int proOne = 1; public int getPrivOne() { return privOne; } public void setPrivOne(int privOne) { this.privOne = privOne; } } class Three extends One { int defThree = 3; private int privThree = 3; public int pubThree = 3; protected int proThree = 3; public int getPrivThree() { return privThree; } public void setPrivThree(int privThree) { this.privThree = privThree; } } Continued....
1st Nov 2017, 5:33 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@ Ipang you don't need the getpv method in class Three as it is inherited from class Two and they match. You are effectively overriding it with a match of itself. Other than that it looks correct. You actually don't even need the method as you can access the variable directly too.
1st Nov 2017, 5:03 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
LOL, I'm just realizing my code got cut off when I pasted it in.
1st Nov 2017, 5:08 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@Ipang yup, stupid autocorrect. It's fixed now!
1st Nov 2017, 5:08 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
// Part 3. Bottom of Program class... // You cannot access private variables that are // inherited from a super class directly through // the object of a child class // System.out.println(three.privOne); // No Error // three.privOne = 4; // No Error // System.out.println(three.privOne); // No Error // System.out.println(three.privTwo); // No Error // three.privTwo = 5; // No Error // System.out.println(three.privTwo); // No Error // You can, however access private variables // that are inherited from a super class via // public/protected/default level access modified // getter and setter methods System.out.println(three.getPrivOne()); three.setPrivOne(4); System.out.println(three.getPrivOne()); System.out.println(three.getPrivTwo()); three.setPrivTwo(5); System.out.println(three.getPrivTwo()); } }
1st Nov 2017, 5:33 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
// Part 2. Top of Program class public class Program { public static void main(String[] args) { Three three = new Three(); // You can access default/package variables that are // inherited from a super class directly through // the object of a child class System.out.println(three.defOne); // Yes three.defOne = 4; // Yes System.out.println(three.defOne); // Yes System.out.println(three.defTwo); // Yes three.defTwo = 5; // Yes System.out.println(three.defTwo); // Yes // You can access public variables that are // inherited from a super class directly through // the object of a child class System.out.println(three.pubOne); // Yes three.pubOne = 4; // Yes System.out.println(three.pubOne); // Yes System.out.println(three.pubTwo); // Yes three.pubTwo = 5; // Yes System.out.println(three.pubTwo); // Yes // You can access protected variables that are // inherited from a super class directly through // the object of a child class System.out.println(three.proOne); // Yes three.proOne = 4; // Yes System.out.println(three.proOne); // Yes System.out.println(three.proTwo); // Yes three.proTwo = 5; // Yes System.out.println(three.proTwo); // Yes // Contiued....
1st Nov 2017, 5:33 AM
ChaoticDawg
ChaoticDawg - avatar