If both base and derive class has a same member variable, how compiler resolve which member is to be called?
The m1
in der
hides the name of base
's m1
, so any access to m1
via a der
object gets you that instance's der::m1
. You can access base::m1
in der
like this:
class der: public base {
public:
int m1;
der() {
m1 = 6;
base::m1=7; // access base object's m1 inside der
}
};
And you can access the base object thus:
der d;
d.base; // base object
std::cout << d.base::m1 >> "\n"; // access base object's m1 outside of der (if allowed)
same data members in both base and derived class
The width
and height
data members in B
hide (or shadow) those in A
.
They serve no useful purpose in this case and should be removed
If you want to access the hidden (or shadowed) data members, you can use scope resolution:
int area()
{
return (A::width * A::height);
}
Static member variables with same name in base and derived classes
Yes, all the same reasons to avoid shadowing non-virtual functions apply to (vacuously non-virtual) members;
I'm going to assume that the override of check()
in Derived is textually identical to that of Base.
You can instead use a virtual method with static locals
class Base
{
// ...
virtual /*const?*/ std::vector<std::string> & filter()
{
static std::vector<std::string> value = ...
return value;
}
bool check() // final
{
if(std::find(filter()...))
}
}
class Derived : public Base
{
/*const?*/ std::vector<std::string> & filter() // override
{
static std::vector<std::string> otherValues = ...
return otherValues;
}
}
C++ Using two variables of same name in Derived and Base classes
Would this be considered bad practice?
Yes, it would be bad practice, because the var
in the Base
would be unused. It does not look like DerivedB
should be deriving from BaseB
: instead, they should be derived from the same abstract base class, like this:
class AbstractB {
public:
virtual void anotherThing1() = 0;
virtual void anotherThing2() = 0;
virtual void anotherThing3() = 0;
};
class DerivedB1 : public AbstractB { // Former BaseB
BaseA *var;
public:
DerivedB1() {
var = new BaseA();
}
virtual void anotherThing1();
virtual void anotherThing2();
virtual void anotherThing3();
};
class DerivedB2 : public AbstractB { // Former DerivedB
DerivedA *var;
public:
DerivedB2() {
var = new DerivedA();
}
void anotherThing1();
void anotherThing2();
void anotherThing3();
void andAnother1();
void andAnother2();
};
General principle in use here is that you should try making all your non-leaf classes in your inheritance hierarchy abstract.
Having 2 variables with the same name in a class that extends another class in Java
In a sense, you are overriding fields of the super class. But it's far easier to do accidentally because there is no overloading of fields (you only have one variable of a given name, the type doesn't matter). This is referred to as variable 'hiding' or 'shadowing'. So, you're correct, you'll end up with two fields with the same name.
Your second example is correct. They are inherited from the super-class and since they are not declared private, they are visible to the subclass. It's generally bad practice to refer directly to a super-class's fields, and unless there is good reason, they should declared private. Your example of invoking the super constructor is the best approach.
Also, if you hide a field with another of the same name, you can still refer to them as super.x, super.y, vs. this.x, this.y, you should avoid this situation if at all possible.
Can two inherited classes access the same variable in a base class?
You are mixing up classes and objects. When you have a base class with some member variable then all derived classes can (potentially) access the member variable in the base class.
However when you instantiate the different derived classes, or one of them multiple times, then each object has a complete independent set of member variables, including the ones from the base class, because that's exactly what an object is.
If you want certain groups of objects to use the same "shared" variables then you could stick these shared things into a dedicated "shared" class. Then for each group of objecst that need to share these "variables", you create an instance of this "shared" class, and pass it to all of the objects in the group.
This is also more flexible than fiddling around with "static" class members, or whatever they are called in the language you are using, because you can have multiple of these "groups" of objects by having multiple "shared" objects and deciding exactly which "actual" objects share the same "shared" data/variables.
Without knowing more about what's going on it's hard to give any more recommendations on how to design the class and object hierarchy.
Related Topics
How to Send Date in Rest API in Post Method
How to Set Environment Variable or System Property in Spring Tests
Null Pointer on an Autowired Bean Which Is Not Mocked by Mockito
Serialize a Double to 2 Decimal Places Using Jackson
How to Get Values of All Elements from Xml String in Java
How to Enable Request Scope in Async Task Executor
Can We Use Multipart and @Requestbody Together in Spring
Convert Yyyy-Mm-Dd Hh:Mm:Ss.Sss Zzz Format Strin to Date Object
How to Convert a Kotlin Source File to a Java Source File
Java Classes to Generate the Given Json String
How to Trim White Space from All Elements in Array
Java: How to Print Array Without Square Brackets and Commas
How to Get Http Response Code Using Selenium Webdriver
Remove Duplicate Values from Hashmap in Java
How to Configure Hikaricp in My Spring Boot App in My Application.Properties Files