Search code examples
javaattributesbehaviorsuperclass

Java Beginner: Subclasses and Superclasses -Can you remove values?


I'm a Java beginner and I'm looking into subclasses and superclass's.

I know you can add values/attributes/behaviour that aren't in a superclass to a subclass to make it more 'task specific'.

But my question is, can I remove an attribute or behaviour value that belongs in a superclass from a subclass?


Solution

  • extend implies inheritance. You don't have a precise choice over what you can inherit and what you can't.

    If parent class has decided to expose some public variables etc, sub class cannot alter that.

    If the parent class doesn't want to expose some fields those can be marked as private.

    Also: A class should be open for extending it but closed for changing it.