member-hiding

Exact difference between overriding and hiding

我只是一个虾纸丫 提交于 2019-11-28 19:34:37
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} public new void Test2() { Impl 4} } static Main() { A aa=new B() //This will give memory to B aa.Test1(); //What happens in terms of memory when this executes aa.Test2(); //-----------------------SAME------------------------ } Here memory is with class B but in the second statement aa.Test2 class A's method will be called. Why is it? If B has memory then B's

Usage of eclipse warning “field declaration hides another field or variable”?

怎甘沉沦 提交于 2019-11-28 10:22:48
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 where this happens: public class Test { private String caption = null; public Test(String caption) { // here this.caption = caption; } } I've seen solutions where the field is renamed, i.e "fCaption", but that would cause the automatic getters/setters that can be genereated to have odd names ( getfCaption() ). Not unreadable, but ugly... Edit: Oh yea,

Hiding Fields in Java Inheritance

大憨熊 提交于 2019-11-28 09:16:56
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); //why 1? System.out.println(father.getI()); //2 System.out.println(father.j); //why 10? System.out.println(father.getJ()); //why 10? System.out.println(); Son son = new Son(); System.out.println(son.i); //2 System.out.println(son.getI()); //2 System.out.println(son.j); //20 System.out.println(son.getJ()); //why 10? } } class Son extends Father { int i = 2; int j = 20;

Java Field Hiding

删除回忆录丶 提交于 2019-11-27 06:22:21
问题 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 boolean field with the same name but is not static and set to true . If I had this code: Superclass d = new subclass(); what would be the value of the boolean field in the superclass and the boolean field in the subclass? Does subclass field stay as

Usage of eclipse warning “field declaration hides another field or variable”?

妖精的绣舞 提交于 2019-11-27 03:36:17
问题 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 where this happens: public class Test { private String caption = null; public Test(String caption) { // here this.caption = caption; } } I've seen solutions where the field is renamed, i.e "fCaption", but that would cause the automatic getters/setters

C# - Keyword usage virtual+override vs. new

白昼怎懂夜的黑 提交于 2019-11-26 01:22:45
问题 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? 回答1: The "new" keyword doesn't override, it signifies a new method that has nothing to do with the base class method. public class Foo { public bool DoSomething() { return false; } } public class Bar : Foo { public new bool DoSomething() {