+ 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?
6 Antworten
+ 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
+ 1
Yes but what the point , just use the setter to set them and getter to get them ok
+ 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
+ 1
Jayakrishna🇮🇳
thank you
+ 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.
+ 1
Samuel Adejumo
yes
thanks