+ 1

Why did they decide polymophisim on variables was a bad idea?

I know about variable hiding but dont understand the purpose really and seems like it could cause bugs if the method wasnt overridden in sub. Is there some rule that a inherited method is not to affect variables of the extending class?

29th Jan 2021, 4:31 PM
D_Stark
D_Stark - avatar
1 Respuesta
+ 1
It sounds like you're referring to private properties as variables and don't know why private properties were added to Java. The designers of Java wanted to hide the complex inner details of each class from code and developers that use them. Allowing developers to make properties private is a mechanism to achieve that. Like any programming you do, you should use good judgement to decide what should be private, protected, package private, or public. If subclasses are likely to be buggy unless they override a certain method, make that method purely abstract in your superclass or turn your class into an interface. Not implementing it at all in an abstract class will force all concrete subclasses to implement it. D_Stark wrote, "Is there some rule that a inherited method is not to affect variables of the extending class?" Response: No. There is no such rule. A subclass can change almost any superclass properties. If the properties are private, they can usually be changed by calling more accessible methods on the superclass. The only properties that can't be changed are final or both private and have no non-private method to update them. This is hard to explain and understand in such abstract terms. Object-oriented design decisions are made with knowledge of specific requirements and specific types of objects. If you look at a few small class definitions and write some yourself, you'll get a better understanding of these.
31st Jan 2021, 9:20 AM
Josh Greig
Josh Greig - avatar