Rule Definition
Hiding data members is not good Object-Oriented practice. It is a redefinition of an instance or class variable and it makes the relationship between the base class and the derived class unclear. It is also likely to be an unintended mistake.
Remediation
The solution depends on the design:
- use the inherited member and remove the member declaration in the child class
- rename the member of the child class
Violation Code Sample
Java Sample:
public class SampleBase {
protected long a = 4;
}
public class Sample extends SampleBase {
protected int a = 5; // VIOLATION
}
C++ Sample:
class BaseClass
{
protected:
long a;
};
class DerivedClass : BaseClass
{
protected:
int a; // VIOLATION
};
Related Technologies
C++
JEE
Technical Criterion
Programming Practices - OO Inheritance and Polymorphism
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.