+ 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 ?
4 ответов
+ 3
Highly complecated
+ 3
Unless they are static, each object should have its own copy.
+ 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
+ 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