Can i inherit like the following? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Can i inherit like the following?

I want to inherit from a class in Java . But it would only inherit its methods and not attributes.Can somebody show me the way?

3rd Apr 2020, 12:14 PM
Wasi
Wasi - avatar
5 Réponses
+ 4
The answer to this question is a bit confusing... According to the official Java documentation https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html private members of the parent class are not inherited by the subclass. However, technically speaking, a subclass inherits the private members of its parent class, but cannot access them directly. That is only possible through public or protected methods like getters and setters.To sun up, even though private fields exist in the subclass, they are not really inherited by the parent class so you just have to declare the parent's class attributes as private
3rd Apr 2020, 3:10 PM
Vasiliki B.
Vasiliki B. - avatar
4th Apr 2020, 9:25 AM
Vasiliki B.
Vasiliki B. - avatar
0
Hmmm
3rd Apr 2020, 3:11 PM
Wasi
Wasi - avatar
0
Can you show me a code
3rd Apr 2020, 3:11 PM
Wasi
Wasi - avatar
0
It would be easier for me to understand. I am learning java for about only 12 hours
3rd Apr 2020, 3:12 PM
Wasi
Wasi - avatar