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
222
votes
11 answers

C# - Keyword usage virtual+override vs. new

What are differences between declaring a method in a base type "virtual" and then overriding it in a child type using the "override" keyword as opposed to simply using the "new" keyword when declaring the matching method in the child type?
i3ensays
  • 2,804
  • 3
  • 19
  • 23
97
votes
9 answers

Java Name Hiding: The Hard Way

I have a problem with name hiding that is extremely hard to solve. Here is a simplified version that explains the problem: There is a class: org.A package org; public class A{ public class X{...} ... protected int net; } Then there…
gexicide
  • 38,535
  • 21
  • 92
  • 152
17
votes
2 answers

Hiding Fields in Java Inheritance

Within a class, a field that has the same name as a field in the superclass hides the superclass's field. public class Test { public static void main(String[] args) { Father father = new Son(); System.out.println(father.i);…
Wuaner
  • 929
  • 2
  • 14
  • 31
17
votes
9 answers

Exact difference between overriding and hiding

Can anybody tell the working of overriding and hiding in terms of memory and references. class A { public virtual void Test1() { //Impl 1} public virtual void Test2() { //Impl 2} } class B : A { public override void Test1() { //Impl 3} …
Akki J
  • 257
  • 2
  • 4
  • 16
16
votes
4 answers

C# Hiding, overriding and calling function from base class

I'm learning C# and I encountered the following problem. I have two classes: base and derived: class MyBase { public void MyMethod() { Console.WriteLine("MyBase::MyMethod()"); } } class MyDerived: MyBase { public void…
Lukasz Lysik
  • 10,462
  • 3
  • 51
  • 72
15
votes
1 answer

Hiding overloaded virtual function

Consider the following hierarchy of structs: struct I1 { virtual void doit() = 0; }; struct I2 { virtual void doit(int) = 0; }; struct I12 : I1, I2 { using I1::doit; using I2::doit; }; struct Derived : I12 { void doit(int)…
phimuemue
  • 34,669
  • 9
  • 84
  • 115
7
votes
1 answer

Redeclaring members in an extension hides the original member *sometimes*. Why?

By chance, I discovered that you can do this without the compiler complaining: extension Date { var timeIntervalSinceNow: TimeInterval { return 1000 } } What's weirder, is that this actually evaluates to…
Sweeper
  • 213,210
  • 22
  • 193
  • 313
5
votes
3 answers

Usage of eclipse warning "field declaration hides another field or variable"?

Eclipse has a java compiler setting called "field declaration hides another field or variable" that can be set to warning/error. How important is this warning in your opinion? What is a good standard way to handle this problem? Code example of…
Fredrik
  • 10,626
  • 6
  • 45
  • 81
5
votes
4 answers

C++ : What is happening when a non-overridden calls a overridden method?

The version of a overridden method that gets invoked depends on if you care calling the function "through" the base class or "through" the derived class. However, I am finding if I call a non-overridden method, and the overridden method calls some…
codecitrus
  • 659
  • 6
  • 17
5
votes
3 answers

Java Field Hiding

I was wondering what it means to say a field is hidden between 2 java classes and what it means when running code in terms of resulting output? I have an abstract class with a protected static boolean field = false and a sub class which has a…
daveb
  • 3,465
  • 6
  • 23
  • 28
4
votes
0 answers

(Field hiding) When does it make sense to use both a subclass field and its hidden superclass field?

In Java (as with most OO languages), you can have two classes, with one extending the other. You can have instance fields with the same name in both classes, where the subclass' instance field hides the superclass' instance field. An example is…
user442920
  • 857
  • 4
  • 21
  • 49
4
votes
4 answers

Hiding methods in subclass

I have a abstract superclass with some implemented methods. Is it possible to hide methods from this superclass in an subclass inheriting from this superclass? I don't want to have some methods visible from the superclass in some of the subclasses.…
machinery
  • 5,972
  • 12
  • 67
  • 118
3
votes
1 answer

Need help to explain Readonly\ScaffoldColumn(false)

Please help with such a question and do not judge strictly because I'm a newbie in MVC: I've got a model for storing names of users by ID in my DB public class Names { public int NameId { get; set; } public string Username { get; set; } } , a…
versus
  • 95
  • 6
3
votes
1 answer

Is it legal to have 2 header files for the same class in c++?

In a project I read, there are two header files and two declarations for the same class. One is used by programs that use this library, serving as an interface. Another is used by the library itself.The interface header file is simpler. It doesn't…
Tokubara
  • 392
  • 3
  • 13
3
votes
2 answers

Using member-hiding (`new`) to get more specific return type

I'm considering to use new to redefine members in subclasses to make more specific return type available. The question is: is this a good idea, or is it asking for troubles? The problem to be solves is when there are several "mirrored" class…
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
1
2 3 4