I need to make an inherited class from a base class with a selected variable. for example, if the base class have 3 variable name, age, marks but inherit class must have name and marks only
how can we do it
CodePudding user response:
When designing object-oriented code, subclasses should be the specializations. The situation you describe makes the base class the specialization because it has more specific requirements than the subclass.
There is a principle called the "Liskov Substitution Principle" that says all subclasses should work where the base class works - and this wouldn't be the case as calling subclass.age
would fail.
Instead, the base class should have the two common properties, and there should be a subclass that represents the extended class that represents the situation where an age would be used.
Score
- Name
- Marks
AgedScore extends Score
- Age
The names are examples here, ideally you'd name them after what they relate to in the business domain.