Questions tagged [member-hiding]

Declaring a class member in a derived class with the same name as a member in a base class.

Default behaviour for C# is that by hiding a member in a base class only that in a derived class is accessible. However it is a good practice to mark the member in the derived class with the new keyword in order to dispose of a compiler warning.

57 questions
3
votes
5 answers

Hide, overload or overwrite in C++

Then I have a question again. something like this: #include using namespace std; class Base { public: void foo() { cout<<"Base."<
Kevin Wang
  • 135
  • 1
  • 8
3
votes
2 answers

Having a more concrete interface in each derived class in c#

I try to give an example as simple as possible but the very essence of the question is quite confusing at least to me. In order to reuse code and not repeat my self I have an interface IStudent that implements another interface IPerson. interface…
Adrian Hristov
  • 1,957
  • 2
  • 16
  • 22
3
votes
3 answers

Java field hiding

In the following scenario: class Person{ public int ID; } class Student extends Person{ public int ID; } Student "hides ID field of person. if we wanted to represent the following in the memory: Student john = new Student(); would…
Bober02
  • 15,034
  • 31
  • 92
  • 178
2
votes
2 answers

Is hiding a member in the base class a bad smell in the code?

To me, it sounds more like an exception to hide a member in the base class in order to hack something instead of refactoring a bad design... Maybe I am wrong. But... Is it a code smell to hide a member in the base class with 'new' keyword?
pencilCake
  • 51,323
  • 85
  • 226
  • 363
2
votes
2 answers

Access hidden field from subclass of class that hides it

How can you access a protected field in a base class's base class that is hidden by a field in a base class? An example: package foo; public class Foo { protected int x; int getFooX() { return x; } } package bar; public class Bar extends…
Artyer
  • 31,034
  • 3
  • 47
  • 75
2
votes
3 answers

Why is the output different in the two cases?

Why is the output different in the below case even when, the variable has been overridden? public class A { int a = 500; void get() { System.out.println("a is " + this.a); } } public class B extends A { int a =…
2
votes
2 answers

Instance method overiding vs Field hiding

Below is the code, package ClassesOverridingHidingAccess; interface I{ int x = 0; } class T1 implements I{ int x = 1; String s(){ return "1"; } } class T2 extends T1{ int x = 2; String s(){ return "2"; …
overexchange
  • 15,768
  • 30
  • 152
  • 347
2
votes
2 answers

overriding functions in c++

#include using namespace std; class Base { public: virtual void some_func(int f1) { cout <<"Base is called: value is : " << f1 <
user2553620
  • 77
  • 1
  • 3
2
votes
1 answer

Varargs with inheritance in Java

Suppose I have a class Foo like so: public class Foo { protected T invoke(Object... args); } Now suppose I add this: public class Bar extends Foo { public T invoke(Arg arg); } Does this mean that I cannot create a Bar
Kelvin Chung
  • 1,327
  • 1
  • 11
  • 23
1
vote
4 answers

C# List hides properties?

Possible Duplicate: How does List make IsReadOnly private when IsReadOnly is an interface member? Okay, this is driving me nuts. List implements IList. However, IList list = new List(); bool b = list.IsReadOnly; bool c =…
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
1
vote
2 answers

How method hiding works in Java?

class Penguin { public static int getHeight() { return 3; } public void printInfo() { System.out.println(this.getHeight()); } } public class CrestedPenguin extends Penguin { public static int getHeight() { …
Salem KRAB
  • 31
  • 4
1
vote
2 answers

Access modifiers and hiding Java fields

This is a follow-up to the question: "java access modifiers and overriding". The former generally deals with Java methods however. Why the flexibility with Java fields? We can narrow or widen the visibility with their respect in an inherited class…
yesilupper
  • 813
  • 1
  • 7
  • 7
1
vote
1 answer

AutoMapper - Map child property hiding base class property

I'm encountering a quite stupid issue while trying to map a class to a derived class using AutoMapper with C#. These are my classes: class BaseParent { public string Name { get; set; } public BaseChild Child { get; set; } } class BaseChild…
Flea777
  • 1,012
  • 9
  • 21
1
vote
1 answer

question about hiding/shadowing member-variables in Java

I want to understand how hiding works in Java. So lets assume you have following code public class A{ protected SomeClass member; public A(SomeClass member){ this.member = member; } } public class B extends A{ protected…
Rafael T
  • 15,401
  • 15
  • 83
  • 144
1
vote
2 answers

Hiding interface-member in derived interface

I just decompiled some 3rd-party interface and scratched my head about the following: public interface IFoo { string Name { get; set; } } public interface IBar : IFoo { new string Name { get; set; } } As you can see I have two interfaces,…
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111