Inheritance in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Inheritance in java

Hi, just wondering if sub class gets its own copies of the super class variables and methods or does the sub class just have accsess to this code in parent class? Because I was using a visualizer tool and seen that when calling a method on the sub class it would use the code that was in the parent class. Would this mean that when we use extend it's just means that this sub class has accsess to the variables and methods of its parent but not actually have it's own copies of it ?

17th Jul 2019, 9:14 AM
D_Stark
D_Stark - avatar
4 Answers
+ 3
Highly complecated
17th Jul 2019, 12:56 PM
MADHU KUMAR KOULAS
MADHU KUMAR KOULAS - avatar
+ 3
Unless they are static, each object should have its own copy.
18th Jul 2019, 10:10 AM
Sonic
Sonic - avatar
+ 1
a subclass inherits all members of its superclass(possesses them). but it can also override its methods(only methods) and give them a new form(except those which are declared or defined private). in this case superclass will have its own implementation of its methods and the subclass will have its own different implementation of them. this is called run-time polymorphism polymorphism means different forms of a same method existing and also executing alongside each other in a program
17th Jul 2019, 3:48 PM
salar vahidi
salar vahidi - avatar
+ 1
Sonic you are right. static methods also can't be overridden alongside private and final methods(private methods can't even get called directly) in contrary, abstract methods(in an abstract class or interface) must be overridden. otherwise it will give compile-time error
18th Jul 2019, 11:15 AM
salar vahidi
salar vahidi - avatar