+ 1

create an object of superclass in subclass

can we create an object of superclass in subclass in java? if yes how can we access to private variable of superclass in subclass? with setter and getter?

11th Nov 2020, 2:44 PM
saeedeh
6 odpowiedzi
+ 1
You can create but actually no need. Private variable can't be accessed in subclass for that you need setters, gmand getters... Try from this link: saeedeh https://www.codejava.net/coding/java-getter-and-setter-tutorial-from-basics-to-best-practices
11th Nov 2020, 3:44 PM
Jayakrishna 🇮🇳
+ 1
Yes but what the point , just use the setter to set them and getter to get them ok
11th Nov 2020, 5:13 PM
George S Mulbah II
George S Mulbah II - avatar
+ 1
George S Mulbah i want to access private variable of superclass in subclass for this reason i wanted to create an object of superclass in subclass
11th Nov 2020, 6:46 PM
saeedeh
+ 1
11th Nov 2020, 6:47 PM
saeedeh
+ 1
Yes you can with the help of getters and setters this applies with inheritance cases and non-inheritance cases because fields and members of a class that are private can only be accessed by that class ONLY, inheritance does not break this rule. So if you have any private member of any class that you need the access in other classes you NEED getters and setters.
11th Nov 2020, 10:30 PM
pNK
pNK - avatar
+ 1
Samuel Adejumo yes thanks
12th Nov 2020, 7:22 AM
saeedeh