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
0
votes
4 answers

Method erasure: why is it said that the the derived class method hides the base class, but it works the other way around?

class Animal { public void Foo() { Console.WriteLine("Animal::Foo()"); } } class Cat : Animal { public void Foo() { Console.WriteLine("Cat::Foo()"); } } class Test { static void Main(string[] args) { Animal a; a =…
John V
  • 4,855
  • 15
  • 39
  • 63
0
votes
0 answers

Difference between "public new void Method()" and "new public void Method() in C#

I stumbled across this post here explaining how you can hide inherited members or override their values. (I was investigating the possibility of changing access modifiers when using inheritance, yes it isn't possible and it makes sense why it isn't…
Jason Loki Smith
  • 428
  • 3
  • 12
0
votes
0 answers

Accessing interface member variables in different ways with different results

While playing with lambdas for Java 8. I noticed an interesting behaviour when trying to access an interface member variable in three ways. import java.util.*; public interface Pet { String kind ="Pet"; default void interact() {…
Evan
  • 1,683
  • 7
  • 35
  • 65
0
votes
0 answers

Configure Spring Property of Subclass Field with Same Name as Parent's

Suppose I have the following classes: package wild; import moco.GeneralMovement; public class Animal{ protected GeneralMovement movement; public getMovement (){ return movement; } public void setMovement…
0
votes
1 answer

C# Hiding fields from model where xml deserialization points to

sI have one controller class, which is having private field of another class which is a model, model which gets data from xml deserialization. Because of this deserialization process I had to create public, parameterless constructor and some public…
Vilo
  • 152
  • 1
  • 1
  • 13
0
votes
2 answers

Sub class variable is not hiding the variable in super class

I just learned that if there are two variables, one in super class and one in sub class which share the same name the value assigned to the variable in the subclass will hide the value of the variable in super class. I have written a program to…
Srimanth
  • 1,042
  • 8
  • 13
0
votes
2 answers

Field with same names from parent class and implemented interface and just inside one class difference

research 2 code snippets Snippet 1: interface Int1{ String str = "123"; } class Pparent{ String str = "123"; } class F extends Pparent implements Int1{ } this code compiles normally. snippet2 class Pparent{ String str =…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
0
votes
3 answers

C# hiding with members

in the below example, what would happen? class Base { public int abc = 3; } Class Derived : Base { public int abc = 2; } static void Main() { Derived blah = new Derived(); Console.WriteLine(blah.abc); } I'm sure you would see '2' on…
Xenoprimate
  • 7,691
  • 15
  • 58
  • 95
0
votes
2 answers

Questions regarding hiding super class members

In my subclass I am hiding a super class member variable or type by redefining it in my sub class and wondering what happens to function calls that use the member variables hidden by the subclass. As an example: class A { class X { int…
Mustafa
  • 253
  • 5
  • 22
0
votes
1 answer

Is it possible to access hidden field from outside object in Java?

Consider a class, hiding member from superclass. If implementing clone, then how to update both members correctly? public class Wrapper implements Cloneable{ protected Collection core; protected Wrapper(Collection core) { this.core =…
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
0
votes
3 answers

What does it mean to say "Instance variables are not over-rided" in java?

I am aware of the concept called field hiding in java. But still I am having a confusion in relation to instance variable being not over-ridden. According to my present knowledge, overriding a method of super-class means that the JVM will call the…
Ankit
  • 609
  • 2
  • 10
  • 26
-3
votes
2 answers

superclass with private static field and subclass calls a method that changes the field, Why doesn't the field change?

public class B { private static boolean goo=true; protected static boolean foo() { goo=!goo; return goo; } public String bar="Base:"+foo(); public static void main(String[] args) { B base=new A(); …
1 2 3
4