I am learning C# and Java and have a question in regards to inheritance.
Is it possible to override private members in a superclass (base class)? In my view, it would not be correct as the access modifier would prevent the member from being accessed.
You cannot override a private or static method in Java. If you create a similar method with same return type and same method arguments in child class then it will hide the super class method; this is known as method hiding. Similarly, you cannot override a private method in sub class because it's not accessible there.
- No, moreover, you cannot access private methods in inherited classes. - They have to be protected in the base class to allow any sort of access.
Methods a Subclass Cannot OverrideFor a discussion of final methods, see Writing Final Classes and Methods. Also, a subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method.
To override an inherited method, the method in the child class must have the same name, parameter list, and return type (or a subclass of the return type) as the parent method. Any method that is called must be defined within its own class or its superclass.
This is not silly question but it gives another concept of hiding variable.
Field in Java are only hidden and not actually overridden (that doesn't mean that we'll get a compilet time error while trying this, instead they are not overridden in its true sense). Overriding means the member should be invoked based on the run time type of the object and not based on the declared type. But binding for fields in Java is always static and hence it's based on the declared type of the object reference only. Read more about Static Binding in article - Dynamic Binding vs Static Binding >>
In case of methods, only those methods are overriden which are inherited and hence static methods are also not overridden but hidden only and they follow Static Binding only. private members (methods or fields both) are neither hidden nor overridden. They also follow Static Binding and they can not be accessed directly from any other class (including sub classes) except the class which have them. Remember, Hidden doesn't mean here we can't access the members from the subclass. So, don't confuse with being not accessible (in case of private members - fields or methods) and being hidden.
private
methods of a class are not visible in its child class so they won't get inherited.
No, you can't override private
elements, they're effectively final (because they're never visible from a subclass to be overriden.)
You can declare private
elements with the same name in the subclass, but that's not overriding the one in the superclass - it's just another private
method with the same name as the one in the superclass.
Technically, you are not overriding it.
If a subclass has a member with a name, and the superclass has a member with the same name that is marked as private, they are two different members.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With